SND@LHC Software
Loading...
Searching...
No Matches
run_MCEventBuilder.py
Go to the documentation of this file.
1import os
2import ROOT
3from ROOT import TObjString
4import time
5from argparse import ArgumentParser
6import SndlhcGeo
7import shipunit as u
8
9# ------------ Argument parser ----------------
10parser = ArgumentParser()
11parser.add_argument("-f", "--inputFile", help="Input file")
12parser.add_argument("-g", "--geoFile", help="geo file")
13parser.add_argument("-o", "--outputFile", help="Output file")
14parser.add_argument("-i", "--firstEvent", type=int, default=0, help="First event to process")
15parser.add_argument("-n", "--nEvents", type=int, default=0, help="Number of events to process (0 = all)")
16parser.add_argument("--saveFirst25nsOnly", action="store_true", help="Only store the first 25-ns chunk of each event")
17options = parser.parse_args()
18
19# ------------ Geo setup ----------------
20geo = SndlhcGeo.GeoInterface(options.geoFile)
21lsOfGlobals = ROOT.gROOT.GetListOfGlobals()
22lsOfGlobals.Add(geo.modules['Scifi'])
23scifiDet = lsOfGlobals.FindObject('Scifi')
24scifiDet.SetConfPar("Scifi/signalSpeed", 15*u.cm/u.nanosecond)
25lsOfGlobals.Add(geo.modules['MuFilter'])
26
27
28#-----------Executioner--------------
29start = time.time()
30inRootTFile = ROOT.TFile(options.inputFile)
31print(f"Input file: {options.inputFile}")
32
33# Use FairRoot framework to arrange the workflow
34# A FairRun is a wrapper of a collection of tasks
35run = ROOT.FairRunAna()
36
37# Input/output manager
38ioman = ROOT.FairRootManager.Instance()
39source = ROOT.FairFileSource(inRootTFile)
40run.SetSource(source)
41outFile = ROOT.TMemFile('dummy','CREATE') #IGNORE
42sink = ROOT.FairRootFileSink(outFile)
43run.SetSink(sink)
44ioman.InitSink()
45run.SetEventHeaderPersistence(False)
46
47#Avoiding some error messages
48xrdb = ROOT.FairRuntimeDb.instance()
49xrdb.getContainer("FairBaseParSet").setStatic()
50xrdb.getContainer("FairGeoParSet").setStatic()
51
52# Add tasks
53eventBuilder = ROOT.MCEventBuilder(options.outputFile, options.saveFirst25nsOnly)
54run.AddTask(eventBuilder)
55
56# Initialize and run
57run.Init()
58run.Run(options.firstEvent, options.firstEvent+options.nEvents)
59
60end = time.time()
61elapsed = end - start
62print(f"Elapsed time: {elapsed:.2f} seconds")