ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RootMaterialDecorator.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RootMaterialDecorator.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
16 #include <TFile.h>
17 #include <TH2F.h>
18 #include <TIterator.h>
19 #include <TKey.h>
20 #include <TList.h>
21 #include <boost/algorithm/string.hpp>
22 #include <boost/algorithm/string/finder.hpp>
23 #include <boost/algorithm/string/iter_find.hpp>
24 #include <cstdio>
25 #include <iostream>
26 #include <sstream>
27 #include <stdexcept>
28 #include <string>
29 
32  : m_cfg(cfg), m_inputFile(nullptr) {
33  // Validate the configuration
34  if (m_cfg.folderNameBase.empty()) {
35  throw std::invalid_argument("Missing ROOT folder name");
36  } else if (m_cfg.fileName.empty()) {
37  throw std::invalid_argument("Missing file name");
38  } else if (!m_cfg.logger) {
39  throw std::invalid_argument("Missing logger");
40  } else if (m_cfg.name.empty()) {
41  throw std::invalid_argument("Missing service name");
42  }
43 
44  // Setup ROOT I/O
45  m_inputFile = TFile::Open(m_cfg.fileName.c_str());
46  if (!m_inputFile) {
47  throw std::ios_base::failure("Could not open '" + m_cfg.fileName);
48  }
49 
50  // Get the list of keys from the file
51  TList* tlist = m_inputFile->GetListOfKeys();
52  auto tIter = tlist->MakeIterator();
53  tIter->Reset();
54 
55  // Iterate over the keys in the file
56  while (TKey* key = (TKey*)(tIter->Next())) {
57  // The surface material to be read in for this
58  std::shared_ptr<const Acts::ISurfaceMaterial> sMaterial = nullptr;
59 
60  // Remember the directory
61  std::string tdName(key->GetName());
62 
63  ACTS_VERBOSE("Processing directory: " << tdName);
64 
65  // volume
66  std::vector<std::string> splitNames;
67  iter_split(splitNames, tdName,
68  boost::algorithm::first_finder(m_cfg.voltag));
69  boost::split(splitNames, splitNames[1], boost::is_any_of("_"));
70  Acts::GeometryID::Value volID = std::stoi(splitNames[0]);
71  // boundary
72  iter_split(splitNames, tdName,
73  boost::algorithm::first_finder(m_cfg.boutag));
74  boost::split(splitNames, splitNames[1], boost::is_any_of("_"));
75  Acts::GeometryID::Value bouID = std::stoi(splitNames[0]);
76  // layer
77  iter_split(splitNames, tdName,
78  boost::algorithm::first_finder(m_cfg.laytag));
79  boost::split(splitNames, splitNames[1], boost::is_any_of("_"));
80  Acts::GeometryID::Value layID = std::stoi(splitNames[0]);
81  // approach
82  iter_split(splitNames, tdName,
83  boost::algorithm::first_finder(m_cfg.apptag));
84  boost::split(splitNames, splitNames[1], boost::is_any_of("_"));
85  Acts::GeometryID::Value appID = std::stoi(splitNames[0]);
86  // sensitive
87  iter_split(splitNames, tdName,
88  boost::algorithm::first_finder(m_cfg.sentag));
89  Acts::GeometryID::Value senID = std::stoi(splitNames[1]);
90 
91  // Reconstruct the geometry ID
92  Acts::GeometryID geoID;
93  geoID.setVolume(volID);
94  geoID.setBoundary(bouID);
95  geoID.setLayer(layID);
96  geoID.setApproach(appID);
97  geoID.setSensitive(senID);
98  ACTS_VERBOSE("GeometryID re-constructed as " << geoID);
99 
100  // Construct the names
101  std::string nName = tdName + "/" + m_cfg.ntag;
102  std::string vName = tdName + "/" + m_cfg.vtag;
103  std::string oName = tdName + "/" + m_cfg.otag;
104  std::string minName = tdName + "/" + m_cfg.mintag;
105  std::string maxName = tdName + "/" + m_cfg.maxtag;
106  std::string tName = tdName + "/" + m_cfg.ttag;
107  std::string x0Name = tdName + "/" + m_cfg.x0tag;
108  std::string l0Name = tdName + "/" + m_cfg.l0tag;
109  std::string aName = tdName + "/" + m_cfg.atag;
110  std::string zName = tdName + "/" + m_cfg.ztag;
111  std::string rhoName = tdName + "/" + m_cfg.rhotag;
112 
113  // Get the histograms
114  TH1F* n = dynamic_cast<TH1F*>(m_inputFile->Get(nName.c_str()));
115  TH1F* v = dynamic_cast<TH1F*>(m_inputFile->Get(vName.c_str()));
116  TH1F* o = dynamic_cast<TH1F*>(m_inputFile->Get(oName.c_str()));
117  TH1F* min = dynamic_cast<TH1F*>(m_inputFile->Get(minName.c_str()));
118  TH1F* max = dynamic_cast<TH1F*>(m_inputFile->Get(maxName.c_str()));
119  TH2F* t = dynamic_cast<TH2F*>(m_inputFile->Get(tName.c_str()));
120  TH2F* x0 = dynamic_cast<TH2F*>(m_inputFile->Get(x0Name.c_str()));
121  TH2F* l0 = dynamic_cast<TH2F*>(m_inputFile->Get(l0Name.c_str()));
122  TH2F* A = dynamic_cast<TH2F*>(m_inputFile->Get(aName.c_str()));
123  TH2F* Z = dynamic_cast<TH2F*>(m_inputFile->Get(zName.c_str()));
124  TH2F* rho = dynamic_cast<TH2F*>(m_inputFile->Get(rhoName.c_str()));
125 
126  // Only go on when you have all histograms
127  if (n and v and o and min and max and t and x0 and l0 and A and Z and rho) {
128  // Get the number of bins
129  int nbins0 = t->GetNbinsX();
130  int nbins1 = t->GetNbinsY();
131 
132  // The material matrix
133  Acts::MaterialPropertiesMatrix materialMatrix(
134  nbins1,
136 
137  // We need binned material properties
138  if (nbins0 * nbins1 > 1) {
139  // Fill the matrix first
140  for (int ib0 = 1; ib0 <= nbins0; ++ib0) {
141  for (int ib1 = 1; ib1 <= nbins1; ++ib1) {
142  double dt = t->GetBinContent(ib0, ib1);
143  if (dt > 0.) {
144  double dx0 = x0->GetBinContent(ib0, ib1);
145  double dl0 = l0->GetBinContent(ib0, ib1);
146  double da = A->GetBinContent(ib0, ib1);
147  double dz = Z->GetBinContent(ib0, ib1);
148  double drho = rho->GetBinContent(ib0, ib1);
149  // Create material properties
150  materialMatrix[ib1 - 1][ib0 - 1] =
151  Acts::MaterialProperties(dx0, dl0, da, dz, drho, dt);
152  }
153  }
154  }
155 
156  // Now reconstruct the bin untilities
157  Acts::BinUtility bUtility;
158  for (int ib = 1; ib < n->GetNbinsX() + 1; ++ib) {
159  size_t nbins = size_t(n->GetBinContent(ib));
160  Acts::BinningValue val = Acts::BinningValue(v->GetBinContent(ib));
161  Acts::BinningOption opt = Acts::BinningOption(o->GetBinContent(ib));
162  float rmin = min->GetBinContent(ib);
163  float rmax = max->GetBinContent(ib);
164  bUtility += Acts::BinUtility(nbins, rmin, rmax, opt, val);
165  }
166  ACTS_VERBOSE("Created " << bUtility);
167 
168  // Construct the binned material with the right bin utility
169  sMaterial = std::make_shared<const Acts::BinnedSurfaceMaterial>(
170  bUtility, std::move(materialMatrix));
171 
172  } else {
173  // Only homogeneous material present
174  double dt = t->GetBinContent(1, 1);
175  double dx0 = x0->GetBinContent(1, 1);
176  double dl0 = l0->GetBinContent(1, 1);
177  double da = A->GetBinContent(1, 1);
178  double dz = Z->GetBinContent(1, 1);
179  double drho = rho->GetBinContent(1, 1);
180  // Create and set the homogenous surface material
181  sMaterial = std::make_shared<const Acts::HomogeneousSurfaceMaterial>(
182  Acts::MaterialProperties(dx0, dl0, da, dz, drho, dt));
183  }
184  }
185  ACTS_VERBOSE("Successfully read Material for : " << geoID);
186 
187  // Insert into the new collection
188  m_surfaceMaterialMap.insert({geoID, std::move(sMaterial)});
189  }
190 }
191 
193  m_inputFile->Close();
194 }