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