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