ATLAS Offline Software
TBCellNoiseCorrection.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "CaloDetDescr/CaloDetDescrElement.h"
8 #include "CaloEvent/CaloCell.h"
10 
12 
13 #include "CLHEP/Random/RandFlat.h"
14 
15 #include <TFile.h>
16 #include <TChain.h>
17 
18 
19 using CLHEP::RandFlat;
20 
21 
23  const std::string& name,
24  const IInterface* parent)
26  m_noise_correction(false),m_noise_file(),
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 
42 { }
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  IIncidentSvc* pIncSvc(0);
113 
114  // set up the incident service:
115  if (!(service("IncidentSvc", pIncSvc, true)).isSuccess()) {
116  ATH_MSG_ERROR( "Could not locate IncidentSvc " );
117  return StatusCode::FAILURE;
118  }
119 
120  //start listening to "BeginEvent"
121  static const int PRIORITY = 100;
122  pIncSvc->addListener(this, "BeginEvent", PRIORITY);
123 
124  ATH_MSG_DEBUG( " TBCellNoiseCorrection initialization finished" );
125 
126  return StatusCode::SUCCESS;
127 }
128 
130 {
131 // m_root->Close();
132 // m_root = NULL;
133  if(m_tree) m_tree->Delete() ;
134 // m_cell_id = NULL;
135 // m_cell_energy = NULL;
136 
137  return StatusCode::SUCCESS;
138 }
139 // MakeCorrection: This is called with a pointer to the Cell Object.
140 
142  const EventContext& /*ctx*/) const
143 {
144 
145 // const CaloDetDescrElement* elt = theCell->caloDDE();
146 
147  if (m_noise_correction) {
148  unsigned int cid = theCell->ID().get_identifier32().get_compact();
149  double e = theCell->energy();
150  unsigned int size = m_cell_id->size();
151  unsigned int i;
152  // removing this, should use the BadChannel now
153  // if(cid == 810049536) e = 0.; // Not working cell in data
154  for(i = 0; i < size; ++i) {
155  if(std::as_const(m_cell_id)->at(i) == cid) {
156  e += std::as_const(m_cell_energy)->at(i);
157  break;
158  }
159  }
160  if(i == size) {
161  ATH_MSG_ERROR( "Could not find a noise value for cell: "<<std::hex<<cid<<std::dec );
162  }
163 
164  theCell->setEnergy(e);
165  }
166 
167 }
168 
169 void TBCellNoiseCorrection::handle(const Incident &inc) {
170  ATH_MSG_DEBUG( " Handle BeginEvent " );
171 
172  if ( inc.type() == "BeginEvent") {
173  m_engine->setSeed ("TBCellNoseCorrection", inc.context());
174  unsigned int random = int(RandFlat::shoot(m_engine->getEngine (inc.context()), 0., m_entries-1.));
175  ATH_MSG_DEBUG( "Reading noise event: "<<random );
176  m_tree->GetEntry(random);
177  unsigned int size = m_cell_id->size();
178  unsigned int sizee = m_cell_energy->size();
179  if(size != sizee) {
180  ATH_MSG_ERROR( "Not equal size of noise vectors !!!! Something wrong !!!" );
181  }
182  }
183 }
ATHRNG::RNGWrapper::setSeed
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition: RNGWrapper.h:169
TBCellNoiseCorrection::m_noise_file
std::vector< std::string > m_noise_file
Definition: TBCellNoiseCorrection.h:46
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
checkFileSG.line
line
Definition: checkFileSG.py:75
TBCellNoiseCorrection::finalize
virtual StatusCode finalize() override
Definition: TBCellNoiseCorrection.cxx:129
TBCellNoiseCorrection::MakeCorrection
void MakeCorrection(CaloCell *theCell, const EventContext &ctx) const override
Definition: TBCellNoiseCorrection.cxx:141
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cxx:251
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
TBCellNoiseCorrection::m_ytable
float m_ytable
Definition: TBCellNoiseCorrection.h:55
TBCellNoiseCorrection::initialize
virtual StatusCode initialize() override
Definition: TBCellNoiseCorrection.cxx:45
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TBCellNoiseCorrection.h
CaloCellCorrection
Definition: CaloCellCorrection.h:51
CaloCell.h
TBCellNoiseCorrection::m_cell_id
std::vector< unsigned int > * m_cell_id
Definition: TBCellNoiseCorrection.h:58
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
TBCellNoiseCorrection::m_entries
unsigned int m_entries
Definition: TBCellNoiseCorrection.h:60
TBCellNoiseCorrection::m_engine
ATHRNG::RNGWrapper * m_engine
Definition: TBCellNoiseCorrection.h:51
CaloCell::setEnergy
virtual void setEnergy(float energy)
set energy
Definition: CaloCell.cxx:136
x
#define x
CaloCell::energy
double energy() const
get energy (data member)
Definition: CaloCell.h:311
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Identifier32::get_compact
value_type get_compact(void) const
Get the compact id.
Definition: Identifier32.h:171
TBCellNoiseCorrection::m_tree
TChain * m_tree
Definition: TBCellNoiseCorrection.h:53
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TBCellNoiseCorrection::handle
virtual void handle(const Incident &) override
Definition: TBCellNoiseCorrection.cxx:169
test_pyathena.parent
parent
Definition: test_pyathena.py:15
TBCellNoiseCorrection::m_energy
float m_energy
Definition: TBCellNoiseCorrection.h:56
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DeMoScan.runnumber
runnumber
Definition: DeMoScan.py:266
TBCellNoiseCorrection::~TBCellNoiseCorrection
virtual ~TBCellNoiseCorrection()
Definition: TBCellNoiseCorrection.cxx:41
TBCellNoiseCorrection::TBCellNoiseCorrection
TBCellNoiseCorrection(const std::string &type, const std::string &name, const IInterface *parent)
Definition: TBCellNoiseCorrection.cxx:22
TBCellNoiseCorrection::m_xcryo
float m_xcryo
Definition: TBCellNoiseCorrection.h:54
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
PlotCalibFromCool.en
en
Definition: PlotCalibFromCool.py:399
CaloCell::ID
Identifier ID() const
get ID (from cached data member) non-virtual and inline for fast access
Definition: CaloCell.h:279
TBCellNoiseCorrection::m_noise_correction
bool m_noise_correction
Definition: TBCellNoiseCorrection.h:45
TBCellNoiseCorrection::m_rndmSvc
ServiceHandle< IAthRNGSvc > m_rndmSvc
Definition: TBCellNoiseCorrection.h:50
ATHRNG::RNGWrapper::getEngine
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition: RNGWrapper.h:134
RNGWrapper.h
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
y
#define y
CaloCell
Data object for each calorimeter readout cell.
Definition: CaloCell.h:57
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Identifier::get_identifier32
Identifier32 get_identifier32(void) const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
TBCellNoiseCorrection::m_cell_energy
std::vector< float > * m_cell_energy
Definition: TBCellNoiseCorrection.h:59