ATLAS Offline Software
Loading...
Searching...
No Matches
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
25TBNoiseWrite::TBNoiseWrite(const std::string& name,
26 ISvcLocator* pSvcLocator) :
27 AthAlgorithm(name, pSvcLocator),
29 m_cell_id(0),
31 m_caloCellContainerName("AllCalo"),
32 m_headerTreeName("HeaderTree"),
33 m_noiseTreeName("NoiseTree"),
34 m_rootfile_name("tbh6tree_noise.root"),
35 m_rootfile(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
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 }
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
120 if (!this->getXcryoYtable(m_header.m_xCryo,m_header.m_yTable,
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
151StatusCode 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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
#define y
#define x
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Container class for CaloCell.
Data object for each calorimeter readout cell.
Definition CaloCell.h:57
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
float getBeamMomentum() const
Definition TBEventInfo.h:73
int getEventType() const
Definition TBEventInfo.h:71
unsigned int getRunNum() const
Definition TBEventInfo.h:72
virtual StatusCode execute() override
std::string m_txtFileWithXY
Text file containing xCryo and yTable.
TFile * m_rootfile
std::string m_noiseTreeName
std::vector< unsigned int > * m_cell_id
virtual StatusCode finalize() override
std::string m_caloCellContainerName
std::string m_headerTreeName
virtual StatusCode initialize() override
TBNoiseWrite(const std::string &name, ISvcLocator *pSvcLocator)
int m_nEventRandomTrigger
TTree * m_tree
std::string m_rootfile_name
StatusCode getXcryoYtable(float &x, float &y, float &eBeam)
Get Xcryo and Ytable from a text file.
virtual ~TBNoiseWrite()
NoiseHeader m_header
std::vector< float > * m_cell_energy
TTree * m_header_tree
Definition run.py:1