ATLAS Offline Software
Loading...
Searching...
No Matches
TBCellNoiseCorrection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include "CaloDetDescr/CaloDetDescrElement.h"
10
12
13#include "CLHEP/Random/RandFlat.h"
14
15#include <TFile.h>
16#include <TChain.h>
17
18
19using CLHEP::RandFlat;
20
21
23 const std::string& name,
24 const IInterface* parent)
25 : CaloCellCorrection(type, name, parent),
27 m_engine(0),
28 m_tree(0), m_xcryo(0.), m_ytable(0.), m_energy(-1.), m_cell_id(0), m_cell_energy(0),m_entries(0)
29 //m_myevtnum(-1)
30{
31 declareInterface<CaloCellCorrection>(this);
32 declareProperty("NoiseFile", m_noise_file);
34 declareProperty("NoiseX", m_xcryo);
35 declareProperty("NoiseY", m_ytable);
36 declareProperty("NoiseE", m_energy);
37}
38
39// Desctructor
40
43
44
46{
47 ATH_MSG_DEBUG( " TBCellNoiseCorrection initialization " );
48
49 // RandomNumbers
50 ATH_CHECK( m_rndmSvc.retrieve() );
51 m_engine = m_rndmSvc->getEngine(this, "TB_NOISE");
52
53 if(m_energy > 0.) { // Do we have run parameters (x,y,energy) ?
54 ATH_MSG_DEBUG( " Trying to patch the noise file name " );
55 std::string fname = PathResolver::find_file ("xcryo_ytable.txt", "DATAPATH");
56 std::ifstream xfile;
57 xfile.open(fname.c_str());
58 if(!xfile.good()){
59 ATH_MSG_FATAL( "Could not open file xcryo_ytable.txt" );
60 return StatusCode::FAILURE;
61 }
62 int runnumber;
63 float x,y,en=-1;
64 std::string line;
65 while (getline(xfile, line, '\n')) {
66 std::istringstream buf(line);
67 buf >> runnumber >> x >> y >> en;
68 if(en < 0. || !(x == m_xcryo && y == m_ytable && en == m_energy)) continue;
69 break;
70 }
71 if(en < 0.) {
72 ATH_MSG_FATAL( " Do not have X: "<<m_xcryo<<" Y: "<<m_ytable<< " E: "<<m_energy<<" in xcryo_ytable.txt" );
73 return StatusCode::FAILURE;
74 }
75 // we have run number - let's change the file name (and take only one):
76 ATH_MSG_DEBUG( "Found run number: "<<runnumber );
77 unsigned pos = m_noise_file[0].find_last_of("0123456789");
78 std::ostringstream buf;
79 buf<<runnumber;
80 m_noise_file[0].replace(pos-3,4,buf.str());
81 ATH_MSG_DEBUG( "patched name: "<<m_noise_file[0].c_str() );
82 }
83
84 // Open file and get Tree
85 /*
86 m_root = new TFile(m_noise_file.c_str(),"READ");
87 if(!m_root->IsOpen()){
88 ATH_MSG_FATAL( "Could not open root file: "<<m_noise_file );
89 return StatusCode::FAILURE;
90 }
91 m_tree = (TTree*) m_root->Get("NoiseTree");
92 if(!m_tree) {
93 ATH_MSG_FATAL( "No NoiseTree in root file: "<<m_noise_file );
94 return StatusCode::FAILURE;
95 }
96 */
97 // Make a chain
98 m_tree = new TChain("NoiseTree");
99 for(unsigned int l=0; l<m_noise_file.size(); ++l) {
100 m_tree->AddFile(m_noise_file[l].c_str());
101 }
102 m_entries = m_tree->GetEntries();
103 if(m_entries < 100) {
104 ATH_MSG_FATAL( "Noise chain has less then 100 events !!!!" );
105 return StatusCode::FAILURE;
106 }
107 m_tree->SetBranchAddress("cell_id", &m_cell_id);
108 m_tree->SetBranchAddress("cell_ener", &m_cell_energy);
109 ATH_MSG_DEBUG(" Got NoiseTree with "<<m_entries<<" entries" );
110
112 ServiceHandle<IIncidentSvc> pIncSvc("IncidentSvc", name());
113 ATH_CHECK( pIncSvc.retrieve() );
114
115 //start listening to "BeginEvent"
116 const int PRIORITY = 100;
117 pIncSvc->addListener(this, "BeginEvent", PRIORITY);
118
119 ATH_MSG_DEBUG( " TBCellNoiseCorrection initialization finished" );
120
121 return StatusCode::SUCCESS;
122}
123
125{
126// m_root->Close();
127// m_root = NULL;
128 if(m_tree) m_tree->Delete() ;
129// m_cell_id = NULL;
130// m_cell_energy = NULL;
131
132 return StatusCode::SUCCESS;
133}
134// MakeCorrection: This is called with a pointer to the Cell Object.
135
137 const EventContext& /*ctx*/) const
138{
139
140// const CaloDetDescrElement* elt = theCell->caloDDE();
141
142 if (m_noise_correction) {
143 unsigned int cid = theCell->ID().get_identifier32().get_compact();
144 double e = theCell->energy();
145 unsigned int size = m_cell_id->size();
146 unsigned int i;
147 // removing this, should use the BadChannel now
148 // if(cid == 810049536) e = 0.; // Not working cell in data
149 for(i = 0; i < size; ++i) {
150 if(std::as_const(m_cell_id)->at(i) == cid) {
151 e += std::as_const(m_cell_energy)->at(i);
152 break;
153 }
154 }
155 if(i == size) {
156 ATH_MSG_ERROR( "Could not find a noise value for cell: "<<std::hex<<cid<<std::dec );
157 }
158
159 theCell->setEnergy(e);
160 }
161
162}
163
164void TBCellNoiseCorrection::handle(const Incident &inc) {
165 ATH_MSG_DEBUG( " Handle BeginEvent " );
166
167 if ( inc.type() == "BeginEvent") {
168 m_engine->setSeed ("TBCellNoseCorrection", inc.context());
169 unsigned int random = int(RandFlat::shoot(m_engine->getEngine (inc.context()), 0., m_entries-1.));
170 ATH_MSG_DEBUG( "Reading noise event: "<<random );
171 m_tree->GetEntry(random);
172 unsigned int size = m_cell_id->size();
173 unsigned int sizee = m_cell_energy->size();
174 if(size != sizee) {
175 ATH_MSG_ERROR( "Not equal size of noise vectors !!!! Something wrong !!!" );
176 }
177 }
178}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_DEBUG(x)
#define y
#define x
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Data object for each calorimeter readout cell.
Definition CaloCell.h:57
double energy() const
get energy (data member)
Definition CaloCell.h:327
virtual void setEnergy(float energy)
set energy
Definition CaloCell.h:472
Identifier ID() const
get ID (from cached data member) non-virtual and inline for fast access
Definition CaloCell.h:295
value_type get_compact() const
Get the compact id.
Identifier32 get_identifier32() const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
std::vector< std::string > m_noise_file
ATHRNG::RNGWrapper * m_engine
void MakeCorrection(CaloCell *theCell, const EventContext &ctx) const override
TBCellNoiseCorrection(const std::string &type, const std::string &name, const IInterface *parent)
ServiceHandle< IAthRNGSvc > m_rndmSvc
std::vector< float > * m_cell_energy
virtual StatusCode finalize() override
std::vector< unsigned int > * m_cell_id
virtual void handle(const Incident &) override
virtual StatusCode initialize() override
static std::vector< uint32_t > runnumber
Definition iLumiCalc.h:37