FairShip
Loading...
Searching...
No Matches
ShipReco.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# SPDX-License-Identifier: LGPL-3.0-or-later
3# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
4
5from argparse import ArgumentParser
6
7withHists = True
8pidProton = False # if true, take truth, if False fake with pion mass
9
10import resource
11
12
13def mem_monitor() -> None:
14 # Getting virtual memory size
15 pid = os.getpid()
16 with open(os.path.join("/proc", str(pid), "status")) as f:
17 lines = f.readlines()
18 _vmsize = [line for line in lines if line.startswith("VmSize")][0]
19 vmsize = int(_vmsize.split()[1])
20 # Getting physical memory size
21 pmsize = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
22 print(f"memory: virtual = {vmsize / 1.0e3:5.2F} MB physical = {pmsize / 1.0e3:5.2F} MB")
23
24
25import os
26
27import global_variables
28import ROOT
29import rootUtils as ut
30import shipRoot_conf
31
33
34parser = ArgumentParser()
35
36parser.add_argument("-f", "--inputFile", dest="inputFile", help="Input file", required=True)
37parser.add_argument(
38 "-n", "--nEvents", dest="nEvents", help="Number of events to reconstruct", required=False, default=999999, type=int
39)
40parser.add_argument("-g", "--geoFile", dest="geoFile", help="ROOT geofile", required=True)
41parser.add_argument(
42 "--noVertexing", dest="noVertexing", help="switch off vertexing", required=False, action="store_true"
43)
44parser.add_argument(
45 "--noStrawSmearing",
46 dest="withNoStrawSmearing",
47 help="no smearing of distance to wire, default on",
48 required=False,
49 action="store_true",
50)
51parser.add_argument(
52 "--withT0", dest="withT0", help="simulate arbitrary T0 and correct for it", required=False, action="store_true"
53)
54parser.add_argument(
55 "-i",
56 "--firstEvent",
57 dest="firstEvent",
58 help="First event of input file to use",
59 required=False,
60 default=0,
61 type=int,
62)
63parser.add_argument(
64 "--realPR",
65 dest="realPR",
66 help="Option for pattern recognition without MC truth. \n\
67 FH : Hough transform.\n\
68 AR : Artificial retina.\n\
69 TemplateMatching : Tracks are searched for based on the template: track seed + hits within a window around the seed.",
70 required=False,
71 choices=["FH", "AR", "TemplateMatching"],
72 default="",
73)
74parser.add_argument("-dy", dest="dy", help="Max height of tank", required=False, default=None, type=int)
75parser.add_argument("--Debug", dest="Debug", help="Switch on debugging", required=False, action="store_true")
76
77options = parser.parse_args()
78vertexing = not options.noVertexing
79
80
81# need to figure out which geometry was used, only needed if no geo file
82if not options.dy:
83 # try to extract from input file name
84 tmp = options.inputFile.split(".")
85 try:
86 dy = float(tmp[1] + "." + tmp[2])
87 except (ValueError, IndexError):
88 dy = None
89
90print(
91 "configured to process ",
92 options.nEvents,
93 " events from ",
94 options.inputFile,
95 " starting with event ",
96 options.firstEvent,
97 " with option Yheight = ",
98 dy,
99 " with vertexing",
100 vertexing,
101 " and real pattern reco ",
102 options.realPR,
103)
104# Determine output filename (will contain only digi/reco branches)
105if not options.inputFile.find("_rec.root") < 0:
106 outFile = options.inputFile
107 options.inputFile = outFile.replace("_rec.root", ".root")
108else:
109 outFile = options.inputFile.replace(".root", "_rec.root")
110 # outfile should be in local directory
111 tmp = outFile.split("/")
112 outFile = tmp[len(tmp) - 1]
113
114if not options.geoFile:
115 tmp = options.inputFile.replace("ship.", "geofile_full.")
116 options.geoFile = tmp.replace("_rec", "")
117
118fgeo = ROOT.TFile.Open(options.geoFile)
119geoMat = ROOT.genfit.TGeoMaterialInterface() # if only called in ShipDigiReco -> crash, reason unknown
120
121from ShipGeoConfig import load_from_root_file
122
123# load Shipgeo dictionary
124ShipGeo = load_from_root_file(fgeo, "ShipGeo")
125
126h = {}
127if withHists:
128 ut.bookHist(h, "distu", "distance to wire", 100, 0.0, 5.0)
129 ut.bookHist(h, "distv", "distance to wire", 100, 0.0, 5.0)
130 ut.bookHist(h, "disty", "distance to wire", 100, 0.0, 5.0)
131 ut.bookHist(h, "nmeas", "nr measurements", 100, 0.0, 50.0)
132 ut.bookHist(h, "chi2", "Chi2/DOF", 100, 0.0, 20.0)
133
134import shipDet_conf
135
136run = ROOT.FairRunSim()
137run.SetName("TGeant4") # Transport engine
138run.SetSink(ROOT.FairRootFileSink(ROOT.TMemFile("output", "recreate"))) # Dummy output file
139run.SetUserConfig("g4Config_basic.C") # geant4 transport not used, only needed for creating VMC field
140rtdb = run.GetRuntimeDb()
141# -----Create geometry----------------------------------------------
142modules = shipDet_conf.configure(run, ShipGeo)
143# run.Init()
144fgeo["FAIRGeom"]
145import geomGeant4
146
147if hasattr(ShipGeo.Bfield, "fieldMap"):
148 fieldMaker = geomGeant4.addVMCFields(ShipGeo, "", True, withVirtualMC=False)
149
150# make global variables
151global_variables.debug = options.Debug
152global_variables.fieldMaker = fieldMaker
153global_variables.pidProton = pidProton
154global_variables.withT0 = options.withT0
155global_variables.realPR = options.realPR
156global_variables.vertexing = vertexing
157global_variables.ShipGeo = ShipGeo
158global_variables.modules = modules
159global_variables.withNoStrawSmearing = options.withNoStrawSmearing
160global_variables.h = h
161global_variables.iEvent = 0
162
163# import reco tasks
164import shipDigiReco
165
166SHiP = shipDigiReco.ShipDigiReco(options.inputFile, outFile, fgeo)
167options.nEvents = min(SHiP.sTree.GetEntries(), options.nEvents)
168# main loop
169for global_variables.iEvent in range(options.firstEvent, options.nEvents):
170 if global_variables.iEvent % 1000 == 0 or global_variables.debug:
171 print("event ", global_variables.iEvent)
172 rc = SHiP.sTree.GetEvent(global_variables.iEvent)
173 SHiP.digitize()
174 SHiP.reconstruct()
175 SHiP.recoTree.Fill()
176# memory monitoring
177# mem_monitor()
178# end loop over events
179SHiP.finish()
None mem_monitor()
Definition: ShipReco.py:13
def addVMCFields(shipGeo, str controlFile="", bool verbose=False, bool withVirtualMC=True)
Definition: geomGeant4.py:165
def configure(run, ship_geo)
None configure(darkphoton=None)
int open(const char *, int)
Opens a file descriptor.