SND@LHC Software
Loading...
Searching...
No Matches
run_muonRecoSND.py
Go to the documentation of this file.
1import ROOT
2from argparse import ArgumentParser
3import os
4
5import SndlhcMuonReco
6
7parser = ArgumentParser()
8parser.add_argument("-f", "--inputFile", dest="inputFile", help="single input file", required=True)
9parser.add_argument("-g", "--geoFile", dest="geoFile", help="geofile", required=False)
10parser.add_argument("-o", "--withOutput", dest="withOutput", help="persistent output", action='store_true',default=False)
11parser.add_argument("-s", "--saveTo", dest="outPath", help="output storage path", type=str,default="",required=False)
12parser.add_argument("-par", "--parFile", dest="parFile", help="parameter file", required=False, default=os.environ['SNDSW_ROOT']+"/python/TrackingParams.xml")
13parser.add_argument("-c", "--case", dest="trackingCase", help="type of tracks to build. Should match the 'tracking_case' name in parFile, use quotes", required=True)
14parser.add_argument("-hf", "--HoughSpaceFormat", dest="HspaceFormat", help="Hough space representation. Should match the 'Hough_space_format' name in parFile, use quotes", required=True)
15parser.add_argument("-n", "--nEvents", dest="nEvents", type=int, help="number of events to process", default=1100000)
16parser.add_argument("-i", "--firstEvent",dest="firstEvent", help="First event of input file to use", required=False, default=0, type=int)
17parser.add_argument("-sc", "--scale",dest="scaleFactor", help="Run reconstruction once for a randomly selected event in every [scaleFactor] events.", required=False, default=1, type=int)
18
19options = parser.parse_args()
20
21x = options.inputFile
22filename = x[x.rfind('/')+1:]
23if x.rfind('/run_')>0:
24 runN = x[x.rfind('/run_')+5:x.rfind('/run_')+11]
25 path = x[:x.rfind('/run_')+1]
26else:
27 runN = ""
28 path = x[:x.rfind('/')+1]
29outFileName = options.outPath+filename.replace('.root','_'+runN+'_muonReco.root')
30
31# Set the geometry file
32import SndlhcGeo
33
34if not options.geoFile:
35 if runN == "" :
36 print('\033[91m'+'Error!'+'\033[0m'+' No run number detected. Must provide a geo file then!')
37 exit()
38 if options.inputFile.find('TI18')<0:
39 if options.inputFile.find('physics/2022')>=0: options.geoFile = path+"geofile_sndlhc_TI18_V0_2022.root"
40 else:
41 if int(runN) < 4575:
42 options.geoFile = "geofile_sndlhc_TI18_V3_08August2022.root"
43 elif int(runN) < 4855:
44 options.geoFile = "geofile_sndlhc_TI18_V5_14August2022.root"
45 elif int(runN) < 5172:
46 options.geoFile = "geofile_sndlhc_TI18_V6_08October2022.root"
47 else:
48 options.geoFile = "geofile_sndlhc_TI18_V7_22November2022.root"
49 # Introducing SNDLHCEventHeader in 2022 run 4791, so there will be tag!='' in the detector classes
50 # and one has to use the */physics/2022 geofiles
51 if int(runN) >= 4791 and int(runN) <= 5429: path = "/eos/experiment/sndlhc/convertedData/physics/2022/"
52 options.geoFile = path + options.geoFile
53
54# Check geo file is a match for the data
55if (options.inputFile.find('/TI18')>=0 and options.geoFile.find('V0_2022')>=0 ) or \
56 (options.inputFile.find('physics/2022')>=0 and options.geoFile.find('/TI18')>=0 ) or \
57 (options.inputFile.find('/TI18')>=0 and options.geoFile.find('/TI18')>=0 and int(runN) >= 4791 and int(runN) <= 5429 ):
58 print('\033[91m'+'Error!'+'\033[0m'+' Consider a different geo file for that input file! Exitting..')
59 exit()
60
61geo = SndlhcGeo.GeoInterface(options.geoFile)
62lsOfGlobals = ROOT.gROOT.GetListOfGlobals()
63lsOfGlobals.Add(geo.modules['Scifi'])
64lsOfGlobals.Add(geo.modules['MuFilter'])
65
66fullPath = options.inputFile
67if options.inputFile.find('/eos/experiment')==0:
68 fullPath = os.environ['EOSSHIP']+options.inputFile
69F = ROOT.TFile.Open(fullPath)
70
71if options.withOutput:
72 outFile = ROOT.TFile(outFileName, 'RECREATE')
73else:
74 outFile = ROOT.TMemFile(outFileName,'CREATE')
75
76treename = None
77for test_treename in ["rawConv", "cbmsim"] :
78 if hasattr(F, test_treename) :
79 treename = test_treename
80 break
81
82if treename is None :
83 raise RuntimeError("File {0} contains no object with a valid SND@LHC TTree name".format(fullPath))
84
85fairRootManager = ROOT.FairRootManager.Instance()
86fairRootManager.SetTreeName(treename)
87
88run = ROOT.FairRunAna()
89print("Initialized FairRunAna")
90
91source = ROOT.FairFileSource(F)
92run.SetSource(source)
93
94sink = ROOT.FairRootFileSink(outFile)
95run.SetSink(sink)
96fairRootManager.InitSink()
97# Don't use FairRoot's default event header settings
98run.SetEventHeaderPersistence(False)
99
100muon_reco_task = SndlhcMuonReco.MuonReco()
101run.AddTask(muon_reco_task)
102
103# Start timer
104w = ROOT.TStopwatch()
105
106# Set the parameter file - must be called before Init()
107muon_reco_task.SetParFile(options.parFile)
108muon_reco_task.SetTrackingCase(options.trackingCase)
109muon_reco_task.SetHoughSpaceFormat(options.HspaceFormat)
110# setting a flag to let the task know which manager called it
111# needed as output handling differs for this manager and run_TrackSelections.
112muon_reco_task.SetStandalone()
113run.Init()
114
115# Set the scale factor - must be after Init()
116print("Setting a scale factor to:", options.scaleFactor)
117muon_reco_task.SetScaleFactor(options.scaleFactor)
118
119# Set number of events to process
120nEvents = min( options.nEvents, source.GetEntries())
121
122run.Run(options.firstEvent, options.firstEvent + nEvents)
123w.Stop()
124print('Real Time:', w.RealTime(), ' CPU time: ', w.CpuTime())
125print("Done running")