From a0a8bbd2a78e0313b4c4c7000bac9405a3373326 Mon Sep 17 00:00:00 2001
From: Eric Torrence <eric.torrence@cern.ch>
Date: Wed, 7 Dec 2022 00:30:35 +0100
Subject: [PATCH] Updates for digitization parameters

---
 .../CaloDigiAlgs/python/CaloDigiAlgsConfig.py | 19 ++++++---
 .../Digitization/scripts/faser_digi.py        | 41 +++++++++++++++++--
 .../Digitization/scripts/faser_digi_merge.py  | 20 +++++----
 .../Reconstruction/scripts/faser_reco.py      |  4 +-
 4 files changed, 67 insertions(+), 17 deletions(-)

diff --git a/Calorimeter/CaloDigiAlgs/python/CaloDigiAlgsConfig.py b/Calorimeter/CaloDigiAlgs/python/CaloDigiAlgsConfig.py
index 816da133..859ceec2 100644
--- a/Calorimeter/CaloDigiAlgs/python/CaloDigiAlgsConfig.py
+++ b/Calorimeter/CaloDigiAlgs/python/CaloDigiAlgsConfig.py
@@ -9,6 +9,8 @@ from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
 from WaveformConditionsTools.WaveformCableMappingConfig import WaveformCableMappingCfg
 #from WaveformConditionsTools.WaveformDigitizationConfig import WaveformDigitizationCfg
 
+from IOVDbSvc.IOVDbSvcConfig import addFolders
+
 # One stop shopping for normal FASER data
 def CaloWaveformDigitizationCfg(flags, **kwargs):
     """ Return all algorithms and tools for Waveform digitization """
@@ -17,17 +19,22 @@ def CaloWaveformDigitizationCfg(flags, **kwargs):
     if not flags.Input.isMC:
         return acc
 
+    # Define folder and tag
+    dbInstance = kwargs.get("dbInstance", "TRIGGER_OFL")
+    dbFolder = kwargs.get("dbFolder", "/WAVE/Digitization")
+    caloDigiTag = kwargs.pop("digiTag", "")
+
+    if len(caloDigiTag) > 0:
+        print(f"Found digiTag={caloDigiTag}")
+        acc.merge(addFolders(flags, dbFolder, dbInstance, className="CondAttrListCollection", tag=caloDigiTag))
+    else:
+        acc.merge(addFolders(flags, dbFolder, dbInstance, className="CondAttrListCollection"))
+
     acc.merge(CaloWaveformDigiCfg(flags, "CaloWaveformDigiAlg", **kwargs))
     acc.merge(CaloWaveformDigitizationOutputCfg(flags))
     acc.merge(WaveformCableMappingCfg(flags))
     #acc.merge(WaveformDigitizationCfg(flags))
 
-    # Just do this here for now
-    dbInstance = kwargs.get("dbInstance", "TRIGGER_OFL")
-    dbFolder = kwargs.get("dbFolder", "/WAVE/Digitization")
-    from IOVDbSvc.IOVDbSvcConfig import addFolders
-    acc.merge(addFolders(flags, dbFolder, dbInstance, className="CondAttrListCollection"))
-
     return acc
 
 # Return configured digitization algorithm from SIM hits
diff --git a/Control/CalypsoExample/Digitization/scripts/faser_digi.py b/Control/CalypsoExample/Digitization/scripts/faser_digi.py
index 2405432c..d20e89ab 100755
--- a/Control/CalypsoExample/Digitization/scripts/faser_digi.py
+++ b/Control/CalypsoExample/Digitization/scripts/faser_digi.py
@@ -25,8 +25,10 @@ parser.add_argument("-g", "--geom", default="TI12MC",
                     help="Specify geometry (default: TI12MC, alt: TestBeamMC)")
 parser.add_argument("-t", "--tag", default="",
                     help="Specify digi tag (to append to output filename)")
-parser.add_argument("--highCaloGain", action='store_true',
-                    help="Use high gain settings for calo PMTs")
+parser.add_argument("--digiTag", default="",
+                    help="Specify tag for waveform digi folder")
+parser.add_argument("--short", default="",
+                    help="Extend short description")
 parser.add_argument("-n", "--nevts", type=int, default=-1,
                     help="Specify number of events to process (default: all)")
 parser.add_argument("-v", "--verbose", action='store_true', 
@@ -93,6 +95,39 @@ filestem = filepath.stem
 if filestem[-5:] == "-HITS":
     filestem = filestem[:-5]
 
+if len(args.short) > 0:
+    words = []
+    numbers = []
+    first = True
+
+    print(f"Modifying {filestem} to ", end='')
+
+    # Split by - and separate out the run/segment numbers from everything else
+    tokens = filestem.split('-')
+    for word in reversed(tokens):
+        if first and word.isnumeric():
+            numbers.insert(0, word)
+        else:
+            first = False
+            words.insert(0, word)
+
+    # Now, build it back together, adding the args.short extension
+    filestem = ""
+    for word in words:
+        filestem += f"{word}-"
+
+    # Tack on the extension
+    filestem = filestem[:-1]
+    filestem += f"{args.short}-"
+
+    # Now add the numbers
+    for word in numbers:
+        filestem += f"{word}-"
+
+    filestem = filestem[:-1]
+
+    print(filestem)
+
 if len(args.tag) > 0:
     print(f"{args.tag} in {filestem}?")
     if args.tag in filestem:
@@ -128,7 +163,7 @@ acc.merge(FaserSCT_DigitizationCfg(ConfigFlags))
 
 # Pass something to set folder tag
 from CaloDigiAlgs.CaloDigiAlgsConfig import CaloWaveformDigitizationCfg
-acc.merge(CaloWaveformDigitizationCfg(ConfigFlags))
+acc.merge(CaloWaveformDigitizationCfg(ConfigFlags, digiTag=args.digiTag))
 
 from ScintDigiAlgs.ScintDigiAlgsConfig import ScintWaveformDigitizationCfg
 acc.merge(ScintWaveformDigitizationCfg(ConfigFlags))
diff --git a/Control/CalypsoExample/Digitization/scripts/faser_digi_merge.py b/Control/CalypsoExample/Digitization/scripts/faser_digi_merge.py
index 1be2af2d..572148a1 100755
--- a/Control/CalypsoExample/Digitization/scripts/faser_digi_merge.py
+++ b/Control/CalypsoExample/Digitization/scripts/faser_digi_merge.py
@@ -31,8 +31,10 @@ parser.add_argument("-f", "--files", type=int, default=5,
                     help="Specify number of input files to run in one batch")
 parser.add_argument("-t", "--tag", default="",
                     help="Specify digi tag (to append to output filename)")
-parser.add_argument("--highCaloGain", action='store_true',
-                    help="Use high gain settings for calo PMTs")
+parser.add_argument("--digiTag", default="",
+                    help="Specify tag for waveform digi folder")
+parser.add_argument("--short", default="",
+                    help="Extend short description")
 parser.add_argument("-n", "--nevts", type=int, default=-1,
                     help="Specify number of events to process (default: all)")
 parser.add_argument("-v", "--verbose", action='store_true', 
@@ -108,6 +110,10 @@ stem = filelist[-1].stem
 spl = stem.split('-')
 seghi = int(spl[3])
 
+# append to short name?
+if len(args.short) > 0:
+    short += args.short
+
 # Build output filename
 if seglo == 0 and (seghi+1) == len(dirlist):  # Full run
     outfile = f"FaserMC-{short}-{run}"
@@ -209,12 +215,12 @@ acc.merge(FaserGeometryCfg(ConfigFlags))
 from FaserSCT_Digitization.FaserSCT_DigitizationConfigNew import FaserSCT_DigitizationCfg
 acc.merge(FaserSCT_DigitizationCfg(ConfigFlags))
 
+# Pass something to set folder tag
+if len(args.digiTag) > 0:
+    print(f"Using digiTag: {args.digiTag}")
+
 from CaloDigiAlgs.CaloDigiAlgsConfig import CaloWaveformDigitizationCfg
-if args.highCaloGain:
-    calo_norm = 25.
-else:
-    calo_norm =  5.
-acc.merge(CaloWaveformDigitizationCfg(ConfigFlags, CB_norm=calo_norm))
+acc.merge(CaloWaveformDigitizationCfg(ConfigFlags, digiTag=args.digiTag))
 
 from ScintDigiAlgs.ScintDigiAlgsConfig import ScintWaveformDigitizationCfg
 acc.merge(ScintWaveformDigitizationCfg(ConfigFlags))
diff --git a/Control/CalypsoExample/Reconstruction/scripts/faser_reco.py b/Control/CalypsoExample/Reconstruction/scripts/faser_reco.py
index 72235c1f..0d061148 100755
--- a/Control/CalypsoExample/Reconstruction/scripts/faser_reco.py
+++ b/Control/CalypsoExample/Reconstruction/scripts/faser_reco.py
@@ -133,7 +133,8 @@ elif runtype == "TI12Data03":
     # Use the updated field map
     ConfigFlags.IOVDb.GlobalTag = "OFLCOND-FASER-03"
     useCal = True
-    useLHC = True
+    if not args.isMC:
+        useLHC = True
 
 else:
     print("Invalid run type found:", runtype)
@@ -197,6 +198,7 @@ acc.merge(WaveformReconstructionCfg(ConfigFlags))
 if args.isMC:
     # Not ready for MC quite yet
     pass
+
 elif useCal:
     from CaloRecAlgs.CaloRecAlgsConfig import CalorimeterReconstructionCfg
     acc.merge(CalorimeterReconstructionCfg(ConfigFlags))
-- 
GitLab