CMS 3D CMS Logo

DD4hep_MaterialAccountingGroup.cc
Go to the documentation of this file.
1 #include <sstream>
2 #include <iomanip>
3 #include <string>
4 #include <stdexcept>
5 
6 #include <TFile.h>
7 #include <TH1F.h>
8 #include <TProfile.h>
9 #include <TCanvas.h>
10 #include <TFrame.h>
11 
13 
20 
23  : m_name(name),
24  m_elements(),
25  m_boundingbox(),
26  m_accounting(),
27  m_errors(),
28  m_tracks(0),
29  m_counted(false),
30  m_file(nullptr) {
31  cms::DDFilter filter("TrackingMaterialGroup", {name.data(), name.size()});
33  bool firstChild = fv.firstChild();
34 
35  edm::LogVerbatim("TrackingMaterialAnalysis") << "Elements within: " << name;
36 
37  for (const auto& j : fv.specpars()) {
38  for (const auto& k : j.second->paths) {
39  if (firstChild) {
40  std::vector<std::vector<cms::Node*>> children = fv.children(k);
41  for (auto const& path : children) {
42  cms::Translation trans = fv.translation(path);
43  GlobalPoint gp = GlobalPoint(trans.x(), trans.y(), trans.z());
44  m_elements.emplace_back(gp);
45  edm::LogVerbatim("TrackerMaterialAnalysis")
46  << "MaterialAccountingGroup:\t"
47  << "Adding element at (r,z) " << gp.perp() << "," << gp.z() << std::endl;
48  }
49  }
50  }
51  }
52 
53  for (unsigned int i = 0; i < m_elements.size(); ++i) {
55  }
56 
58 
59  edm::LogVerbatim("TrackerMaterialAnalysis")
60  << "MaterialAccountingGroup:\t"
61  << "Final BBox r_range: " << m_boundingbox.range_r().first << ", " << m_boundingbox.range_r().second << std::endl
62  << "Final BBox z_range: " << m_boundingbox.range_z().first << ", " << m_boundingbox.range_z().second << std::endl;
63 
64  m_dedx_spectrum = std::make_shared<TH1F>((m_name + "_dedx_spectrum").c_str(), "Energy loss spectrum", 1000, 0., 1.);
66  std::make_shared<TH1F>((m_name + "_radlen_spectrum").c_str(), "Radiation lengths spectrum", 1000, 0., 1.);
67  m_dedx_vs_eta = std::make_shared<TProfile>((m_name + "_dedx_vs_eta").c_str(), "Energy loss vs. eta", 600, -3., 3.);
68  m_dedx_vs_z = std::make_shared<TProfile>((m_name + "_dedx_vs_z").c_str(), "Energy loss vs. Z", 6000, -300., 300.);
69  m_dedx_vs_r = std::make_shared<TProfile>((m_name + "_dedx_vs_r").c_str(), "Energy loss vs. R", 1200, 0., 120.);
71  std::make_shared<TProfile>((m_name + "_radlen_vs_eta").c_str(), "Radiation lengths vs. eta", 600, -3., 3.);
73  std::make_shared<TProfile>((m_name + "_radlen_vs_z").c_str(), "Radiation lengths vs. Z", 6000, -300., 300.);
75  std::make_shared<TProfile>((m_name + "_radlen_vs_r").c_str(), "Radiation lengths vs. R", 1200, 0., 120.);
76 
77  m_dedx_spectrum->SetDirectory(nullptr);
78  m_radlen_spectrum->SetDirectory(nullptr);
79  m_dedx_vs_eta->SetDirectory(nullptr);
80  m_dedx_vs_z->SetDirectory(nullptr);
81  m_dedx_vs_r->SetDirectory(nullptr);
82  m_radlen_vs_eta->SetDirectory(nullptr);
83  m_radlen_vs_z->SetDirectory(nullptr);
84  m_radlen_vs_r->SetDirectory(nullptr);
85 }
86 
88  const GlobalPoint& position = detector.position();
89 
90  edm::LogVerbatim("MaterialAccountingGroup")
91  << "Testing position: (x, y, z, r) = " << position.x() << ", " << position.y() << ", " << position.z() << ", "
92  << position.perp() << std::endl;
93 
94  if (not m_boundingbox.inside(position.perp(), position.z())) {
95  edm::LogVerbatim("MaterialAccountingGroup")
96  << "r outside of: (" << m_boundingbox.range_r().first << ", " << m_boundingbox.range_r().second
97  << "), Z ouside of: (" << m_boundingbox.range_z().first << ", " << m_boundingbox.range_z().second << ")"
98  << std::endl;
99  return false;
100  } else {
101  edm::LogVerbatim("MaterialAccountingGroup")
102  << "r within: (" << m_boundingbox.range_r().first << ", " << m_boundingbox.range_r().second << "), Z within: ("
103  << m_boundingbox.range_z().first << ", " << m_boundingbox.range_z().second << ")" << std::endl;
104 
105  for (unsigned int i = 0; i < m_elements.size(); ++i) {
106  edm::LogVerbatim("MaterialAccountingGroup")
107  << "Closest testing agains(x, y, z, r): (" << m_elements[i].x() << ", " << m_elements[i].y() << ", "
108  << m_elements[i].z() << ", " << m_elements[i].perp() << ") --> " << (position - m_elements[i]).mag()
109  << " vs tolerance: " << s_tolerance << std::endl;
110  if ((position - m_elements[i]).mag2() < (s_tolerance * s_tolerance))
111  return true;
112  }
113  return false;
114  }
115 }
116 
118  if (!isInside(detector))
119  return false;
120 
121  m_buffer += detector.material();
122  m_counted = true;
123 
124  return true;
125 }
126 
128  if (m_counted) {
131  ++m_tracks;
132 
133  GlobalPoint average((m_buffer.in().x() + m_buffer.out().x()) / 2.,
134  (m_buffer.in().y() + m_buffer.out().y()) / 2.,
135  (m_buffer.in().z() + m_buffer.out().z()) / 2.);
138  m_dedx_vs_eta->Fill(average.eta(), m_buffer.energyLoss(), 1.);
139  m_dedx_vs_z->Fill(average.z(), m_buffer.energyLoss(), 1.);
140  m_dedx_vs_r->Fill(average.perp(), m_buffer.energyLoss(), 1.);
141  m_radlen_vs_eta->Fill(average.eta(), m_buffer.radiationLengths(), 1.);
143  m_radlen_vs_r->Fill(average.perp(), m_buffer.radiationLengths(), 1.);
144  }
145  m_counted = false;
147 }
148 
150  m_file = std::make_unique<TFile>((m_name + ".root").c_str(), "RECREATE");
151  savePlot(m_dedx_spectrum, m_name + "_dedx_spectrum");
152  savePlot(m_radlen_spectrum, m_name + "_radlen_spectrum");
153  savePlot(m_dedx_vs_eta, averageEnergyLoss(), m_name + "_dedx_vs_eta");
154  savePlot(m_dedx_vs_z, averageEnergyLoss(), m_name + "_dedx_vs_z");
155  savePlot(m_dedx_vs_r, averageEnergyLoss(), m_name + "_dedx_vs_r");
156  savePlot(m_radlen_vs_eta, averageRadiationLengths(), m_name + "_radlen_vs_eta");
159  m_file->Write();
160  m_file->Close();
161 }
162 
163 void DD4hep_MaterialAccountingGroup::savePlot(std::shared_ptr<TH1F>& plot, const std::string& name) {
164  TCanvas canvas(name.c_str(), plot->GetTitle(), 1280, 1024);
165  plot->SetFillColor(15); // grey
166  plot->SetLineColor(1); // black
167  plot->Draw("c e");
168  canvas.GetFrame()->SetFillColor(kWhite);
169  canvas.Draw();
170  canvas.SaveAs((name + ".png").c_str(), "");
171  plot->SetDirectory(m_file.get());
172 }
173 
174 void DD4hep_MaterialAccountingGroup::savePlot(std::shared_ptr<TProfile>& plot, float average, const std::string& name) {
175  std::unique_ptr<TH1F> line = std::make_unique<TH1F>(
176  (name + "_par").c_str(), "Parametrization", 1, plot->GetXaxis()->GetXmin(), plot->GetXaxis()->GetXmax());
177 
178  line->SetBinContent(1, average);
179 
180  TCanvas canvas(name.c_str(), plot->GetTitle(), 1280, 1024);
181  plot->SetFillColor(15); // grey
182  plot->SetLineColor(1); // black
183  plot->SetLineWidth(2);
184  plot->Draw("c e6");
185  line->SetLineColor(2); // red
186  line->SetLineWidth(2);
187  line->Draw("same");
188  canvas.GetFrame()->SetFillColor(kWhite);
189  canvas.Draw();
190  canvas.SaveAs((name + ".png").c_str(), "");
191  plot->SetDirectory(m_file.get());
192  line->SetDirectory(m_file.get());
193  line->Write();
194 }
195 
197  std::stringstream out;
198  out << std::setw(48) << std::left << m_name << std::right << std::fixed;
199 
200  out << "BBox: " << std::setprecision(1) << std::setw(6) << m_boundingbox.range_z().first << " < Z < "
201  << std::setprecision(1) << std::setw(6) << m_boundingbox.range_z().second;
202  out << ", " << std::setprecision(1) << std::setw(5) << m_boundingbox.range_r().first << " < R < "
203  << std::setprecision(1) << std::setw(5) << m_boundingbox.range_r().second;
204  out << " Elements: " << std::setw(6) << m_elements.size();
205  return out.str();
206 }
207 
210 }
211 
213  return m_tracks ? m_accounting.length() / m_tracks : 0.;
214 }
215 
217  return m_tracks ? m_accounting.energyLoss() / m_tracks : 0.;
218 }
219 
222 }
223 
225  return m_tracks
227  : 0.;
228 }
229 
232 }
233 
236 }
svgfig.canvas
def canvas(*sub, **attr)
Definition: svgfig.py:482
alignBH_cfg.fixed
fixed
Definition: alignBH_cfg.py:54
mps_fire.i
i
Definition: mps_fire.py:428
DD4hep_MaterialAccountingGroup::m_elements
std::vector< GlobalPoint > m_elements
Definition: DD4hep_MaterialAccountingGroup.h:33
DD4hep_MaterialAccountingGroup::m_buffer
MaterialAccountingStep m_buffer
Definition: DD4hep_MaterialAccountingGroup.h:39
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
MaterialAccountingStep.h
PV3DBase::x
T x() const
Definition: PV3DBase.h:59
DD4hep_MaterialAccountingGroup::addDetector
bool addDetector(const MaterialAccountingDetector &detector)
Definition: DD4hep_MaterialAccountingGroup.cc:117
class-composition.children
children
Definition: class-composition.py:88
geometry
Definition: geometry.py:1
DD4hep_MaterialAccountingGroup::m_radlen_vs_eta
std::shared_ptr< TProfile > m_radlen_vs_eta
Definition: DD4hep_MaterialAccountingGroup.h:46
DD4hep_MaterialAccountingGroup::endOfTrack
void endOfTrack(void)
Definition: DD4hep_MaterialAccountingGroup.cc:127
DD4hep_MaterialAccountingGroup::sigmaRadiationLengths
double sigmaRadiationLengths(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:224
DD4hep_MaterialAccountingGroup::DD4hep_MaterialAccountingGroup
DD4hep_MaterialAccountingGroup(const DD4hep_MaterialAccountingGroup &layer)=delete
cms::DDFilteredView
Definition: DDFilteredView.h:70
MaterialAccountingStep::in
const GlobalPoint & in(void) const
Definition: MaterialAccountingStep.h:38
perp
T perp() const
Magnitude of transverse component.
Definition: Basic3DVectorLD.h:133
DD4hep_MaterialAccountingGroup::sigmaLength
double sigmaLength(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:220
plotFactory.plot
plot
Definition: plotFactory.py:109
DD4hep_MaterialAccountingGroup::m_name
std::string m_name
Definition: DD4hep_MaterialAccountingGroup.h:32
PV3DBase::z
T z() const
Definition: PV3DBase.h:61
DD4hep_MaterialAccountingGroup.h
DD4hep_MaterialAccountingGroup::m_counted
bool m_counted
Definition: DD4hep_MaterialAccountingGroup.h:38
cms::DDFilter
Definition: DDFilteredView.h:59
MaterialAccountingDetector
Definition: MaterialAccountingDetector.h:15
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
DDAxes::z
DD4hep_MaterialAccountingGroup::isInside
bool isInside(const MaterialAccountingDetector &detector) const
Definition: DD4hep_MaterialAccountingGroup.cc:87
DD4hep_MaterialAccountingGroup::s_tolerance
static constexpr double s_tolerance
Definition: DD4hep_MaterialAccountingGroup.h:52
DD4hep_MaterialAccountingGroup::info
std::string info(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:196
DDFilteredView.h
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
DD4hep_MaterialAccountingGroup::m_accounting
MaterialAccountingStep m_accounting
Definition: DD4hep_MaterialAccountingGroup.h:35
dqmdumpme.k
k
Definition: dqmdumpme.py:60
Point3DBase< float, GlobalTag >
ALCARECOTkAlBeamHalo_cff.filter
filter
Definition: ALCARECOTkAlBeamHalo_cff.py:27
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
runTauDisplay.gp
gp
Definition: runTauDisplay.py:431
DD4hep_MaterialAccountingGroup::averageLength
double averageLength(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:212
MaterialAccountingStep::out
const GlobalPoint & out(void) const
Definition: MaterialAccountingStep.h:40
DD4hep_MaterialAccountingGroup::m_boundingbox
BoundingBox m_boundingbox
Definition: DD4hep_MaterialAccountingGroup.h:34
DD4hep_MaterialAccountingGroup::sigmaEnergyLoss
double sigmaEnergyLoss(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:230
BoundingBox::range_r
std::pair< double, double > range_r() const
Definition: BoundingBox.h:27
position
static int position[264][3]
Definition: ReadPGInfo.cc:289
BoundingBox::range_z
std::pair< double, double > range_z() const
Definition: BoundingBox.h:29
PV3DBase::y
T y() const
Definition: PV3DBase.h:60
DD4hep_MaterialAccountingGroup::m_radlen_spectrum
std::shared_ptr< TH1F > m_radlen_spectrum
Definition: DD4hep_MaterialAccountingGroup.h:42
average
Definition: average.py:1
DD4hep_MaterialAccountingGroup::m_radlen_vs_r
std::shared_ptr< TProfile > m_radlen_vs_r
Definition: DD4hep_MaterialAccountingGroup.h:48
DD4hep_MaterialAccountingGroup::savePlots
void savePlots(void)
Definition: DD4hep_MaterialAccountingGroup.cc:149
DD4hep_MaterialAccountingGroup::average
MaterialAccountingStep average(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:208
DD4hep_MaterialAccountingGroup::averageEnergyLoss
double averageEnergyLoss(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:216
DD4hep_MaterialAccountingGroup::m_dedx_vs_z
std::shared_ptr< TProfile > m_dedx_vs_z
Definition: DD4hep_MaterialAccountingGroup.h:44
mag
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Definition: Basic3DVectorLD.h:127
BoundingBox::grow
void grow(const double &r, const double &z)
Definition: BoundingBox.cc:3
DD4hep_MaterialAccountingGroup::m_radlen_vs_z
std::shared_ptr< TProfile > m_radlen_vs_z
Definition: DD4hep_MaterialAccountingGroup.h:47
DD4hep_MaterialAccountingGroup::m_dedx_spectrum
std::shared_ptr< TH1F > m_dedx_spectrum
Definition: DD4hep_MaterialAccountingGroup.h:41
MaterialAccountingStep
Definition: MaterialAccountingStep.h:9
MaterialAccountingStep::energyLoss
double energyLoss(void) const
Definition: MaterialAccountingStep.h:36
MaterialAccountingStep::radiationLengths
double radiationLengths(void) const
Definition: MaterialAccountingStep.h:34
MaterialAccountingStep::length
double length(void) const
Definition: MaterialAccountingStep.h:32
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
BoundingBox::inside
bool inside(const double &r, const double &z) const
Definition: BoundingBox.h:23
DD4hep_MaterialAccountingGroup::m_tracks
unsigned int m_tracks
Definition: DD4hep_MaterialAccountingGroup.h:37
cms::DDCompactView
Definition: DDCompactView.h:31
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
DD4hep_MaterialAccountingGroup::averageRadiationLengths
double averageRadiationLengths(void) const
Definition: DD4hep_MaterialAccountingGroup.cc:234
DD4hep_MaterialAccountingGroup::m_dedx_vs_r
std::shared_ptr< TProfile > m_dedx_vs_r
Definition: DD4hep_MaterialAccountingGroup.h:45
DD4hep_MaterialAccountingGroup::m_dedx_vs_eta
std::shared_ptr< TProfile > m_dedx_vs_eta
Definition: DD4hep_MaterialAccountingGroup.h:43
hgcalTestNeighbor_cfi.detector
detector
Definition: hgcalTestNeighbor_cfi.py:6
MaterialAccountingDetector.h
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
DD4hep_MaterialAccountingGroup::m_file
std::unique_ptr< TFile > m_file
Definition: DD4hep_MaterialAccountingGroup.h:50
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
DD4hep_MaterialAccountingGroup::name
const std::string & name(void) const
Definition: DD4hep_MaterialAccountingGroup.h:71
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
mps_splice.line
line
Definition: mps_splice.py:76
Vector3D.h
cms::Translation
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > Translation
Definition: DDFilteredView.h:56
GlobalPoint.h
DD4hep_MaterialAccountingGroup::savePlot
void savePlot(std::shared_ptr< TH1F > &plot, const std::string &name)
Definition: DD4hep_MaterialAccountingGroup.cc:163
DD4hep_MaterialAccountingGroup::m_errors
MaterialAccountingStep m_errors
Definition: DD4hep_MaterialAccountingGroup.h:36