ATLAS Offline Software
createDCubeRecoHistograms_withSel.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2 
3 # this script can be used to create DCube histograms from the output ntuples of NSWPRDValAlg
4 
5 import os, sys, ROOT, argparse
6 import math
7 from DCubeHistograms import MyHistoFiller
8 
9 if __name__ == "__main__":
10  parser = argparse.ArgumentParser(prog='createDCubeRecoHistograms', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
11  parser.add_argument('-i', '--inputFile', help='choose input ROOT file', default='NSWPRDValAlg.reco.ntuple.root', type=str)
12  parser.add_argument('-o', '--outputFile', help='choose output ROOT file', default='NSWPRDValAlg.reco.dcube.root', type=str)
13  parser.add_argument('--doCSC', help='turn on CSC if using Run4 input ROOT file', default=False, action='store_true')
14  parser.add_argument('--doTGC', help='turn on TGC', default=False, action='store_true')
15  parser.add_argument('--CSCsel', help='Choose eta_sector selections for CSC, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
16  parser.add_argument('--TGCsel', help='Choose eta_sector selections for TGC, e.g. positive_1 for positive eta and sector 1, None_None for no selection', default='None_None', type=str)
17 
18  Options = parser.parse_args()
19 
20  ROOT.gROOT.SetBatch(True)
21 
22  if not os.path.exists(Options.inputFile):
23  print ('ERROR: File %s does not exist'%Options.inputFile)
24  sys.exit(1)
25 
26  inputFile = ROOT.TFile(Options.inputFile, "READ")
27  if not inputFile:
28  print ('ERROR: Failed to open file %s'%Options.inputFile)
29  sys.exit(1)
30  inputTree = inputFile.Get("NSWValTree")
31  if not inputTree:
32  print ('ERROR: NSWValTree does not exist in file %s'%Options.inputFile)
33  sys.exit(1)
34 
35  nEntries = inputTree.GetEntries()
36  if nEntries==0:
37  print ('ERROR: NSWValTree of file %s has 0 entries'%Options.inputFile)
38  sys.exit(1)
39 
40  outputFile = ROOT.TFile(Options.outputFile, "RECREATE")
41  if not outputFile:
42  print ('ERROR: Failed to open file %s'%Options.outputFile)
43  sys.exit(1)
44 
45  outputFile.cd()
46  outputFile.mkdir("reconstruction/")
47  ODir = outputFile.GetDirectory("reconstruction/")
48  ODir.cd()
49 
50  CSCselections = Options.CSCsel.split("_")
51  CSC_eta = CSCselections[0]
52  if CSCselections[1] != "None":
53  CSC_sector = int (CSCselections[1])
54  else:
55  CSC_sector = CSCselections[1]
56 
57  TGCselections = Options.TGCsel.split("_")
58  TGC_eta = TGCselections[0]
59  if TGCselections[1] != "None":
60  TGC_sector = int (TGCselections[1])
61  else:
62  TGC_sector = TGCselections[1]
63 
64  #Filling
65  for i in range(inputTree.GetEntries()):
66  inputTree.GetEntry(i)
67  cscPRDHists = []
68  tgcPRDHists = []
69 
70  # CSCs
71  if Options.doCSC:
72  if CSC_eta == "positive":
73  csc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.PRD_CSC_stationEta[ncscPRD])) >= 0
74  elif CSC_eta == "negative":
75  csc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.PRD_CSC_stationEta[ncscPRD])) < 0
76  else:
77  csc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.PRD_CSC_stationEta[ncscPRD])) < 9
78 
79  if CSC_sector == "None":
80  csc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.PRD_CSC_stationPhi[ncscPRD])) < 10
81  else:
82  csc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.PRD_CSC_stationPhi[ncscPRD])) == CSC_sector
83 
84  for ncscPRD in range(0,len(inputTree.PRD_CSC_localPosX)):
85  cscPRDHists += [MyHistoFiller( chamber_name = "CSC_PRD", eta_sel = csc_eta_sel, sector_sel = csc_sector_sel )]
86  cscPRDHists[ncscPRD].fill(inputTree, ncscPRD)
87 
88 
89 
90  # TGCs
91  if Options.doTGC:
92  if TGC_eta == "positive":
93  tgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.PRD_TGC_stationEta[ntgcPRD])) >= 0
94  elif TGC_eta == "negative":
95  tgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.PRD_TGC_stationEta[ntgcPRD])) < 0
96  else:
97  tgc_eta_sel = lambda t: MyHistoFiller.Eta(ord(t.PRD_TGC_stationEta[ntgcPRD])) < 9
98 
99  if TGC_sector == "None":
100  tgc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.PRD_TGC_stationPhi[ntgcPRD])) < 51
101  else:
102  tgc_sector_sel = lambda s: MyHistoFiller.Eta(ord(s.PRD_TGC_stationPhi[ntgcPRD])) == TGC_sector
103 
104  for ntgcPRD in range(0,len(inputTree.PRD_TGC_localPosX)):
105  tgcPRDHists += [MyHistoFiller( chamber_name = "TGC_PRD", eta_sel = tgc_eta_sel, sector_sel = tgc_sector_sel )]
106  tgcPRDHists[ntgcPRD].fill(inputTree, ntgcPRD)
107 
108 
109  #Writing
110  if Options.doCSC:
111  cscPRDHist = MyHistoFiller( chamber_name = "CSC_PRD", eta_sel = None, sector_sel = None )
112  cscPRDHist.write(ODir)
113 
114  if Options.doTGC:
115  tgcPRDHist = MyHistoFiller( chamber_name = "TGC_PRD", eta_sel = None, sector_sel = None )
116  tgcPRDHist.write(ODir)
117 
DCubeHistograms.MyHistoFiller
Definition: DCubeHistograms.py:7
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
fill
void fill(H5::Group &out_file, size_t iterations)
Definition: test-hdf5-writer.cxx:95