ATLAS Offline Software
Loading...
Searching...
No Matches
LArCellNtuple.py
Go to the documentation of this file.
1#!/bin/env python
2# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3
4
9import os,sys,getopt
10
11# dumping class
12
13import cppyy
14from array import array
15
16import ROOT
17from ROOT import HWIdentifier, Identifier, IdentifierHash, TFile
18from AthenaPython import PyAthena
19
21 def __init__(self,ofile,isSC):
22 super(CellE,self).__init__()
23 self.fout = ROOT.TFile(ofile,"RECREATE")
24 self.isSC = isSC
25
26 def initialize (self):
27 self.msg.debug("Doing CellE init")
28 self.sg = PyAthena.py_svc('StoreGateSvc')
29 self.has_offID=False
30 self.is_init=False
31 return 1
32
33 def execute (self):
34 if self.detStore is None:
35 self.detStore = PyAthena.py_svc('DetectorStore')
36 if self.detStore is None:
37 print("Failed to get DetectorStore")
38 return 0
39 if not self.has_offID:
40 if not self.isSC:
41 self.offlineID = self.detStore['CaloCell_ID']
42 else:
43 self.offlineID = self.detStore['CaloCell_SuperCell_ID']
44 if self.offlineID is None:
45 print("Failed to get CaloCell_ID")
46 return 0
47 EI = self.sg['EventInfo']
48 if EI is None:
49 self.msg.warning("Failed to get EventInfo")
50 self.evtid=0
51 self.bcid=0
52 else:
53 self.evtid=EI.eventNumber()
54 self.bcid=EI.bcid()
55
56 if not self.is_init:
57 self.msg.debug("Doing CellE Ttree init")
58 self.maxcells=self.offlineID.calo_cell_hash_max()
59 self.amaxcells=array('i',[self.maxcells])
60 self.aevtid=array('L',[self.evtid])
61 self.abcid=array('I',[self.bcid])
62 self.idvec = array('i',self.maxcells*[0]) # storing the identifiers
63 self.calosamp = array('i',self.maxcells*[0]) # storing the calo sample
64 self.posneg = array('i',self.maxcells*[0]) # storing the side
65 self.region = array('i',self.maxcells*[0]) # storing the region
66 self.etaidx = array('i',self.maxcells*[0]) # storing the eta bin
67 self.phiidx = array('i',self.maxcells*[0]) # storing the phi bin
68 self.encell = array('d',self.maxcells*[0.]) # energies
69 self.timecell = array('d',self.maxcells*[0.]) # timing
70 self.provcell = array('H',self.maxcells*[0]) # provenance
71 self.qcell = array('H',self.maxcells*[0]) # quality
72 self.fout.cd()
73 self.nt = ROOT.TTree("mytree","mytree")
74 self.nt.SetDirectory(self.fout)
75 self.nt.Branch("ncell",self.amaxcells,"ncell/I")
76 self.nt.Branch("Event",self.aevtid,"Event/l")
77 self.nt.Branch("BCID",self.abcid,"BCID/I")
78 self.nt.Branch("OffID",self.idvec,"OffID[ncell]/i")
79 self.nt.Branch("calosamp",self.calosamp,"calosamp[ncell]/I")
80 self.nt.Branch("pos_neg",self.posneg,"pos_neg[ncell]/I")
81 self.nt.Branch("region",self.region,"region[ncell]/I")
82 self.nt.Branch("eta",self.etaidx,"eta[ncell]/I")
83 self.nt.Branch("phi",self.phiidx,"phi[ncell]/I")
84 self.nt.Branch("energy",self.encell,"energy[ncell]/D")
85 self.nt.Branch("time",self.timecell,"time[ncell]/D")
86 self.nt.Branch("prov",self.provcell,"prov[ncell]/s")
87 self.nt.Branch("qual",self.qcell,"qual[ncell]/s")
88 self.is_init=True
89 self.msg.debug("CellE Ttree init done")
90
91 if not self.isSC:
92 Cells = self.sg['AllCalo']
93 else:
94 Cells = self.sg['SCell']
95 nc = Cells.size()
96 cellitr=0;
97 for j in range(nc):
98 c=Cells[j]
99 id=c.ID()
100 if self.offlineID.is_tile(id): continue
101 if cellitr >= self.maxcells: continue
102 self.idvec[cellitr]=id.get_identifier32().get_compact()
103 self.calosamp[cellitr]=self.offlineID.calo_sample(id)
104 self.region[cellitr]=self.offlineID.region(id)
105 self.etaidx[cellitr]=self.offlineID.eta(id)
106 self.posneg[cellitr]=self.offlineID.pos_neg(id)
107 self.phiidx[cellitr]=self.offlineID.phi(id)
108 self.encell[cellitr]=c.energy()
109 self.timecell[cellitr]=c.time()
110 self.provcell[cellitr]=c.provenance()
111 self.qcell[cellitr]=c.quality()
112 cellitr += 1
113 self.aevtid[0]=self.evtid
114 self.abcid[0]=self.bcid
115 self.amaxcells[0]=cellitr
116 if self.amaxcells[0] > 0:
117 self.nt.Fill()
118 return 1
119
120 def finalize (self):
121 self.fout.cd()
122 self.nt.Write()
123 self.fout.Close()
124 return 1
125
126
127def usage():
128 print(sys.argv[0]+": Dump cells from pool file to ntuple")
129 print("Options:")
130 print("-i input file (default ESD.pool.root)")
131 print("-o output file (default cells.root)")
132 print("-n number of events to dump (default -1)")
133 print("-m MC pool file (default false)")
134 print("-s is SC (default false)")
135 print("--detdescr <DetDescrVersion>")
136 print("-h Print this help text and exit")
137
138try:
139 opts,args=getopt.getopt(sys.argv[1:],"i:o:n:msh",["help","detdescr="])
140except Exception as e:
141 usage()
142 print(e)
143 sys.exit(-1)
144
145
146ifile='ESD.pool.root'
147ofile="cells.root"
148nev=-1
149mc=False
150sc=False
151from AthenaConfiguration.TestDefaults import defaultGeometryTags
152detdescrtag=defaultGeometryTags.RUN2
153
154for o,a in opts:
155 if (o=="-i"): ifile=a
156 if (o=="-o"): ofile=a
157 if (o=="-n"): nev=int(a)
158 if (o=="-m"): mc=True
159 if (o=="-s"): sc=True
160 if (o=="-h" or o=="--help"):
161 usage()
162 sys.exit(0)
163 if (o=="--detdescr"):
164 detdescrtag=a
165
166#Don't let PyRoot open X-connections
167sys.argv = sys.argv[:1] + ['-b']
168
169from AthenaConfiguration.AllConfigFlags import initConfigFlags
170flags=initConfigFlags()
171flags.Input.Files = ifile.split(",")
172flags.Input.isMC=mc
173flags.IOVDb.DatabaseInstance="CONDBR2"
174flags.GeoModel.AtlasVersion = detdescrtag
175flags.LAr.doAlign=False
176
177from AthenaConfiguration.DetectorConfigFlags import disableDetectors, allDetectors
178disableDetectors(flags, allDetectors, toggle_geometry = True)
179flags.Detector.EnableLAr = True
180flags.Detector.EnableTile = True
181flags.Detector.EnableCalo = True
182
183from AthenaCommon.Constants import INFO
184flags.Exec.OutputLevel=INFO
185flags.lock()
186flags.dump()
187
188from RootUtils import PyROOTFixes # noqa F401
189from AthenaConfiguration.MainServicesConfig import MainServicesCfg
190cfg=MainServicesCfg(flags)
191
192from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
193cfg.merge(PoolReadCfg(flags))
194
195from TileGeoModel.TileGMConfig import TileGMCfg
196cfg.merge( TileGMCfg(flags) )
197from LArGeoAlgsNV.LArGMConfig import LArGMCfg
198cfg.merge(LArGMCfg(flags))
199if sc:
200 from AthenaConfiguration.ComponentFactory import CompFactory
201 cfg.addCondAlgo(CompFactory.CaloSuperCellAlignCondAlg())
202
203cfg.addEventAlgo(CellE(ofile,sc))
204
205cfg.run(nev)
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
const bool debug
void print(char *figname, TCanvas *c1)
MsgStream & msg() const
__init__(self, ofile, isSC)
virtual StatusCode execute() override
virtual StatusCode finalize() override
virtual StatusCode initialize() override
STL class.