ATLAS Offline Software
CaloGeometryFromFile.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <fstream>
6 #include <sstream>
7 
8 #include <TAxis.h>
9 #include <TFile.h>
10 #include <TGraph.h>
11 #include <TTree.h>
12 
13 #ifdef ENABLE_XROOTD_SUPPORT
14 #include "XrdStreamBuf.h"
15 #endif
16 
17 #include "CaloDetDescr/CaloDetDescrElement.h"
18 
19 #include "CaloGeometryFromFile.h"
20 
22 
24  const std::string& treeName,
25  const std::string& hashFileName)
26 {
27  std::map<uint64_t, uint64_t> cellId_vs_cellHashId_map;
28 
29  std::unique_ptr<std::istream> hashStream{};
30  std::unique_ptr<std::streambuf> hashStreamBuf{};
31 #ifdef ENABLE_XROOTD_SUPPORT
32  if (hashFileName.find("root://") != std::string::npos) {
33  hashStreamBuf = std::make_unique<XrdStreamBuf>(hashFileName);
34  hashStream = std::make_unique<std::istream>(hashStreamBuf.get());
35  } else {
36 #endif
37  std::unique_ptr<std::ifstream> hashStreamDirect = std::make_unique<std::ifstream>(hashFileName);
38  if (!hashStreamDirect->is_open()) {
39  std::cout << "Error: Could not open " << hashFileName << std::endl;
40  throw std::runtime_error("Could not open file");
41  }
42  hashStream = std::move(hashStreamDirect);
43 #ifdef ENABLE_XROOTD_SUPPORT
44  }
45 #endif
46 
47  std::cout << "Loading cellId_vs_cellHashId_map" << std::endl;
48 
49  int i = 0;
50  uint64_t id, hash_id;
51  while (!hashStream->eof()) {
52  i++;
53 
54  *hashStream >> id >> hash_id;
55  cellId_vs_cellHashId_map[id] = hash_id;
56  if (i % 10000 == 0)
57  std::cout << "Line: " << i << " id " << std::hex << id << " hash_id "
58  << std::dec << hash_id << std::endl;
59  }
60 
61  std::cout << "Done." << std::endl;
62 
63  auto f = std::unique_ptr<TFile>(TFile::Open(fileName.c_str()));
64  if (!f) {
65  std::cerr << "Error: Could not open file '" << fileName << "'" << std::endl;
66  return false;
67  }
68  TTree *tree = (TTree*)f->Get(treeName.c_str());
69  if (!tree)
70  return false;
71 
72  TTree *fChain = tree;
73 
75 
76  // List of branches
77  TBranch *b_identifier;
78  TBranch *b_calosample;
79  TBranch *b_eta;
80  TBranch *b_phi;
81  TBranch *b_r;
82  TBranch *b_eta_raw;
83  TBranch *b_phi_raw;
84  TBranch *b_r_raw;
85  TBranch *b_x;
86  TBranch *b_y;
87  TBranch *b_z;
88  TBranch *b_x_raw;
89  TBranch *b_y_raw;
90  TBranch *b_z_raw;
91  TBranch *b_deta;
92  TBranch *b_dphi;
93  TBranch *b_dr;
94  TBranch *b_dx;
95  TBranch *b_dy;
96  TBranch *b_dz;
97 
98  fChain->SetMakeClass(1);
99  fChain->SetBranchAddress("identifier", &cell.m_identify, &b_identifier);
100  fChain->SetBranchAddress("calosample", &cell.m_calosample, &b_calosample);
101  fChain->SetBranchAddress("eta", &cell.m_eta, &b_eta);
102  fChain->SetBranchAddress("phi", &cell.m_phi, &b_phi);
103  fChain->SetBranchAddress("r", &cell.m_r, &b_r);
104  fChain->SetBranchAddress("eta_raw", &cell.m_eta_raw, &b_eta_raw);
105  fChain->SetBranchAddress("phi_raw", &cell.m_phi_raw, &b_phi_raw);
106  fChain->SetBranchAddress("r_raw", &cell.m_r_raw, &b_r_raw);
107  fChain->SetBranchAddress("x", &cell.m_x, &b_x);
108  fChain->SetBranchAddress("y", &cell.m_y, &b_y);
109  fChain->SetBranchAddress("z", &cell.m_z, &b_z);
110  fChain->SetBranchAddress("x_raw", &cell.m_x_raw, &b_x_raw);
111  fChain->SetBranchAddress("y_raw", &cell.m_y_raw, &b_y_raw);
112  fChain->SetBranchAddress("z_raw", &cell.m_z_raw, &b_z_raw);
113  fChain->SetBranchAddress("deta", &cell.m_deta, &b_deta);
114  fChain->SetBranchAddress("dphi", &cell.m_dphi, &b_dphi);
115  fChain->SetBranchAddress("dr", &cell.m_dr, &b_dr);
116  fChain->SetBranchAddress("dx", &cell.m_dx, &b_dx);
117  fChain->SetBranchAddress("dy", &cell.m_dy, &b_dy);
118  fChain->SetBranchAddress("dz", &cell.m_dz, &b_dz);
119 
120  Long64_t nentries = fChain->GetEntriesFast();
121  for (Long64_t jentry = 0; jentry < nentries; jentry++) {
122  Long64_t ientry = fChain->LoadTree(jentry);
123  if (ientry < 0)
124  break;
125  fChain->GetEntry(jentry);
126 
127  if (cellId_vs_cellHashId_map.find(cell.m_identify)
128  != cellId_vs_cellHashId_map.end()) {
129  cell.m_hash_id = cellId_vs_cellHashId_map[cell.m_identify];
130  if (cell.m_hash_id != jentry)
131  std::cout << jentry << " : ERROR hash=" << cell.m_hash_id << std::endl;
132  } else
133  std::cout << std::endl
134  << "ERROR: Cell id not found in the cellId_vs_cellHashId_map!!!"
135  << std::endl
136  << std::endl;
137 
138  const CaloDetDescrElement *pcell = new CaloDetDescrElement(cell);
139  this->addcell(pcell);
140 
141  if (jentry % 25000 == 0) {
142  std::cout << "Checking loading cells from file" << std::endl
143  << jentry << " : " << pcell->getSampling() << ", "
144  << pcell->identify() << std::endl;
145 
146  }
147  }
148 
149  f->Close();
150  bool ok = PostProcessGeometry();
151 
152  std::cout << "Result of PostProcessGeometry(): " << ok << std::endl;
153 
154  const CaloDetDescrElement *mcell = 0;
155  unsigned long long cellid64(3179554531063103488);
156  Identifier cellid(cellid64);
157  mcell = this->getDDE(cellid); // This is working also for the FCal
158 
159  std::cout << "\n \n";
160  std::cout << "Testing whether CaloGeoGeometry is loaded properly"
161  << std::endl;
162  if (!mcell) {
163  std::cout << "Cell is not found" << std::endl;
164  }
165  else {
166  std::cout << "Identifier " << mcell->identify() << " sampling "
167  << mcell->getSampling() << " eta: " << mcell->eta()
168  << " phi: " << mcell->phi() << " CaloDetDescrElement=" << mcell
169  << std::endl
170  << std::endl;
171 
172  const CaloDetDescrElement *mcell2
173  = this->getDDE(mcell->getSampling(), mcell->eta(), mcell->phi());
174  std::cout << "Identifier " << mcell2->identify() << " sampling "
175  << mcell2->getSampling() << " eta: " << mcell2->eta()
176  << " phi: " << mcell2->phi() << " CaloDetDescrElement=" << mcell2
177  << std::endl
178  << std::endl;
179  }
180 
181  return ok;
182 }
183 
184 bool CaloGeometryFromFile::LoadFCalGeometryFromFiles(const std::array<std::string, 3> &fileNames)
185 {
186  std::vector<std::unique_ptr<std::istream>> electrodes;
187  std::vector<std::unique_ptr<std::streambuf>> electrodesBuf;
188  electrodes.reserve(3);
189  electrodesBuf.reserve(3);
190 
191  for (uint16_t i = 0; i < 3; i++) {
192  const std::string &file = fileNames[i];
193 #ifdef ENABLE_XROOTD_SUPPORT
194  if (file.find("root://") != std::string::npos) {
195  electrodesBuf.emplace_back(std::make_unique<XrdStreamBuf>(file));
196  electrodes.emplace_back(std::make_unique<std::istream>(electrodesBuf.back().get()));
197  } else {
198 #endif
199  std::unique_ptr<std::ifstream> directStream = std::make_unique<std::ifstream>(file);
200  if (!directStream->is_open()) {
201  std::cout << "Error: Could not open " << file << std::endl;
202  throw std::runtime_error("Could not open file");
203  }
204  electrodes.push_back(std::move(directStream));
205 #ifdef ENABLE_XROOTD_SUPPORT
206  }
207 #endif
208  }
209 
210  int thisTubeId;
211  int thisTubeI;
212  int thisTubeJ;
213  // int thisTubeID;
214  // int thisTubeMod;
215  double thisTubeX;
216  double thisTubeY;
217  TString tubeName;
218 
219  // int second_column;
220  std::string seventh_column;
221  std::string eight_column;
222  int ninth_column;
223 
224  int i;
225  for (int imodule = 1; imodule <= 3; imodule++) {
226  std::cout << "Loading FCal electrode #" << imodule << std::endl;
227 
228  i = 0;
229  while (1) {
230 
231  (*electrodes[imodule - 1]) >> tubeName;
232  if (electrodes[imodule - 1]->eof())
233  break;
234  (*electrodes[imodule - 1]) >> thisTubeId; // ?????
235  (*electrodes[imodule - 1]) >> thisTubeI;
236  (*electrodes[imodule - 1]) >> thisTubeJ;
237  (*electrodes[imodule - 1]) >> thisTubeX;
238  (*electrodes[imodule - 1]) >> thisTubeY;
239  (*electrodes[imodule - 1]) >> seventh_column;
240  (*electrodes[imodule - 1]) >> eight_column;
241  (*electrodes[imodule - 1]) >> ninth_column;
242 
243  tubeName.ReplaceAll("'", "");
244  std::string tubeNamestring = tubeName.Data();
245 
246  std::istringstream tileStream1(std::string(tubeNamestring, 1, 1));
247  std::istringstream tileStream2(std::string(tubeNamestring, 3, 2));
248  std::istringstream tileStream3(std::string(tubeNamestring, 6, 3));
249  int a1 = 0, a2 = 0, a3 = 0;
250  if (tileStream1)
251  tileStream1 >> a1;
252  if (tileStream2)
253  tileStream2 >> a2;
254  if (tileStream3)
255  tileStream3 >> a3;
256 
257  std::stringstream s;
258 
259  m_FCal_ChannelMap.add_tube(tubeNamestring, imodule, thisTubeId, thisTubeI,
260  thisTubeJ, thisTubeX, thisTubeY,
261  seventh_column);
262 
263  i++;
264  }
265  }
266 
267  m_FCal_ChannelMap.finish(); // Creates maps
268 
269  electrodes.clear();
270  electrodesBuf.clear();
271 
272  this->calculateFCalRminRmax();
273  return this->checkFCalGeometryConsistency();
274 }
275 
277 {
278 
279  std::stringstream ss;
280  ss << "FCal" << isam - 20 << std::endl;
281 
282  const int size = m_cells_in_sampling[isam].size();
283 
284  std::vector<double> x;
285  std::vector<double> y;
286  x.reserve(size);
287  y.reserve(size);
288 
289  for (auto it = m_cells_in_sampling[isam].begin();
290  it != m_cells_in_sampling[isam].end(); it++) {
291  x.push_back(it->second->x());
292  y.push_back(it->second->y());
293  }
294 
295  TGraph *graph = new TGraph(size, &x[0], &y[0]);
296  graph->SetLineColor(color);
297  graph->SetTitle(ss.str().c_str());
298  graph->SetMarkerStyle(21);
299  graph->SetMarkerColor(color);
300  graph->SetMarkerSize(0.5);
301  graph->GetXaxis()->SetTitle("x [mm]");
302  graph->GetYaxis()->SetTitle("y [mm]");
303 
304  graph->Draw("AP");
305 }
306 
308 {
309 
310  m_FCal_rmin.resize(3, FLT_MAX);
311  m_FCal_rmax.resize(3, 0.);
312 
313  double x(0.), y(0.), r(0.);
314  for (int imap = 1; imap <= 3; imap++)
315  for (auto it = m_FCal_ChannelMap.begin(imap);
316  it != m_FCal_ChannelMap.end(imap); it++) {
317  x = it->second.x();
318  y = it->second.y();
319  r = sqrt(x * x + y * y);
320  if (r < m_FCal_rmin[imap - 1])
321  m_FCal_rmin[imap - 1] = r;
322  if (r > m_FCal_rmax[imap - 1])
323  m_FCal_rmax[imap - 1] = r;
324  }
325 }
326 
328 {
329 
330  unsigned long long phi_index, eta_index;
331  float x, y, dx, dy;
332  long id;
333 
334  long mask1[]{0x34, 0x34, 0x35};
335  long mask2[]{0x36, 0x36, 0x37};
336 
337  m_FCal_rmin.resize(3, FLT_MAX);
338  m_FCal_rmax.resize(3, 0.);
339 
340  for (int imap = 1; imap <= 3; imap++) {
341 
342  int sampling = imap + 20;
343 
344  if ((int)m_cells_in_sampling[sampling].size()
345  != 2
347  m_FCal_ChannelMap.end(imap))) {
348  std::cout
349  << "Error: Incompatibility between FCalChannel map and GEO file: "
350  "Different number of cells in m_cells_in_sampling and "
351  "FCal_ChannelMap"
352  << std::endl;
353  std::cout << "m_cells_in_sampling: "
354  << m_cells_in_sampling[sampling].size() << std::endl;
355  std::cout << "FCal_ChannelMap: "
356  << 2
358  m_FCal_ChannelMap.end(imap))
359  << std::endl;
360  return false;
361  }
362 
363  for (auto it = m_FCal_ChannelMap.begin(imap);
364  it != m_FCal_ChannelMap.end(imap); it++) {
365 
366  phi_index = it->first & 0xffff;
367  eta_index = it->first >> 16;
368  x = it->second.x();
369  y = it->second.y();
370  m_FCal_ChannelMap.tileSize(imap, eta_index, phi_index, dx, dy);
371 
372  id = (mask1[imap - 1] << 12) + (eta_index << 5) + 2 * phi_index;
373 
374  if (imap == 2)
375  id += (8 << 8);
376 
377  Identifier id1((unsigned long long)(id << 44));
378  const CaloDetDescrElement *DDE1 = getDDE(id1);
379 
380  id = (mask2[imap - 1] << 12) + (eta_index << 5) + 2 * phi_index;
381  if (imap == 2)
382  id += (8 << 8);
383  Identifier id2((unsigned long long)(id << 44));
384  const CaloDetDescrElement *DDE2 = getDDE(id2);
385 
386  if (!TMath::AreEqualRel(x, DDE1->x(), 1.E-8)
387  || !TMath::AreEqualRel(y, DDE1->y(), 1.E-8)
388  || !TMath::AreEqualRel(x, DDE2->x(), 1.E-8)
389  || !TMath::AreEqualRel(y, DDE2->y(), 1.E-8)) {
390  std::cout
391  << "Error: Incompatibility between FCalChannel map and GEO file \n"
392  << x << " " << DDE1->x() << " " << DDE2->x() << y << " "
393  << DDE1->y() << " " << DDE2->y() << std::endl;
394  return false;
395  }
396  }
397  }
398 
399  return true;
400 }
CaloGeometryFromFile::checkFCalGeometryConsistency
bool checkFCalGeometryConsistency()
Definition: CaloGeometryFromFile.cxx:327
beamspotman.r
def r
Definition: beamspotman.py:676
CaloGeometry::PostProcessGeometry
virtual bool PostProcessGeometry()
Definition: CaloGeometry.cxx:627
CaloGeometryFromFile::DrawFCalGraph
void DrawFCalGraph(int isam, int color)
Definition: CaloGeometryFromFile.cxx:276
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
color
Definition: jFexInputByteStreamTool.cxx:25
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
CaloGeometry::m_FCal_rmax
std::vector< double > m_FCal_rmax
Definition: CaloGeometry.h:119
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
checkxAOD.fileNames
fileNames
Definition: Tools/PyUtils/bin/checkxAOD.py:73
CaloDetDescrElement::y
float y() const
cell y
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:365
FCAL_ChannelMap::end
tileMap_const_iterator end(int isam) const
Definition: LArCalorimeter/LArGeoModel/LArReadoutGeometry/LArReadoutGeometry/FCAL_ChannelMap.h:148
CaloGeometryFromFile::LoadGeometryFromFile
bool LoadGeometryFromFile(const std::string &fileName, const std::string &treeName, const std::string &hashFileName="/eos/atlas/atlascerngroupdisk/proj-simul/" "CaloGeometry/cellId_vs_cellHashId_map.txt")
Definition: CaloGeometryFromFile.cxx:23
CaloDetDescrElement
This class groups all DetDescr information related to a CaloCell. Provides a generic interface for al...
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:66
tree
TChain * tree
Definition: tile_monitor.h:30
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
skel.it
it
Definition: skel.GENtoEVGEN.py:423
FCAL_ChannelMap::add_tube
void add_tube(const std::string &tileName, int mod, int id, int i, int j, double xCm, double yCm)
Definition: LArCalorimeter/LArGeoModel/LArReadoutGeometry/src/FCAL_ChannelMap.cxx:69
CaloGeometry::addcell
virtual void addcell(const CaloDetDescrElement *cell)
Definition: CaloGeometry.cxx:67
CaloGeometryFromFile::calculateFCalRminRmax
void calculateFCalRminRmax()
Definition: CaloGeometryFromFile.cxx:307
x
#define x
CaloGeometry
Definition: CaloGeometry.h:27
CaloGeometry::m_FCal_ChannelMap
FCAL_ChannelMap m_FCal_ChannelMap
Definition: CaloGeometry.h:118
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
CaloDetDescrElement::identify
Identifier identify() const override final
cell identifier
Definition: CaloDetDescrElement.cxx:64
PlotCalibFromCool.nentries
nentries
Definition: PlotCalibFromCool.py:798
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
id2
HWIdentifier id2
Definition: LArRodBlockPhysicsV0.cxx:564
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
lumiFormat.i
int i
Definition: lumiFormat.py:92
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
CaloGeometry::m_FCal_rmin
std::vector< double > m_FCal_rmin
Definition: CaloGeometry.h:119
CaloGeometryFromFile.h
file
TFile * file
Definition: tile_monitor.h:29
CaloGeometryFromFile::LoadFCalGeometryFromFiles
bool LoadFCalGeometryFromFiles(const std::array< std::string, 3 > &fileNames)
Definition: CaloGeometryFromFile.cxx:184
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:20
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
createCablingJSON.eta_index
int eta_index
Definition: createCablingJSON.py:9
FCAL_ChannelMap::finish
void finish()
Definition: LArCalorimeter/LArGeoModel/LArReadoutGeometry/src/FCAL_ChannelMap.cxx:55
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:194
WriteCellNoiseToCool.mcell
mcell
Definition: WriteCellNoiseToCool.py:479
CaloDetDescrElement::x
float x() const
cell x
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:363
CaloGeometry::m_cells_in_sampling
std::vector< t_cellmap > m_cells_in_sampling
Definition: CaloGeometry.h:102
makeTRTBarrelCans.dy
tuple dy
Definition: makeTRTBarrelCans.py:21
y
#define y
CaloGeometry::getDDE
virtual const CaloDetDescrElement * getDDE(Identifier identify)
Definition: CaloGeometry.cxx:466
CaloDetDescrElement::getSampling
CaloCell_ID::CaloSample getSampling() const
cell sampling
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:395
CaloGeometryFromFile::CaloGeometryFromFile
CaloGeometryFromFile()
Definition: CaloGeometryFromFile.cxx:21
makeTRTBarrelCans.dx
tuple dx
Definition: makeTRTBarrelCans.py:20
FCAL_ChannelMap::begin
tileMap_const_iterator begin(int isam) const
Definition: LArCalorimeter/LArGeoModel/LArReadoutGeometry/LArReadoutGeometry/FCAL_ChannelMap.h:147
CaloDetDescrElement::eta
float eta() const
cell eta
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:344
CaloDetDescrElement::phi
float phi() const
cell phi
Definition: Calorimeter/CaloDetDescr/CaloDetDescr/CaloDetDescrElement.h:346
FCAL_ChannelMap::tileSize
void tileSize(int sam, int eta, int phi, float &dx, float &dy) const
Definition: LArCalorimeter/LArGeoModel/LArReadoutGeometry/src/FCAL_ChannelMap.cxx:429
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54