From 457330a89f7e6ad37808b855f0e510f0c560b46b Mon Sep 17 00:00:00 2001
From: FaserMC <fasermc@cern.ch>
Date: Tue, 17 Jan 2023 08:01:12 +0100
Subject: [PATCH] Update scripts for MC reco

---
 .../Digitization/scripts/submit_faser_digi.sh |  4 +-
 .../scripts/submit_faser_digi_merge.sh        |  5 +-
 .../scripts/submit_faser_reco.sh              | 16 +++-
 .../scripts/faser_ntuple_maker.py             | 85 ++++++++++++++-----
 .../scripts/submit_faser_ntuple_maker.sh      | 10 ++-
 5 files changed, 93 insertions(+), 27 deletions(-)

diff --git a/Control/CalypsoExample/Digitization/scripts/submit_faser_digi.sh b/Control/CalypsoExample/Digitization/scripts/submit_faser_digi.sh
index 1274dc73..532bf0b5 100644
--- a/Control/CalypsoExample/Digitization/scripts/submit_faser_digi.sh
+++ b/Control/CalypsoExample/Digitization/scripts/submit_faser_digi.sh
@@ -2,10 +2,10 @@
 # Used with a condor file to submit to vanilla universe
 #
 # Usage:
-# submit_faser_digi.sh [--highGain] filepath [release_directory] [working_directory]
+# submit_faser_digi.sh filepath [release_directory] [working_directory]
 #
 # Options:
-#   --highGain - apply high gain settings to the Calorimeter PMTs (for muons)
+#   --digiTag <tag> - override digitization tag for calo gain
 #   --geom - geometry setting
 #   --out - specify output location (in EOS) to copy output HITS file
 #   --log - specify output location (in EOS) for log file
diff --git a/Control/CalypsoExample/Digitization/scripts/submit_faser_digi_merge.sh b/Control/CalypsoExample/Digitization/scripts/submit_faser_digi_merge.sh
index 62095daa..d8ac88cf 100755
--- a/Control/CalypsoExample/Digitization/scripts/submit_faser_digi_merge.sh
+++ b/Control/CalypsoExample/Digitization/scripts/submit_faser_digi_merge.sh
@@ -2,10 +2,11 @@
 # Used with a condor file to submit to vanilla universe
 #
 # Usage:
-# submit_faser_digi_merge.sh [--highGain] dirpath slice nfiles [release_directory] [working_directory] 
+# submit_faser_digi_merge.sh dirpath slice nfiles [release_directory] [working_directory] 
 #
 # Options:
-#   --highGain - apply high gain settings to the Calorimeter PMTs (for muons)
+#   --digiTag <tag> - override digitization tag for calo gain
+#   --partial - allow missing files in merge
 #   --geom - geometry setting
 #   --out - specify output location (in EOS) to copy output HITS file
 #   --log - specify output location (in EOS) for log file
diff --git a/Control/CalypsoExample/Reconstruction/scripts/submit_faser_reco.sh b/Control/CalypsoExample/Reconstruction/scripts/submit_faser_reco.sh
index 20001adc..5f1ee066 100755
--- a/Control/CalypsoExample/Reconstruction/scripts/submit_faser_reco.sh
+++ b/Control/CalypsoExample/Reconstruction/scripts/submit_faser_reco.sh
@@ -8,7 +8,10 @@
 #   --out - specify output location (in EOS) to copy output HITS file
 #   --log - specify output location (in EOS) for log file
 #   --geom - specify geometry
+#
+# Monte Carlo options:
 #   --isMC - needed for MC reco
+#   --digiTag <tag> - override MC reco tag for calo gain (matches digi tag)
 # 
 # file_path - full file name (with path)
 # release_directory - optional path to release install directory (default pwd)
@@ -25,6 +28,9 @@
 # Keep track of time
 SECONDS=0
 #
+# Job option strings
+gainstr=""
+#
 # Parse command-line options
 while [ -n "$1" ]
 do 
@@ -45,9 +51,16 @@ do
 	  shift;;
 
       --isMC)
+	  echo "Set isMC true"
 	  ismc=1
 	  shift;;
 
+      --digiTag)
+	  echo "Override calo digi tag with $2"
+	  gainstr="--MC_calibTag $2"
+	  shift;
+	  shift;;
+
       --) # End of options
 	  shift; # Eat this
 	  break;; # And stop parsing
@@ -119,6 +132,7 @@ echo `date` - $HOSTNAME
 echo "File: $file_name"
 echo "Filepath: $file_path"
 echo "Geom: $geom"
+echo "Gain: $gainstr"
 echo "Release: $release_directory"
 echo "Output: $output_directory"
 echo "Starting: $starting_directory"
@@ -216,7 +230,7 @@ else
     mcstr="--isMC"
 fi
 #
-faser_reco.py "--nevents=$nevents" $geomstr $tagstr $mcstr "$file_path" 
+faser_reco.py "--nevents=$nevents" $geomstr $tagstr $mcstr $gainstr "$file_path" 
 reco_code=$?
 echo "Return code: $reco_code"
 #
diff --git a/PhysicsAnalysis/NtupleDumper/scripts/faser_ntuple_maker.py b/PhysicsAnalysis/NtupleDumper/scripts/faser_ntuple_maker.py
index 35a4df7a..5ae87306 100755
--- a/PhysicsAnalysis/NtupleDumper/scripts/faser_ntuple_maker.py
+++ b/PhysicsAnalysis/NtupleDumper/scripts/faser_ntuple_maker.py
@@ -22,7 +22,9 @@ parser.add_argument("path",
 parser.add_argument("--slice", type=int, default=0,
                     help="Specify ordinal output file to produce")
 parser.add_argument("--files", type=int, default=1,
-                    help="Specify files per slice")
+                    help="Specify reco files per slice")
+parser.add_argument("--merge", type=int, default=1,
+                    help="Specify merged files per reco file (MC only)")
 parser.add_argument("--last", type=int, default=0,
                     help="Specify last file in slice (normally --files)")
 
@@ -42,6 +44,11 @@ parser.add_argument("--isMC", action='store_true',
 parser.add_argument("--partial", action='store_true',
                     help="Allow partial input files")
  
+parser.add_argument("--fluka", action='store_true',
+                    help="Add FLUKA weights to ntuple")
+parser.add_argument("--genie", action='store_true',
+                    help="Add Genie weights to ntuple")
+
 args = parser.parse_args()
 
 from pathlib import Path
@@ -53,21 +60,42 @@ filelist = []
 # If this is a directory, need to create file list
 if filepath.is_dir():
 
-    # Parsing MC is tricky
-    if args.isMC:
-        print("Monte Carlo not supported yet!")
-        sys.exit(0)
-
     # Use expected data pattern to find files
     runstr = filepath.stem
-    start = args.slice * args.files
-    if args.last > 0:
-        end = start + args.last
+
+    # Make list of segments to search for
+    seglist = []
+    if args.merge > 1:
+        start = args.slice * args.files * args.merge
+
+        # Number of files to combine
+        if args.last > 0:
+            num = args.last
+        else:
+            num = args.files
+
+        # Make segment list
+        for i in range(start, start+num*args.merge, args.merge):
+            seg = f"{i:05d}-{(i+args.merge-1):05d}"
+            seglist.append(seg)
+
     else:
-        end = start + args.files
+        start = args.slice * args.files
+        if args.last > 0:
+            end = start + args.last
+        else:
+            end = start + args.files
+
+        seglist = [f'{seg:05d}' for seg in range(start, end)]
+
+
+    for seg in seglist:
+
+        if args.isMC:
+            searchstr = f"FaserMC-*-{runstr}-{seg}-*xAOD.root"
+        else:
+            searchstr = f"Faser-Physics-{runstr}-{seg}-*xAOD.root"
 
-    for seg in range(start, end):
-        searchstr = f"Faser-Physics-{runstr}-{seg:05d}-*xAOD.root"
         flist = list(filepath.glob(searchstr))
         if len(flist) == 0:
             print(f"Didn't find file {searchstr}!")
@@ -88,19 +116,28 @@ if filepath.is_dir():
 
     firstfile = Path(filelist[0])
     firststem = str(firstfile.stem)
+    firstfaser = firststem.split('-')[0]
+    firstshort = firststem.split('-')[1]
     firstseg = firststem.split('-')[3]
+    if args.merge > 1:
+        firstseg2 = firststem.split('-')[4]
 
     lastfile = Path(filelist[-1])
     laststem = str(lastfile.stem)
     lastseg = laststem.split('-')[3]
+    if args.merge > 1:
+        lastseg = laststem.split('-')[4]
 
     # Find any tags
-    tagstr = firststem.replace(f"Faser-Physics-{runstr}-{firstseg}", "")
+    tagstr = firststem.replace(f"{firstfaser}-{firstshort}-{runstr}-{firstseg}", "")
+    if args.merge > 1:
+        tagstr = tagstr.replace(f"-{firstseg2}", "")
+
     tagstr = tagstr.replace("-xAOD", "")
     print(f"Tag = {tagstr}")
 
     # Build output name
-    outfile = f"Faser-Physics-{runstr}-{firstseg}-{lastseg}"
+    outfile = f"{firstfaser}-{firstshort}-{runstr}-{firstseg}-{lastseg}"
 
     # This will include the leading -
     if len(tagstr) > 0:
@@ -161,12 +198,11 @@ Configurable.configurableRun3Behavior = True
 # Configure
 ConfigFlags.Input.Files = filelist
 ConfigFlags.IOVDb.GlobalTag = "OFLCOND-FASER-03"         # Always needed; must match FaserVersionS
+ConfigFlags.Input.isMC = args.isMC
 if args.isMC:
     ConfigFlags.IOVDb.DatabaseInstance = "OFLP200"           # Use MC conditions 
-    ConfigFlags.Input.isMC = True                            # Needed to bypass autoconfig
 else:
     ConfigFlags.IOVDb.DatabaseInstance = "CONDBR3"           # Use data conditions
-    ConfigFlags.Input.isMC = False                           # Needed to bypass autoconfig
 
 ConfigFlags.Input.ProjectName = "data21"                     # Needed to bypass autoconfig
 
@@ -184,14 +220,21 @@ acc.merge(PoolReadCfg(ConfigFlags))
 # algorithm
 from NtupleDumper.NtupleDumperConfig import NtupleDumperAlgCfg
 if args.isMC:
-    acc.merge(NtupleDumperAlgCfg(ConfigFlags, outfile, UseFlukaWeights=True))
+    if args.genie:
+        acc.merge(NtupleDumperAlgCfg(ConfigFlags, outfile, UseGenieWeights=True))
+    elif args.fluka:
+        acc.merge(NtupleDumperAlgCfg(ConfigFlags, outfile, UseFlukaWeights=True))
+    else:
+        acc.merge(NtupleDumperAlgCfg(ConfigFlags, outfile))
+
 else:
     acc.merge(NtupleDumperAlgCfg(ConfigFlags, outfile))
 
-from AthenaConfiguration.ComponentFactory import CompFactory
-AthenaEventLoopMgr = CompFactory.AthenaEventLoopMgr()
-AthenaEventLoopMgr.EventPrintoutInterval=1000
-acc.addService(AthenaEventLoopMgr)
+if not args.verbose:
+    from AthenaConfiguration.ComponentFactory import CompFactory
+    AthenaEventLoopMgr = CompFactory.AthenaEventLoopMgr()
+    AthenaEventLoopMgr.EventPrintoutInterval=1000
+    acc.addService(AthenaEventLoopMgr)
 
 # Hack to avoid problem with our use of MC databases when isMC = False
 if not args.isMC:
diff --git a/PhysicsAnalysis/NtupleDumper/scripts/submit_faser_ntuple_maker.sh b/PhysicsAnalysis/NtupleDumper/scripts/submit_faser_ntuple_maker.sh
index 429ebc71..9a5a557b 100755
--- a/PhysicsAnalysis/NtupleDumper/scripts/submit_faser_ntuple_maker.sh
+++ b/PhysicsAnalysis/NtupleDumper/scripts/submit_faser_ntuple_maker.sh
@@ -10,6 +10,7 @@
 #   --log - specify output location (in EOS) for log file
 #   --isMC - needed for MC reco
 #   --partial - allow missing files
+#   --merge - merge factor of reco files (for MC)
 #
 # dirpath - full directory path to HITS files
 # slice - ordinal output file number
@@ -28,6 +29,7 @@ SECONDS=0
 # Defaults
 ismc=""
 partialstr=""
+mergestr=""
 #
 # Parse command-line options
 while [ -n "$1" ]
@@ -52,6 +54,11 @@ do
 	  partialstr="--partial"
 	  shift;;
 
+      --merge)
+	  mergestr="--merge $2";
+	  shift;
+	  shift;;
+
       --) # End of options
 	  shift; # Eat this
 	  break;; # And stop parsing
@@ -140,6 +147,7 @@ echo `date` - $HOSTNAME
 echo "Directory: $dir_path"
 echo "Slice: $slice"
 echo "NFiles: $nfiles"
+echo "Merge: $mergestr"
 echo "Release: $release_directory"
 echo "Output: $output_directory"
 echo "Starting: $starting_directory"
@@ -219,7 +227,7 @@ export EOS_MGM_URL=root://eospublic.cern.ch
 #
 # Run job
 #
-faser_ntuple_maker.py $last_file_str $partialstr $tagstr $ismc --slice $slice --files $nfiles $dir_path
+faser_ntuple_maker.py $last_file_str $partialstr $tagstr $ismc --slice $slice --files $nfiles $mergestr $dir_path
 ntup_code=$?
 echo "Return code: $ntup_code"
 #
-- 
GitLab