71def main(filename, **args):
72 logging.debug("initializing xAOD")
73 if (not ROOT.xAOD.Init().isSuccess()):
74 raise IOError("Failed xAOD.Init()")
75
76
77 tree = None
78 if ".txt" in filename:
79 logging.debug("filename is a list of files")
80 chain = ROOT.TChain(args['tree_name'])
81 for line in islice(open(filename), 10):
82 logging.debug("adding %s", line.strip())
83 chain.Add(line.strip())
84 tree = ROOT.xAOD.MakeTransientTree(chain)
85 else:
86 logging.debug("opening file %s", filename)
87 f = ROOT.TFile.Open(filename)
88 if not f:
89 logging.error("problem opening file %s", filename)
90 tree = ROOT.xAOD.MakeTransientTree(f, args['tree_name'])
91 if not tree:
92 logging.warning("cannot find tree in the file")
93 f.Print()
94 return
95
96 logging.info("input has %d entries", tree.GetEntries())
97
98 logging.debug("initializing tool")
99 tool = ROOT.CP.EgammaCalibrationAndSmearingTool("tool")
100 tool.setProperty("ESModel", args["esmodel"]).ignore()
101 tool.setProperty("decorrelationModel", args["decorrelation"]).ignore()
102 if args["no_smearing"]:
103 tool.setProperty("int")("doSmearing", 0).ignore()
104 if args['debug']:
105 tool.msg().setLevel(0)
106 if args['use_afii'] is not None:
107 tool.setProperty("bool")("useFastSim", bool(args['use_afii'])).ignore()
108
109 tool.initialize()
110
111 if args['variation'] is not None:
112 logging.info("applying systematic variation %s", args['variation'])
113 sys_set = ROOT.CP.SystematicSet()
114 sys_set.insert(ROOT.CP.SystematicVariation(args['variation'], args['variation_sigma']))
115 tool.applySystematicVariation(sys_set)
116
117 logging.debug("creating output tree")
118 fout = ROOT.TFile("output.root", "recreate")
119 tree_out = ROOT.TNtuple(args["tree_name"], args["tree_name"], "eventNumber:eta:phi:true_e:pdgId:e:xAOD_e:raw_e:raw_ps")
120
121 logging.debug("looping on electron container")
122
123 def newevent_function():
124 logging.debug("executing new event function")
125 tool.beginInputFile()
126 tool.beginEvent()
127
128 def endevent_function():
129 logging.debug("executing end event function")
130 tool.endInputFile()
131
132 generator = xAOD_photon_generator(tree, newevent_function = newevent_function,
133 endevent_function = endevent_function,
134 min_pt=args['min_pt'],
135 min_abseta=args['min_abseta'], max_abseta=args['max_abseta'],
136 event_numbers=args['event_number'])
137
138 _ = ROOT.xAOD.TruthParticle_v1
139 get_truth_particle = ROOT.xAOD.TruthHelpers.getTruthParticle
140
141 for particle in islice(generator, None, args['nparticles']):
142 ei = tree.EventInfo
143 logging.debug(" === new photon eventNumber|eta|phi|e| = |%d|%.2f|%.2f|%.2f|", ei.eventNumber(), particle.eta(), particle.phi(), particle.e())
144 if not particle.caloCluster():
145 logging.warning("particle has no calo cluster")
146 continue
147 logging.debug(" |rawe0|raweAcc|TileGap3| = |%.2f|%.2f|%.2f|",
148 particle.caloCluster().energyBE(0),
149 particle.caloCluster().energyBE(1) + particle.caloCluster().energyBE(2) + particle.caloCluster().energyBE(3),
150 particle.caloCluster().eSample(17))
151 xAOD_energy = particle.e()
152 cluster = particle.caloCluster()
153 raw_e = cluster.energyBE(1) + cluster.energyBE(2) + cluster.energyBE(3)
154 raw_ps = cluster.energyBE(0)
155 true_particle = get_truth_particle(particle)
156 true_e = true_particle.e() if true_particle else 0
157 pdgId = true_particle.pdgId() if true_particle else 0
158 calibrated_energy = tool.getEnergy(particle)
159 tree_out.Fill(ei.eventNumber(), cluster.eta(), cluster.phi(),
160 true_e, pdgId, calibrated_energy, xAOD_energy, raw_e, raw_ps)
161 if math.isnan(calibrated_energy) or math.isnan(calibrated_energy) or calibrated_energy < 1:
162 print(
"==>", particle.author(), particle.eta(), particle.phi(), xAOD_energy, calibrated_energy)
163
164
165 logging.info("%d events written", tree_out.GetEntries())
166
167 tree_out.Write()
168 fout.Close()
169
void print(char *figname, TCanvas *c1)