From e2a209defbfd919b21f32d8325cb99d35af78552 Mon Sep 17 00:00:00 2001
From: FASER Reco <faserrec@lxplus745.cern.ch>
Date: Fri, 23 Dec 2022 01:45:36 +0100
Subject: [PATCH] Fix ntuple script

---
 .../NtupleDumper/scripts/analyzeRun.py        | 72 ++++++++++++-------
 1 file changed, 45 insertions(+), 27 deletions(-)

diff --git a/PhysicsAnalysis/NtupleDumper/scripts/analyzeRun.py b/PhysicsAnalysis/NtupleDumper/scripts/analyzeRun.py
index 8a4785761..eaca9c5b1 100755
--- a/PhysicsAnalysis/NtupleDumper/scripts/analyzeRun.py
+++ b/PhysicsAnalysis/NtupleDumper/scripts/analyzeRun.py
@@ -8,6 +8,10 @@ from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
 from AthenaConfiguration.ComponentFactory import CompFactory
 from MagFieldServices.MagFieldServicesConfig import MagneticFieldSvcCfg
 
+from FaserActsGeometry.ActsGeometryConfig import ActsTrackingGeometryToolCfg
+# from FaserActsGeometry.ActsGeometryConfig import ActsTrackingGeometrySvcCfg
+# from FaserActsGeometry.ActsGeometryConfig import ActsExtrapolationToolCfg
+
 
 def NtupleDumperAlgCfg(flags, OutName, **kwargs):
     # Initialize GeoModel
@@ -15,12 +19,15 @@ def NtupleDumperAlgCfg(flags, OutName, **kwargs):
     acc = FaserGeometryCfg(flags)
 
     acc.merge(MagneticFieldSvcCfg(flags))
-    # acc.merge(FaserActsTrackingGeometrySvcCfg(flags))
-    # acc.merge(FaserActsAlignmentCondAlgCfg(flags))
+    #acc.merge(ActsTrackingGeometrySvcCfg(flags))
+    #acc.merge(FaserActsAlignmentCondAlgCfg(flags))
+
+    result, actsTrackingGeometryTool = ActsTrackingGeometryToolCfg(flags)
+    acc.merge(result)
 
     actsExtrapolationTool = CompFactory.FaserActsExtrapolationTool("FaserActsExtrapolationTool")
     actsExtrapolationTool.MaxSteps = 10000
-    actsExtrapolationTool.TrackingGeometryTool = CompFactory.FaserActsTrackingGeometryTool("TrackingGeometryTool")
+    actsExtrapolationTool.TrackingGeometryTool = actsTrackingGeometryTool 
 
     NtupleDumperAlg = CompFactory.NtupleDumperAlg("NtupleDumperAlg",**kwargs)
     NtupleDumperAlg.ExtrapolationTool = actsExtrapolationTool
@@ -56,30 +63,41 @@ if __name__ == "__main__":
     Configurable.configurableRun3Behavior = True
 
     dataDir=f"/eos/experiment/faser/rec/2022/{ptag}/{runno:06d}"
-    if inputIsMC: dataDir=f"/eos/experiment/faser/sim/mdc/foresee/{ui.runnum}/rec/r0009/"
+
     files=sorted(glob.glob(f"{dataDir}/Faser*"))
-    if num==-1: num=len(files)
-    fileListInitial=files[0:num]#[num*filesPerJob:(num+1)*filesPerJob]
+    if filesPerJob <= 0: filesPerJob = len(files)
+
+    start = num*filesPerJob
+    end = (num+1)*filesPerJob
+    if end > len(files):
+        end = len(files)
+    fileListInitial=files[start:end]
     fileList=[]
     for fName in fileListInitial:
+        #if fName[:4] == '/eos':
+        #    fName = f'root:/{fName}'
+
         try:
             fh=ROOT.TFile(fName)
             fileList.append(fName)
         except OSError:
             print("Warning bad file: ",fName)
 
-    log.info(f"Analyzing Run {runno} files {0} to {num} (num={num})")
+    log.info(f"Analyzing Run {runno} files {start} to {end} (num={num})")
     log.info(f"Got {len(fileList)} files out of {len(fileListInitial)}")
 
-    outName=f"Data-tuple-{runno:06d}-{num:05d}-{filesPerJob}.root"
+    if start == 0 and end == len(files):
+        outName=f"Faser-Physics-{runno:06d}-{ptag}-PHYS.root"
+    else:
+        outName=f"Faser-Physics-{runno:06d}-{start:05d}-{(end-1):05d}-{ptag}-PHYS.root"
 
     # Configure
     ConfigFlags.Input.Files = fileList
     ConfigFlags.IOVDb.GlobalTag = "OFLCOND-FASER-03"             # Always needed; must match FaserVersionS
     ConfigFlags.IOVDb.DatabaseInstance = "OFLP200"               # Use MC conditions for now
     ConfigFlags.Input.ProjectName = "data21"                     # Needed to bypass autoconfig
-    ConfigFlags.Input.isMC = inputIsMC                                # Needed to bypass autoconfig
-    ConfigFlags.GeoModel.FaserVersion     = "FASERNU-03"           # FASER geometry
+    ConfigFlags.Input.isMC = False                               # Needed to bypass autoconfig
+    ConfigFlags.GeoModel.FaserVersion   = "FASERNU-03"           # FASER geometry
     ConfigFlags.Common.isOnline = False
     ConfigFlags.GeoModel.Align.Dynamic = False
     ConfigFlags.Beam.NumberOfCollisions = 0.
@@ -93,36 +111,36 @@ if __name__ == "__main__":
     acc.merge(PoolReadCfg(ConfigFlags))
 
     # algorithm
-    acc.merge(NtupleDumperAlgCfg(ConfigFlags, outName, UseFlukaWeights=True))
+    acc.merge(NtupleDumperAlgCfg(ConfigFlags, outName))
 
     AthenaEventLoopMgr = CompFactory.AthenaEventLoopMgr()
     AthenaEventLoopMgr.EventPrintoutInterval=1000
     acc.addService(AthenaEventLoopMgr)
 
     # # Hack to avoid problem with our use of MC databases when isMC = False
-    if not inputIsMC:
-        replicaSvc = acc.getService("DBReplicaSvc")
-        replicaSvc.COOLSQLiteVetoPattern = ""
-        replicaSvc.UseCOOLSQLite = True
-        replicaSvc.UseCOOLFrontier = False
-        replicaSvc.UseGeomSQLite = True
+    replicaSvc = acc.getService("DBReplicaSvc")
+    replicaSvc.COOLSQLiteVetoPattern = ""
+    replicaSvc.UseCOOLSQLite = True
+    replicaSvc.UseCOOLFrontier = False
+    replicaSvc.UseGeomSQLite = True
 
     # Timing
     #acc.merge(MergeRecoTimingObjCfg(ConfigFlags))
 
     # Dump config
-    # logging.getLogger('forcomps').setLevel(VERBOSE)
-    # acc.foreach_component("*").OutputLevel = VERBOSE
-    # acc.foreach_component("*ClassID*").OutputLevel = INFO
-    # acc.getCondAlgo("FaserSCT_AlignCondAlg").OutputLevel = VERBOSE
-    # acc.getCondAlgo("FaserSCT_DetectorElementCondAlg").OutputLevel = VERBOSE
-    # acc.getService("StoreGateSvc").Dump = True
-    # acc.getService("ConditionStore").Dump = True
-    # acc.printConfig(withDetails=True)
-    # ConfigFlags.dump()
+    if False:
+        logging.getLogger('forcomps').setLevel(VERBOSE)
+        acc.foreach_component("*").OutputLevel = VERBOSE
+        acc.foreach_component("*ClassID*").OutputLevel = INFO
+        acc.getCondAlgo("FaserSCT_AlignCondAlg").OutputLevel = VERBOSE
+        acc.getCondAlgo("FaserSCT_DetectorElementCondAlg").OutputLevel = VERBOSE
+        acc.getService("StoreGateSvc").Dump = True
+        acc.getService("ConditionStore").Dump = True
+        acc.printConfig(withDetails=True)
+        ConfigFlags.dump()
 
     # Execute and finish
-    sc = acc.run(maxEvents=ui.events)
+    sc = acc.run(maxEvents=-1)
 
     # Success should be 0
     sys.exit(not sc.isSuccess())    
-- 
GitLab