ATLAS Offline Software
Loading...
Searching...
No Matches
LArMinBiasAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "LArMinBiasAlg.h"
6
11
12#include "LArSimEvent/LArHit.h"
14#include "TTree.h"
15#include "GaudiKernel/ITHistSvc.h"
16#include "CaloDetDescr/CaloDetDescrElement.h"
17
18 //Constructor
19 LArMinBiasAlg:: LArMinBiasAlg(const std::string& name, ISvcLocator* pSvcLocator):
20 AthAlgorithm(name,pSvcLocator)
21 {
22 }
23
24 //__________________________________________________________________________
25 //Destructor
27 //__________________________________________________________________________
29 {
30
31 ATH_MSG_INFO ( " LArMinBiasAlg initialize() " );
32
33 ATH_CHECK( m_thistSvc.retrieve() );
34
35
36 m_tree = new TTree("m_tree","Offset ntuple");
37 m_tree->Branch("ncell",&m_nsymcell,"ncell/I");
38 m_tree->Branch("nevt_total",&m_nevt_total,"nevt_total/I");
39 m_tree->Branch("identifier",m_identifier,"identifier[ncell]/I");
40 m_tree->Branch("region",m_region,"region[ncell]/I");
41 m_tree->Branch("ieta",m_ieta,"ieta[ncell]/I");
42 m_tree->Branch("layer",m_layer,"layer[ncell]/I");
43 m_tree->Branch("region",m_region,"region[ncell]/I");
44 m_tree->Branch("ieta",m_ieta,"ieta[ncell]/I");
45 m_tree->Branch("eta",m_eta,"eta[ncell]/F");
46 m_tree->Branch("phi",m_phi,"phi[ncell]/F");
47 m_tree->Branch("nevt",m_nevt,"nevt[ncell]/D");
48 m_tree->Branch("average",m_average,"average[ncell]/D");
49 m_tree->Branch("rms",m_rms,"rms[ncell]/D");
50 m_tree->Branch("reference",m_offset,"offset[ncell]/D");
51
52 if( m_thistSvc->regTree("/file1/offset",m_tree).isFailure()) {
53 ATH_MSG_WARNING(" cannot register ntuple " );
54 return StatusCode::SUCCESS;
55 }
56
57 const CaloIdManager* mgr = nullptr;
58 ATH_CHECK( detStore()->retrieve( mgr ) );
59 if(m_isSC) {
60 m_calo_id = (const CaloCell_Base_ID*)mgr->getCaloCell_SuperCell_ID();
61 } else {
62 m_calo_id = (const CaloCell_Base_ID*)mgr->getCaloCell_ID();
63 }
64
65 ATH_CHECK(m_mcSymKey.initialize());
66
67 ATH_CHECK(m_cablingKey.initialize());
68
69 ATH_CHECK(m_caloMgrKey.initialize());
70
71 ATH_CHECK(m_caloSCMgrKey.initialize(m_isSC));
72
73 ATH_CHECK(m_eventInfoKey.initialize());
74
75 ATH_CHECK(m_larHitKeys.assign(m_inputKeys.value()));
76 ATH_CHECK(m_larHitKeys.initialize(!m_inputKeys.empty() ));
77
78 if(m_isSC) ATH_CHECK( m_scidtool.retrieve() );
79
80 m_n1=0;
81 m_n2=0;
82
84
85 return StatusCode::SUCCESS;
86
87 }
88 //__________________________________________________________________________
90 {
91 ATH_MSG_INFO("number of events in the two samples " << m_n1 << " " << m_n2);
92 this->fillNtuple();
93 ATH_MSG_INFO(" stop after fill ntuple");
94 return StatusCode::SUCCESS;
95 }
96
97 //__________________________________________________________________________
99 {
100 //.............................................
101
102 ATH_MSG_DEBUG(" LArMinBiasAlg execute()");
103
104 const EventContext& ctx = Gaudi::Hive::currentContext();
105
106 if (m_first) {
107
108 const CaloDetDescrManager_Base *cMgr=nullptr;
109 if(m_isSC){
111 ATH_CHECK(caloMgrHandle.isValid());
112 cMgr=(const CaloDetDescrManager_Base *)(*caloMgrHandle);
113 } else {
115 ATH_CHECK(caloMgrHandle.isValid());
116 cMgr=(const CaloDetDescrManager_Base *)(*caloMgrHandle);
117 }
118
121 const LArOnOffIdMapping* cabling{*cablingHdl};
122 if(!cabling) {
123 ATH_MSG_ERROR( "Do not have cabling mapping from key " << m_cablingKey.key() );
124 return StatusCode::FAILURE;
125 }
126
127 m_ncell = m_calo_id->calo_cell_hash_max();
128 ATH_MSG_DEBUG("Hash max: "<<m_ncell);
129
130 ATH_MSG_INFO(" --- first event " << m_ncell);
131 m_symCellIndex.resize(m_ncell,-1);
132 std::vector<int> doneCell;
133 doneCell.resize(m_ncell,-1);
134
135 //m_readCell.resize(m_ncell,0);
136
137 m_eCell.resize(m_ncell,0.);
138
139 m_CellList.reserve(MAX_SYM_CELLS);
140 int nsym=0;
141 // loop over cells
142 // and find symmetry cells
143 for (unsigned int i=0;i<((unsigned int)(m_ncell));i++) {
144 IdentifierHash idHash=i;
145 Identifier id=m_calo_id->cell_id(idHash);
146 if (m_calo_id->is_tile(id)) continue;
147 // convert cell id to symmetric identifier
148 HWIdentifier hwid2 = mcsym->ZPhiSymOfl(id);
149 Identifier id2 = cabling->cnvToIdentifier(hwid2);
150 int i2 = (int) (m_calo_id->calo_cell_hash(id2));
151 if(i2>=m_ncell) {
152 ATH_MSG_WARNING("problem: i2: "<<i2<<" for id: "<<m_calo_id->print_to_string(id)<<" symmetrized: "<<m_calo_id->print_to_string(id2));
153 }
154 // we have already processed this hash => just need to associate cell i to the same symmetric cell
155 if (doneCell[i2]>=0) {
156 m_symCellIndex[i]=doneCell[i2];
157 ATH_MSG_DEBUG("Adding cell "<<id.get_identifier32().get_compact()<<" to a symmetrized cell "<<id2.get_identifier32().get_compact());
158 }
159 // we have not already processed this hash, add an entry for this new symmetric cell
160 else {
161 ATH_MSG_DEBUG("New symmetrized cell "<<id2.get_identifier32().get_compact());
162 doneCell[i2]=nsym;
163 m_symCellIndex[i] = nsym;
164 CellInfo cell;
165 const CaloDetDescrElement* calodde = cMgr->get_element(id);
166 cell.eta = calodde->eta();
167 cell.phi = calodde->phi();
168 cell.region = m_calo_id->region(id);
169 cell.ieta = m_calo_id->eta(id);
170 cell.layer = m_calo_id->calo_sample(id);
171 cell.region = m_calo_id->region(id);
172 cell.ieta = m_calo_id->eta(id);
173 //cell.identifier = id2.get_identifier32().get_compact();
174 cell.identifier = id2;
175 cell.average=0.;
176 cell.offset=0.;
177 cell.rms=0.;
178 cell.nevt=0.;
179 m_CellList.push_back(cell);
180 nsym++;
181 }
182 }
183 ATH_MSG_INFO(" --- number of symmetric cells found " << nsym << " " << m_CellList.size());
184 if (nsym>=MAX_SYM_CELLS) ATH_MSG_ERROR(" More than "<<MAX_SYM_CELLS<<" number of symmetric cells... Fix array size for ntuple writing !!!! ");
185 m_nsymcell=nsym;
186 m_first=false;
187 }
188
190 if (!eventInfo.isValid()) {
191 ATH_MSG_ERROR ("Could not retrieve EventInfo");
192 return StatusCode::FAILURE;
193 }
194 int channelNumber = eventInfo->mcChannelNumber();
195
196 m_nevt_total++;
197
198 double weight=1.;
199
200// Dataset ID for lowPt MinBias
201 if (channelNumber==m_datasetID_lowPt) {
202 weight = m_weight_lowPt;
203 m_n1+=1;
204 }
205// Dataset ID for highPt MinBias
206 else if (channelNumber==m_datasetID_highPt) {
207 weight = m_weight_highPt;
208 m_n2+=1;
209 }
210 else {
211 ATH_MSG_WARNING(" Neither low nor high Pt MinBias sample " << channelNumber << " set weight to 1.0 ");
212 weight=1.;
213 }
214
215
216 if ((m_nevt_total%100)==1) ATH_MSG_INFO(" ---- process event number " << m_nevt_total << " " << channelNumber << " weight " << weight);
217
218 for (int i=0;i<m_ncell;i++) m_eCell[i]=0.;
219
220 auto hitVectorHandles = m_larHitKeys.makeHandles(ctx);
221 for (auto & inputHits : hitVectorHandles) {
222 if (!inputHits.isValid()) {
223 ATH_MSG_ERROR("BAD HANDLE"); //FIXME improve error here
224 //return StatusCode::FAILURE;
225 continue;
226 }
227
228 for (const LArHit* hit : *inputHits)
229 {
230 Identifier hitCellID=hit->cellID();
231 double energy = hit->energy();
232 double time =hit->time();
233 Identifier cellID;
234 if(m_isSC){
235 cellID=m_scidtool->offlineToSuperCellID(hitCellID);
236 } else {
237 cellID=hitCellID;
238 }
239 int index = (int) (m_calo_id->calo_cell_hash(cellID));
240
241 if (index < m_ncell && index>=0 && std::fabs(time)<25.) {
242 m_eCell[index] += energy;
243 }
244
245 } // loop over hits in container
246 } // loop over containers
247
248 for (int i=0;i<m_ncell;i++) {
249 addCell(i,m_eCell[i],0.,weight);
250 }
251
252
253 return StatusCode::SUCCESS;
254 }
255
256 void LArMinBiasAlg::addCell(int index, double energy, double eshift, double weight)
257 {
258 if (index < m_ncell && index>=0) {
259 int index2= m_symCellIndex[index];
260 if (index2<0) return;
261 if (index2 >= ((int)(m_CellList.size())) ) {
262 ATH_MSG_INFO(" LArMinBiasAlg::addCell: for " << index << ", " << index2 << " is out-of bounds for list of size " << m_CellList.size());
263 return;
264 }
265 double oldN = m_CellList[index2].nevt;
266 double oldAverage = m_CellList[index2].average;
267 double oldRMS = m_CellList[index2].rms;
268 double oldAverage2 = m_CellList[index2].offset;
269
270 double frac = oldN/(weight+oldN);
271 double Anew = weight+oldN;
272 double newAverage = frac*oldAverage + weight*energy/Anew;
273 double deltaE = energy-newAverage;
274 double newRMS = frac*(oldRMS + (newAverage-oldAverage)*(newAverage-oldAverage)) + weight*deltaE*deltaE/Anew;
275
276 double newAverage2 = frac*oldAverage2 + weight*eshift/Anew;
277
278 m_CellList[index2].nevt = Anew;
279 m_CellList[index2].average = newAverage;
280 m_CellList[index2].rms = newRMS;
281 m_CellList[index2].offset = newAverage2;
282
283 }
284 }
285
287 {
288
289 ATH_MSG_INFO(" in fillNtuple " << m_nsymcell);
290 for (int i=0;i<m_nsymcell;i++) {
291 m_identifier[i] = m_CellList[i].identifier.get_identifier32().get_compact();
292 m_layer[i] = m_CellList[i].layer;
293 m_region[i] = m_CellList[i].region;
294 m_ieta[i] = m_CellList[i].ieta;
295 m_eta[i] = m_CellList[i].eta;
296 m_phi[i] = m_CellList[i].phi;
297 m_nevt[i] = m_CellList[i].nevt;
298 m_offset[i] = (float) (m_CellList[i].offset);
299 m_average[i] = (float) (m_CellList[i].average);
300 m_rms[i] = (float) (std::sqrt(m_CellList[i].rms));
301 }
302 m_tree->Fill();
303 ATH_MSG_INFO(" after tree fill ");
304
305 for (int i=0;i<m_nsymcell;i++) {
306 m_CellList[i].nevt=0;
307 m_CellList[i].offset=0.;
308 m_CellList[i].average=0;
309 m_CellList[i].rms=0;
310 }
311 ATH_MSG_INFO(" end of fillNtuple ");
312
313 }
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Helper class for offline supercell identifiers.
#define MAX_SYM_CELLS
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
const ServiceHandle< StoreGateSvc > & detStore() const
Helper base class for offline cell identifiers.
This class groups all DetDescr information related to a CaloCell.
const CaloDetDescrElement * get_element(const Identifier &cellId) const
get element by its identifier
This class initializes the Calo (LAr and Tile) offline identifiers.
value_type get_compact() const
Get the compact id.
This is a "hash" representation of an Identifier.
Identifier32 get_identifier32() const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
Class to store hit energy and time in LAr cell from G4 simulation.
Definition LArHit.h:25
std::vector< CellInfo > m_CellList
double m_average[MAX_SYM_CELLS]
virtual StatusCode initialize() override
Gaudi::Property< int > m_datasetID_lowPt
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
double m_rms[MAX_SYM_CELLS]
SG::ReadHandleKeyArray< LArHitContainer > m_larHitKeys
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKey
double m_offset[MAX_SYM_CELLS]
int m_region[MAX_SYM_CELLS]
std::vector< int > m_symCellIndex
double m_nevt[MAX_SYM_CELLS]
Gaudi::Property< double > m_weight_lowPt
Gaudi::Property< int > m_datasetID_highPt
int m_layer[MAX_SYM_CELLS]
virtual StatusCode execute() override
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloMgrKey
ToolHandle< ICaloSuperCellIDTool > m_scidtool
StringArrayProperty m_inputKeys
SG::ReadCondHandleKey< LArMCSym > m_mcSymKey
std::vector< double > m_eCell
float m_eta[MAX_SYM_CELLS]
virtual StatusCode stop() override
BooleanProperty m_isSC
const CaloCell_Base_ID * m_calo_id
LArMinBiasAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Gaudi::Property< double > m_weight_highPt
~LArMinBiasAlg()
Default Destructor.
float m_phi[MAX_SYM_CELLS]
ServiceHandle< ITHistSvc > m_thistSvc
SG::ReadCondHandleKey< CaloSuperCellDetDescrManager > m_caloSCMgrKey
void addCell(int index, double e1, double e2, double wt=1.)
int m_ieta[MAX_SYM_CELLS]
int m_identifier[MAX_SYM_CELLS]
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Definition index.py:1