ATLAS Offline Software
runHistoBuilders.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 from __future__ import print_function
4 
5 # External dependencies
6 import ROOT
7 import sys
8 import re
9 import math
10 ROOT.gSystem.Load("libGenVector.so")
11 
12 
13 # Control switch
14 IsDebug = False
15 
16 
17 
18 
21 
22 treeNameJETMET = "qcd"
23 treeNameCOMMON = "physics"
24 writeBranchInformation = False
25 
26 if IsDebug:
27  fileNameJETMET="/afs/cern.ch/user/d/delsart/public/COMMON/NTUP_COMMON_ref.root"
28  treeNameJETMET="physics"
29  fileNameCOMMON="/afs/cern.ch/user/d/delsart/public/COMMON/NTUP_COMMON_new.root"
30 else:
31  if len(sys.argv) < 3:
32  print ("Too few arguments. Expected the following:")
33  print ("1. JETMET file (required)")
34  print ("2. COMMON file (required)")
35  print ("3. JETMET tree name (optional, default=\"qcd\")")
36  print ("4. COMMON tree name (optional, default=\"physics\")")
37  print ("5. Whether or not to write branch information to files (optional, default=False)")
38  exit(1)
39 
40  fileNameJETMET=sys.argv[1]
41  fileNameCOMMON=sys.argv[2]
42  if len(sys.argv) > 3:
43  treeNameJETMET = sys.argv[3]
44  if len(sys.argv) > 4:
45  treeNameCOMMON = sys.argv[4]
46  if len(sys.argv) > 5:
47  if "True" in sys.argv[5] or "true" in sys.argv[5]:
48  writeBranchInformation = True
49  elif "False" in sys.argv[5] or "false" in sys.argv[5]:
50  writeBranchInformation = False
51  else:
52  print ("Could not interpret the branch information control argument: ",sys.argv[5])
53  exit(2)
54 
55 
56 
59 
60 #fileNameJETMET="/afs/cern.ch/atlas/groups/JetEtmiss/COMMON_validation/JETMET/mc12_8TeV.147914.Pythia8_AU2CT10_jetjet_JZ4W.merge.JETMET.e1126_s1469_s1470_r3542_r3549_r17_2_7_5_3.000212.root"
61 #fileNameCOMMON="/afs/cern.ch/atlas/groups/JetEtmiss/COMMON_validation/COMMON/test162_mc12.aod.100evt.physics.12_04_2013.root"
62 #fileNameJETMET="/afs/cern.ch/user/t/toshi/public/NTUP_COMMON/test182_mc12.aod.100evt.qcd.root"
63 #fileNameJETMET="/afs/cern.ch/user/d/delsart/public/COMMON/NTUP_COMMON_ref.root"
64 
65 #fileNameCOMMON="/afs/cern.ch/atlas/groups/JetEtmiss/COMMON_validation/COMMON/latesttest_mc12.aod.100evt.physics.root "
66 #fileNameCOMMON="/afs/cern.ch/user/t/toshi/public/NTUP_COMMON/test173_mc12.aod.100evt.physics.root"
67 #fileNameCOMMON="/afs/cern.ch/user/t/toshi/public/NTUP_COMMON/test182_mc12.aod.100evt.physics.root"
68 #fileNameCOMMON="/afs/cern.ch/user/d/delsart/public/COMMON/NTUP_COMMON_new.root"
69 
70 
71 fileJETMET = ROOT.TFile(fileNameJETMET,"READ")
72 fileCOMMON = ROOT.TFile(fileNameCOMMON,"READ")
73 
74 treeJETMET = fileJETMET.Get(treeNameJETMET)
75 if not isinstance(treeJETMET,ROOT.TTree) :
76  print ("JETMET file does not contain the specified tree. Is the tree name correct?")
77  print ("File: ",fileNameJETMET)
78  print ("Tree: ",treeNameJETMET)
79  exit(3)
80 
81 treeCOMMON = fileCOMMON.Get(treeNameCOMMON)
82 if not isinstance(treeJETMET,ROOT.TTree):
83  print ("COMMON file does not contain the specified tree. Is the tree name correct?")
84  print ("File: ",fileNameCOMMON)
85  print ("Tree: ",treeNameCOMMON)
86  exit(4)
87 
88 
89 
90 
91 
94 from JetValidation.D3PDHistoBuildLib import MultipleHistoBuilderFromD3PD,JetType,JetVar
95 
96 
97 
100 
101 branchesJETMET = treeJETMET.GetSetOfTreeBranchNames()
102 branchesCOMMON = treeCOMMON.GetSetOfTreeBranchNames()
103 branchesBOTH = frozenset.intersection(branchesJETMET,branchesCOMMON)
104 brNotInCOMMON = branchesJETMET-branchesCOMMON
105 brNotInJETMET = branchesCOMMON-branchesJETMET
106 
107 if writeBranchInformation:
108  outFile = open('Branches_JETMET.txt','w')
109  outFile.write(str(sorted(branchesJETMET)))
110  outFile.close()
111 
112  outFile = open('Branches_COMMON.txt','w')
113  outFile.write(str(sorted(branchesCOMMON)))
114  outFile.close()
115 
116  outFile = open('Branches_BOTH.txt','w')
117  outFile.write(str(sorted(branchesBOTH)))
118  outFile.close()
119 
120  outFile = open('Branches_NotIn_COMMON.txt','w')
121  outFile.write(str(sorted(brNotInCOMMON)))
122  outFile.close()
123 
124  outFile = open('Branches_NotIn_JETMET.txt','w')
125  outFile.write(str(sorted(brNotInJETMET)))
126  outFile.close()
127 
128  # Write out in twiki-syntax
129  outFile = open('Twiki_Branches_NotIn_COMMON.txt','w')
130  outFile.write('| *Branch name* | *Expected* | *Confirmed by* ||\n')
131  for aBranch in sorted(brNotInCOMMON):
132  outFile.write('| !%s | ? | ? ||\n'%(aBranch))
133  outFile.close()
134 
135 
136 
139 
140 if writeBranchInformation:
141  jetTypesJETMET = set([])
142  jetVarsJETMET = set([])
143  for name in branchesJETMET:
144  if "jet_" in name:
145  type = JetType(name)
146  if type is not None:
147  jetTypesJETMET.add(type)
148  jetVarsJETMET.add(JetVar(name))
149 
150  jetTypesCOMMON = set([])
151  jetVarsCOMMON = set([])
152  for name in branchesCOMMON:
153  if "jet_" in name:
154  type = JetType(name)
155  if type is not None:
156  jetTypesCOMMON.add(type)
157  jetVarsCOMMON.add(JetVar(name))
158 
159  jetTypesBoth = set.intersection(jetTypesJETMET,jetTypesCOMMON)
160  jetVarsBoth = set.intersection(jetVarsJETMET,jetVarsCOMMON)
161 
162  # For each jet type, ensure that every variable is included
163  branchesNotExisting = []
164  for aType in jetTypesBoth:
165  for aVar in jetVarsBoth:
166  toCheck = aType + "_" + aVar
167  if toCheck not in branchesBOTH:
168  branchesNotExisting.append(toCheck)
169 
170  outFile = open('Branches_JetTypes_NotExisting.txt','w')
171  outFile.write(str(sorted(branchesNotExisting)))
172  outFile.close()
173 
174 
175 
178 
179 multiBuilder = MultipleHistoBuilderFromD3PD()
180 multiBuilder.addTrees(treeJETMET,treeCOMMON)
181 multiBuilder.createBuildersFromTrees()
182 
183 
184 #multiBuilder.addHistosWithDefaultSelectors(["jet_AntiKt4LCTopo_constscale_pt"], "jet_AntiKt4LCTopo_pt")
185 #multiBuilder.addHistosWithDefaultSelectors(["jet_AntiKt4LCTopo_constscale_pt",100,0,1000e3], "jet_AntiKt4LCTopo_pt")
186 #for aBranch in multiBuilder.getBranchIntersection() :
187 # if "jet_AntiKt4LC" in aBranch :
188 # multiBuilder.addHistosWithDefaultSelectors(aBranch)
189 # multiBuilder.addHistosWithHighPtDetscaleSelectors(aBranch)
190 
191 #multiBuilder.addAllHistosInBranchIntersection()
192 #multiBuilder.addAllLeadingJetHistosInBranchIntersection()
193 #multiBuilder.addHistosWithDefaultSelectors(*multiBuilder.getBranchIntersection())
194 #multiBuilder.addHistosWithHighPtDetscaleSelectors(*multiBuilder.getBranchIntersection())
195 
196 for aBranch in multiBuilder.getBranchIntersection() :
197 #for aBranch in ["jet_AntiKt4LCTopo_emscale_pt","jet_AntiKt4LCTopo_eta"] :
198  if "EventNumber" in aBranch:
199  multiBuilder.addHistos(aBranch)
200  if "jet_AntiKt4" in aBranch and ("LC" in aBranch or "EM" in aBranch) :
201  multiBuilder.addHistosWithLeadingJetSelectors(JetType(aBranch), aBranch)
202 
203 print ("\nRunning event loop...")
204 multiBuilder.eventLoop()
205 print ("Done event loop!")
206 
207 #multiBuilder.draw(0)
208 #multiBuilder.canvas.Print("test.pdf")
209 twikiLog = open('Twiki_ToleranceResults.log','w')
210 twikiLog.write('| *Variable* | *Passes tolerance* | *Num failed/total bins* | *Largest difference (%)* ||\n')
211 multiBuilder.writeAllHistoToFile("test.pdf",logFile="failsTolerance.log",twikiLogFile=twikiLog)
212 twikiLog.close()
213 
214 multiBuilder.close()
215 fileJETMET.Close()
216 fileCOMMON.Close()
217 
218 
219 
220 
JetVar
Definition: JetSelectorAttribute.h:15
calibdata.exit
exit
Definition: calibdata.py:236
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:224
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11