ATLAS Offline Software
TBNoiseWrite.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class TBNoiseWrite // Write ROOT Tree with noise in randomly triggered
6 // events
7 //
8 // author: A.Minaenko
9 // date: 08/05/2008
10 
11 #include "TBNoiseWrite.h"
12 
14 
15 #include <TFile.h>
16 #include <TTree.h>
17 
18 #include "CaloEvent/CaloCell.h"
20 
21 #include "TBEvent/TBEventInfo.h"
22 
23 #include <fstream>
24 
25 TBNoiseWrite::TBNoiseWrite(const std::string& name,
26  ISvcLocator* pSvcLocator) :
27  AthAlgorithm(name, pSvcLocator),
28  m_nEvent(0), m_nEventRandomTrigger(0), m_first(true),
29  m_cell_id(0),
30  m_cell_energy(0),
31  m_caloCellContainerName("AllCalo"),
32  m_headerTreeName("HeaderTree"),
33  m_noiseTreeName("NoiseTree"),
34  m_rootfile_name("tbh6tree_noise.root"),
35  m_rootfile(0),
36  m_header_tree(0),
37  m_tree(0),
38  //m_calo_id(0),
39  //m_calo_dd_man(0),
40  m_txtFileWithXY("xcryo_ytable.txt")
41 {
43  declareProperty("RootFileName",m_rootfile_name);
44  declareProperty("NoiseTreeName",m_noiseTreeName);
45 
46  m_header.m_nRun = 0;
47  m_header.m_beamMom = 0;
48  m_header.m_xCryo = 0;
49  m_header.m_yTable = 0;
50 }
51 
53 { }
54 
56 {
57  // Cell vectors
58  m_cell_id = new std::vector<unsigned int>;
59  m_cell_energy = new std::vector<float>;
60 
61  // Open file and create TTrees
62  m_rootfile = new TFile(m_rootfile_name.data(), "RECREATE");
63  if (!m_rootfile->IsOpen()) {
64  ATH_MSG_FATAL ( "Cann't open Root file" );
65  return StatusCode::FAILURE;
66  }
67  m_header_tree =
68  new TTree(m_headerTreeName.c_str(), m_headerTreeName.c_str());
69  m_tree = new TTree(m_noiseTreeName.c_str(), m_noiseTreeName.c_str());
70 
71  // Define branches
72  // Header
74  Branch("Header",&m_header.m_nRun,"nrun/I:ener/F:xcryo:ytable");
75  // Cell parameters
76  m_tree->Branch("cell_id", &m_cell_id);
77  m_tree->Branch("cell_ener", &m_cell_energy);
78 
79  ATH_MSG_INFO ( "end of initialize()" );
80  return StatusCode::SUCCESS;
81 }
82 
83 
85 {
86  ATH_MSG_INFO ( "finalize(): (invoked/random) ("<< m_nEvent
87  << "/" << m_nEventRandomTrigger << ")" );
88  ATH_MSG_INFO ( "Print contents of " << m_noiseTreeName );
89  m_rootfile->Print();
90  m_rootfile->Write();
91  m_rootfile->Close();
92  return StatusCode::SUCCESS;
93 }
94 
96 {
97  m_cell_id->clear();
98  m_cell_energy->clear();
99 }
100 
102 {
103  m_nEvent++;
104  ATH_MSG_DEBUG ( "Executing TBNoiseWrite " );
105 
106  // Retrieve Event Info
107  const TBEventInfo* theEventInfo;
108  ATH_CHECK( evtStore()->retrieve(theEventInfo,"TBEventInfo") );
109  unsigned short evType = theEventInfo->getEventType();
110  ATH_MSG_DEBUG ( "Event Type found " << evType );
111  if (evType != 3) return StatusCode::FAILURE;
112 
113  // Do first event initialization (run header filling)
114  if (m_first) {
115  m_first = false;
116  // Fill run header
117  m_header.m_nRun = theEventInfo->getRunNum();
118  m_header.m_beamMom = theEventInfo->getBeamMomentum();
119  // Get xcryo and ytable from a file
121  m_header.m_beamMom)) {
122  ATH_MSG_ERROR ( "xCryo and yTable are not found for run " <<
123  m_header.m_nRun << " in file " << m_txtFileWithXY );
124  return StatusCode::FAILURE;
125  }
126  ATH_MSG_INFO ( "nRun = " << m_header.m_nRun << ", beamMomentum = "
127  << m_header.m_beamMom << " GeV, CryoX = " << m_header.m_xCryo
128  << ", tableY = " << m_header.m_yTable );
129  m_header_tree->Fill();
130  }
131 
132  this->clear();
133 
134  // Get cell information
135  const CaloCellContainer* cellContainer = 0;
136  ATH_CHECK( evtStore()->retrieve(cellContainer, m_caloCellContainerName) );
137 
138  // Cell loop
139  for (const CaloCell* cell : *cellContainer) {
140  unsigned int id = cell->ID().get_identifier32().get_compact();
141  m_cell_id->push_back(id);
142  m_cell_energy->push_back((float)cell->energy());
143  }
144  // Fill the tree
145  m_tree->Fill();
147  return StatusCode::SUCCESS;
148 
149 }
150 
151 StatusCode TBNoiseWrite::getXcryoYtable(float &x, float &y, float &e) {
152 
153  ATH_MSG_DEBUG ( "in getXcryoYtable(float x, float y)" );
154 
155  std::ifstream xyFile;
156  std::string line;
157  std::string filename = PathResolver::find_file(m_txtFileWithXY, "DATAPATH");
158  xyFile.open(filename.c_str());
159  if (!xyFile.is_open()) {
160  ATH_MSG_ERROR ( "File " << m_txtFileWithXY << " fail to open in $DATAPATH");
161  return StatusCode::FAILURE;
162  }
163 
164  while ( getline(xyFile, line, '\n') ) {
165  int run;
166  std::istringstream buf(line);
167  e = 0;
168  buf >> run >> x >> y >> e;
169  ATH_MSG_DEBUG ( "run,x,y,e= "<<run<<" "<<x<<" "<<y<<" "<<e);
170  if (run == m_header.m_nRun && xyFile.good()) return StatusCode::SUCCESS;
171  }
172 
173  return StatusCode::FAILURE;
174 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
TBNoiseWrite::m_noiseTreeName
std::string m_noiseTreeName
Definition: TBNoiseWrite.h:57
TBNoiseWrite::TBNoiseWrite
TBNoiseWrite(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TBNoiseWrite.cxx:25
TBNoiseWrite::NoiseHeader::m_xCryo
float m_xCryo
Beam momentum.
Definition: TBNoiseWrite.h:42
TBNoiseWrite::NoiseHeader::m_beamMom
float m_beamMom
Run number.
Definition: TBNoiseWrite.h:41
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
checkFileSG.line
line
Definition: checkFileSG.py:75
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TBNoiseWrite::m_cell_id
std::vector< unsigned int > * m_cell_id
Definition: TBNoiseWrite.h:50
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
TBNoiseWrite::m_cell_energy
std::vector< float > * m_cell_energy
Definition: TBNoiseWrite.h:51
TBNoiseWrite::NoiseHeader::m_yTable
float m_yTable
CryoX.
Definition: TBNoiseWrite.h:43
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TBNoiseWrite::m_first
bool m_first
Definition: TBNoiseWrite.h:36
TBEventInfo::getRunNum
unsigned int getRunNum() const
Definition: TBEventInfo.h:67
TBNoiseWrite.h
run
int run(int argc, char *argv[])
Definition: ttree2hdf5.cxx:28
CaloCell.h
TBEventInfo::getEventType
int getEventType() const
Definition: TBEventInfo.h:66
TBNoiseWrite::m_rootfile_name
std::string m_rootfile_name
Definition: TBNoiseWrite.h:58
TBNoiseWrite::m_caloCellContainerName
std::string m_caloCellContainerName
Definition: TBNoiseWrite.h:55
x
#define x
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TBNoiseWrite::m_nEventRandomTrigger
int m_nEventRandomTrigger
Definition: TBNoiseWrite.h:35
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
TBNoiseWrite::m_header_tree
TTree * m_header_tree
Definition: TBNoiseWrite.h:60
TBNoiseWrite::m_txtFileWithXY
std::string m_txtFileWithXY
Text file containing xCryo and yTable.
Definition: TBNoiseWrite.h:64
TBNoiseWrite::NoiseHeader::m_nRun
int m_nRun
Definition: TBNoiseWrite.h:40
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TBNoiseWrite::getXcryoYtable
StatusCode getXcryoYtable(float &x, float &y, float &eBeam)
Get Xcryo and Ytable from a text file.
Definition: TBNoiseWrite.cxx:151
run
Definition: run.py:1
AthAlgorithm
Definition: AthAlgorithm.h:47
TBNoiseWrite::m_tree
TTree * m_tree
Definition: TBNoiseWrite.h:61
TBNoiseWrite::m_header
NoiseHeader m_header
Definition: TBNoiseWrite.h:45
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TBNoiseWrite::m_nEvent
int m_nEvent
Definition: TBNoiseWrite.h:34
CaloCellContainer.h
CaloCellContainer
Container class for CaloCell.
Definition: CaloCellContainer.h:55
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
TBEventInfo
Definition: TBEventInfo.h:27
TBNoiseWrite::m_headerTreeName
std::string m_headerTreeName
Definition: TBNoiseWrite.h:56
TBNoiseWrite::clear
void clear()
Definition: TBNoiseWrite.cxx:95
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
TBEventInfo::getBeamMomentum
float getBeamMomentum() const
Definition: TBEventInfo.h:68
TBEventInfo.h
TBNoiseWrite::m_rootfile
TFile * m_rootfile
Definition: TBNoiseWrite.h:59
TBNoiseWrite::initialize
virtual StatusCode initialize() override
Definition: TBNoiseWrite.cxx:55
TBNoiseWrite::~TBNoiseWrite
virtual ~TBNoiseWrite()
Definition: TBNoiseWrite.cxx:52
TBNoiseWrite::execute
virtual StatusCode execute() override
Definition: TBNoiseWrite.cxx:101
TBNoiseWrite::finalize
virtual StatusCode finalize() override
Definition: TBNoiseWrite.cxx:84