ATLAS Offline Software
Loading...
Searching...
No Matches
TBXMLCaloCellWriterTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5
8#include "TBXMLWriter.h"
9
10#include "GaudiKernel/MsgStream.h"
11
12#include "CLHEP/Units/SystemOfUnits.h"
13
14#include "Identifier/Identifier.h"
16
18#include "CaloDetDescr/CaloDetDescrElement.h"
19
21
22#include "CaloGeoHelpers/CaloSampling.h"
23#include "CaloEvent/CaloCell.h"
25
26#include "boost/io/ios_state.hpp"
27
28#include <iostream>
29#include <fstream>
30#include <iomanip>
31
32#include <vector>
33#include <string>
34#include <sstream>
35#include <map>
36
37#include <cmath>
38
39
40using CLHEP::deg;
41
42
43const unsigned int TBXMLCaloCellWriterTool::m_nCols = 2;
44
46 const std::string& name,
47 const IInterface* parent)
48 : TBXMLWriterToolBase(type,name,parent)
49 , m_etaMin(-5.0)
50 , m_etaMax(5.0)
51 , m_phiMin(0.*deg)
52 , m_phiMax(360.*deg)
53 , m_firstEvent(true)
54 , m_idHelper(0)
55{
56 declareProperty("InputCellContainer", m_cellContainer);
57 declareProperty("IncludedCalos", m_includedCalos);
58 declareProperty("IncludedSamplings", m_includedSamplings);
59 declareProperty("EtaMin", m_etaMin);
60 declareProperty("EtaMax", m_etaMax);
61 declareProperty("PhiMin", m_phiMin);
62 declareProperty("PhiMax", m_phiMax);
63 // save pointer to algorithm
64 m_mother = dynamic_cast<const TBXMLWriter*>(parent);
65}
66
69
71{
73 ATH_CHECK(m_caloMgrKey.initialize());
74 return StatusCode::SUCCESS;
75}
76
78// Event Writer //
80
81StatusCode
83 const std::string& /* entryTag */)
84{
85 // messaging
86 MsgStream log(msgSvc(),name());
87
89 // On the Fly Initialization //
91
92 if ( m_firstEvent )
93 {
94 // check pointer
95 if ( m_mother == 0 )
96 {
97 log << MSG::ERROR
98 << "tool does not hang off the TBXMLWriter algorithm."
99 << endmsg;
100 return StatusCode::FAILURE;
101 }
102
103 // convert poperties
104 StatusCode checkOut = this->convertProperties();
105 if ( checkOut.isFailure() )
106 {
107 log << MSG::ERROR
108 << "cannot convert properties correctly!"
109 << endmsg;
110 return StatusCode::FAILURE;
111 }
112 m_firstEvent = false;
113
114 // print out configuration
115 log << MSG::INFO
116 << "list of included calo samplings:"
117 << endmsg;
118 for ( unsigned int i=0; i<m_caloSamplings.size(); i++ )
119 {
120 log << MSG::INFO
121 << "Sampling \042"
123 << "\042 with SubCalo index "
124 << (int)m_caloSamplings[i]
125 << endmsg;
126 }
127 }
128
130 // Check On Begin Run //
132
133 const EventContext& ctx = Gaudi::Hive::currentContext();
134 EventIDBase::number_type run_number = ctx.eventID().run_number();
135 if (m_runNumbers.insert (run_number).second) {
136 StatusCode checkOut = this->writeRunFiles(m_mother->getFileDir(),
137 run_number);
138 if ( checkOut.isFailure() )
139 {
140 log << MSG::ERROR
141 << "cannot produce run XML files."
142 << endmsg;
143 return StatusCode::FAILURE;
144 }
145 }
146
147
149 // Retrieve Data from StoreGate //
151
152 const CaloCellContainer* theContainer = 0;
153 ATH_CHECK( evtStore()->retrieve(theContainer,m_cellContainer) );
154
156 // Loop Individual Calorimeters //
158
159 for ( unsigned int i=0; i<m_caloIndices.size(); i++ )
160 {
161 // write container tag
162 outStream << "<!-- TBXMLCaloCellWriterTool: begin write cell container "
163 << m_includedCalos[i] << " -->" << std::endl;
164
165 std::vector<std::string> theCellAttrNames;
166 theCellAttrNames.push_back("name");
167 theCellAttrNames.push_back("ncols");
168 theCellAttrNames.push_back("nrows");
169 theCellAttrNames.push_back("idtype");
170 theCellAttrNames.push_back("etype");
171 std::vector<std::string> theCellAttrValues;
172 theCellAttrValues.push_back(m_includedCalos[i]);
173 std::ostringstream theCols;
174 theCols << m_nCols << std::ends;
175
176 // get data size
177 unsigned int dataSize = theContainer->nCellsCalo(m_caloIndices[i]);
178 unsigned int nRows =
179 (unsigned int)ceil( (double)dataSize / (double)m_nCols );
180 std::ostringstream theRows;
181 theRows << nRows << std::ends;
182 theCellAttrValues.push_back(theCols.str());
183 theCellAttrValues.push_back(theRows.str());
184 theCellAttrValues.push_back("uint");
185 theCellAttrValues.push_back("double");
186 this->openElement(outStream,"CaloCellContainer",
187 theCellAttrNames,theCellAttrValues);
188
189 // loop on cells
191 theContainer->beginConstCalo(m_caloIndices[i]);
193 theContainer->endConstCalo(m_caloIndices[i]);
194
195 // baseline index
196 IdentifierHash baseIndex, lastIndex;
197 m_idHelper->calo_cell_hash_range((int)m_caloIndices[i],
198 baseIndex, lastIndex);
199
200 unsigned int theCtr = 0;
201 bool isTerminated = false;
202 for ( ; firstCell != lastCell; ++firstCell )
203 {
204 // check if cell in requested layer and range
205 double eta = (*firstCell)->eta();
206 double phi = (*firstCell)->phi();
207 const CaloDetDescrElement * theCaloDDE= (*firstCell)->caloDDE();
209 if (theCaloDDE) {
210 theLayer = (CaloSampling::CaloSample) theCaloDDE->getSampling();
211 }else{
212 theLayer = CaloSampling::Unknown;
213 }
214 if ( ( std::find(m_caloSamplings.begin(),
215 m_caloSamplings.end(),
216 theLayer) != m_caloSamplings.end() ) &&
217 ( ( eta > m_etaMin && eta < m_etaMax ) &&
218 ( phi > m_phiMin && phi < m_phiMax ) )
219 )
220 {
221 // get calorimeter hash index
222 IdentifierHash theIndex =
223 (*firstCell)->caloDDE()->calo_hash();
224 // get eta, phi, region
225 int etaIndex = m_idHelper->eta((*firstCell)->ID());
226 int phiIndex = m_idHelper->phi((*firstCell)->ID());
227 int regIndex = m_idHelper->region((*firstCell)->ID());
228 // int subIndex = m_idHelper->sub_calo((*firstCell)->ID());
229 int lyrIndex = m_idHelper->sampling((*firstCell)->ID());
230 int subIndex = -1;
231 // get subIndex
232 if ( m_idHelper->is_em_barrel((*firstCell)->ID()) )
233 {
234 subIndex = 0;
235 }
236 else if ( m_idHelper->is_em_endcap((*firstCell)->ID()) )
237 {
238 subIndex = 1;
239 }
240 else if ( m_idHelper->is_tile_barrel((*firstCell)->ID()) )
241 {
242 subIndex = 2;
243 }
244 else if ( m_idHelper->is_tile_extbarrel((*firstCell)->ID()) )
245 {
246 subIndex = 3;
247 }
248 else if ( m_idHelper->is_hec((*firstCell)->ID()) )
249 {
250 subIndex = 4;
251 }
252 else if ( m_idHelper->is_fcal((*firstCell)->ID()) )
253 {
254 subIndex = 5;
255 }
256 // get signal
257 double theEnergy = (*firstCell)->e();
258 if ( theCtr == 0 || ( theCtr % m_nCols ) == 0 )
259 {
260 outStream << " ";
261 }
262 theIndex -= (int)baseIndex;
263 boost::io::ios_base_all_saver streamsave (outStream);
264 outStream
265 << std::setw(10) << std::setfill(' ') << theIndex << " "
266 << std::setw(3) << std::setfill(' ') << subIndex << " "
267 << std::setw(3) << std::setfill(' ') << lyrIndex << " "
268 << std::setw(3) << std::setfill(' ') << regIndex << " "
269 << std::setw(3) << std::setfill(' ') << etaIndex << " "
270 << std::setw(3) << std::setfill(' ') << phiIndex << " "
271 << std::setw(10) << std::setprecision(5) << theEnergy;
272 theCtr++;
273 if ( (isTerminated = ( theCtr % m_nCols )) == 0 )
274 {
275 outStream << std::endl;
276 }
277 }
278 }
279 if ( ! isTerminated ) outStream << std::endl;
280 this->closeElement(outStream);
281 // write container tag
282 outStream << "<!-- TBXMLCaloCellWriterTool: end write cell container "
283 << m_includedCalos[i] << " -->" << std::endl;
284 }
285
286 return StatusCode::SUCCESS;
287}
288
290// Run File Writer //
292
293StatusCode
294TBXMLCaloCellWriterTool::writeRunFiles(const std::string& fileDir,
295 unsigned int runNumber)
296{
297
299 // Write Dictionary //
301
302 // construct directory and file name
303 std::ostringstream thisFileName;
304 thisFileName << fileDir << "/geom."
305 << std::setw(6) << std::setfill('0')
306 << runNumber << ".xml" << std::ends;
307
308 std::ofstream thisFileStream((thisFileName.str()).c_str());
309
310 // Document type
311 std::vector<std::string> theRunElements;
312 theRunElements.push_back("FCalGeomTable*");
313 this->openDictionary(thisFileStream,"FCalGeomTable",theRunElements);
314 std::vector<std::string> theRunAttr;
315 theRunAttr.push_back("name CDATA #REQUIRED");
316 this->addAttributes(thisFileStream,"FCalGeomTable",theRunAttr);
317
318 // FCal geometry table
319 std::vector<std::string> anyData;
320 anyData.push_back("ANY");
321 this->addDictElement(thisFileStream,"FCalGeomTable",anyData);
322 std::vector<std::string> theGeomAttr;
323 theGeomAttr.push_back("name CDATA #REQUIRED");
324 this->addAttributes(thisFileStream,"FCalGeomTable",theGeomAttr);
325
326 // close dictionary
327 this->closeDictionary(thisFileStream);
328
330 // Write Run Geometry //
332
334 const CaloDetDescrManager* caloDetMgr = *caloMgrHandle;
335 ATH_CHECK( detStore()->retrieve (caloDetMgr, "CaloMgr") );
336
337 IdentifierHash firstIndex, lastIndex, safeIndex;
338 m_idHelper->calo_cell_hash_range((int)CaloCell_ID::LARFCAL,
339 firstIndex, lastIndex);
340 safeIndex = firstIndex;
341
342 // open element
343 std::vector<std::string> theRunValues(theRunAttr.size());
344 for ( unsigned int i=0; i<theRunAttr.size(); i++ )
345 {
346 if ( (theRunAttr[i]).find("name") != std::string::npos )
347 {
348 theRunValues[i] = "FCalMod0";
349 theRunAttr[i] =
350 theRunAttr[i].substr(0,theRunAttr[i].find_first_of(" "));
351 }
352 }
353 this->openElement(thisFileStream,"FCalGeomTable",theRunAttr,theRunValues);
354 theRunValues[0] = "FCalCells";
355 this->openElement(thisFileStream,"FCalGeomTable",theRunAttr,theRunValues);
356 thisFileStream << "<!-- 32-bit Id"
357 << " Module# "
358 << " EtaIndex "
359 << " PhiIndex "
360 << " X [cm] "
361 << " Y [cm] "
362 << " Z [cm] "
363 << " TileSize "
364 << "-->" << std::endl;
365
366 // find big/small tile indicator -> to be replaced
367 std::map<int,double> smallestDx;
368 std::map<int,double> largestDx;
369 for ( unsigned int iCtr = (unsigned int)safeIndex;
370 iCtr <= (unsigned int)lastIndex; iCtr++ )
371 {
372 IdentifierHash theIndex(iCtr);
373 Identifier theId = m_idHelper->cell_id(theIndex);
374 int theCalo = m_idHelper->sub_calo(theId);
375 const CaloDetDescrElement* theElement =
376 caloDetMgr->get_element(theIndex);
377 double dx = theElement->dx();
378 // check tile size
379 if ( smallestDx.find(theCalo) == smallestDx.end() )
380 {
381 smallestDx[theCalo] = dx;
382 largestDx[theCalo] = dx;
383 }
384 else
385 {
386 if ( dx < smallestDx[theCalo] ) smallestDx[theCalo] = dx;
387 if ( dx > largestDx[theCalo] ) largestDx[theCalo] = dx;
388 }
389 }
390
391 // get geometry
392 for (unsigned int iCtr=(unsigned int)firstIndex;
393 iCtr<=(unsigned int)lastIndex; iCtr++ )
394 {
395 IdentifierHash theIndex(iCtr);
396 // get identifiers
397 Identifier theId = m_idHelper->cell_id(theIndex);
398 int theModule = m_idHelper->sampling(theId);
399 int theEta = m_idHelper->eta(theId);
400 int thePhi = m_idHelper->phi(theId);
401 int theCalo = m_idHelper->sub_calo(theId);
402 // get geometry
403 const CaloDetDescrElement* theElement =
404 caloDetMgr->get_element(theIndex);
405 double theX = theElement->x();
406 double theY = theElement->y();
407 double theZ = theElement->z();
408 // check tile size
409 double dx = theElement->dx();
410 int tileSize = dx > 1.1*smallestDx[theCalo] ? 1 : 0;
411
412 // write out
413 thisFileStream << " "
414 << std::setw(10)
415 << std::setfill(' ') << iCtr-(unsigned int)firstIndex
416 << " "
417 << std::setw(2) << std::setfill(' ') << theModule
418 << " "
419 << std::setw(2) << std::setfill(' ') << theEta
420 << " "
421 << std::setw(2) << std::setfill(' ') << thePhi
422 << " "
423 << std::setw(10) << std::setprecision(5) << theX << " "
424 << std::setw(10) << std::setprecision(5) << theY << " "
425 << std::setw(10) << std::setprecision(5) << theZ << " "
426 << " "
427 << std::setw(2) << std::setfill(' ') << tileSize
428 << std::endl;
429 }
430 ATH_CHECK(this->finalize(thisFileStream));
431 thisFileStream.close();
432
433 return StatusCode::SUCCESS;
434}
435
436StatusCode
438{
439 // messaging
440 MsgStream log(msgSvc(),name());
441
442 // get calo id helper
443 ATH_CHECK( detStore()->retrieve (m_idHelper, "CaloCell_ID") );
444
446 // Get Calo Indices //
448
449 std::vector<std::string>::iterator firstCalo = m_includedCalos.begin();
450 std::vector<std::string>::iterator lastCalo = m_includedCalos.end();
451 while ( firstCalo != lastCalo )
452 {
453 if ( *firstCalo == "LAREM" )
454 {
456 ++firstCalo;
457 }
458 else if ( *firstCalo == "LARHEC" )
459 {
461 ++firstCalo;
462 }
463 else if ( *firstCalo == "LARFCAL" )
464 {
466 ++firstCalo;
467 }
468 else if ( *firstCalo == "TILE" )
469 {
471 ++firstCalo;
472 }
473 else
474 {
475 firstCalo = m_includedCalos.erase(firstCalo);
476 }
477 }
478
480 // Get CaloSamplings //
482
483 // nasty (no switch on string types...)
484 for (const std::string& sample : m_includedSamplings)
485 {
486 // H8 calos
487 if ( sample == "PreSamplerB" )
488 m_caloSamplings.push_back(CaloSampling::PreSamplerB);
489 if ( sample == "EMB0" )
490 m_caloSamplings.push_back(CaloSampling::EMB1);
491 if ( sample == "EMB1" )
492 m_caloSamplings.push_back(CaloSampling::EMB2);
493 if ( sample == "EMB2" )
494 m_caloSamplings.push_back(CaloSampling::EMB3);
495 if ( sample == "TileBar0" )
496 m_caloSamplings.push_back(CaloSampling::TileBar0);
497 if ( sample == "TileBar1" )
498 m_caloSamplings.push_back(CaloSampling::TileBar0);
499 if ( sample == "TileBar2" )
500 m_caloSamplings.push_back(CaloSampling::TileBar0);
501 if ( sample == "TileExt0" )
502 m_caloSamplings.push_back(CaloSampling::TileExt0);
503 if ( sample == "TileExt1" )
504 m_caloSamplings.push_back(CaloSampling::TileExt1);
505 if ( sample == "TileExt2" )
506 m_caloSamplings.push_back(CaloSampling::TileExt2);
507
508 // H6 calos
509 if ( sample == "EME1" )
510 m_caloSamplings.push_back(CaloSampling::EME1);
511 if ( sample == "EME2" )
512 m_caloSamplings.push_back(CaloSampling::EME2);
513 if ( sample == "HEC0" )
514 m_caloSamplings.push_back(CaloSampling::HEC0);
515 if ( sample == "HEC1" )
516 m_caloSamplings.push_back(CaloSampling::HEC1);
517 if ( sample == "HEC2" )
518 m_caloSamplings.push_back(CaloSampling::HEC2);
519 if ( sample == "HEC3" )
520 m_caloSamplings.push_back(CaloSampling::HEC3);
521 if ( sample == "FCAL0" )
522 m_caloSamplings.push_back(CaloSampling::FCAL0);
523 if ( sample == "FCAL1" )
524 m_caloSamplings.push_back(CaloSampling::FCAL1);
525 if ( sample == "FCAL2" )
526 m_caloSamplings.push_back(CaloSampling::FCAL2);
527 }
528
529 return m_caloSamplings.size() == 0 && m_caloIndices.size() == 0
530 ? StatusCode::FAILURE
531 : StatusCode::SUCCESS;
532}
533
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
Definition of CaloDetDescrManager.
#define deg
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
const ServiceHandle< StoreGateSvc > & detStore() const
Container class for CaloCell.
CaloCellContainer::const_iterator beginConstCalo(CaloCell_ID::SUBCALO caloNum) const
get const iterators on cell of just one calo
CaloCellContainer::const_iterator endConstCalo(CaloCell_ID::SUBCALO caloNum) const
int nCellsCalo(const CaloCell_ID::SUBCALO caloNum) const
get number of cels of given calorimeter
This class groups all DetDescr information related to a CaloCell.
CaloCell_ID::CaloSample getSampling() const
cell sampling
const CaloDetDescrElement * get_element(const Identifier &cellId) const
get element by its identifier
This class provides the client interface for accessing the detector description information common to...
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
This is a "hash" representation of an Identifier.
std::vector< std::string > m_includedSamplings
virtual StatusCode writeRunFiles(const std::string &fileDir, unsigned int runNumber) override
virtual StatusCode writeEvent(std::ostream &outFile, const std::string &) override
TBXMLCaloCellWriterTool(const std::string &type, const std::string &name, const IInterface *parent)
tool constructor
virtual StatusCode initialize() override
static const unsigned int m_nCols
std::unordered_set< EventIDBase::number_type > m_runNumbers
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloMgrKey
std::vector< std::string > m_includedCalos
virtual StatusCode convertProperties()
std::vector< CaloCell_ID::SUBCALO > m_caloIndices
std::vector< CaloSampling::CaloSample > m_caloSamplings
virtual void openDictionary(std::ostream &outStream, const std::string &dictName, const std::vector< std::string > &listOfElements)
virtual void openElement(std::ostream &outStream, const std::string &theElement, const std::vector< std::string > &listOfAttr, const std::vector< std::string > &listOfValues)
TBXMLWriterToolBase(const std::string &type, const std::string &name, const IInterface *parent)
virtual StatusCode finalize() override
virtual void closeElement(std::ostream &outStream, const std::string &theElement)
virtual void addDictElement(std::ostream &outStream, const std::string &theElement, const std::vector< std::string > &listOfElements)
virtual void closeDictionary(std::ostream &outStream)
virtual void addAttributes(std::ostream &outStream, const std::string &theElement, const std::vector< std::string > &listOfAttributes)
virtual StatusCode initialize() override
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138