CMS 3D CMS Logo

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 
19 
21  0.01; // 100um should be small enough that no elements from different layers/groups are so close
22 
24  : m_name(name),
25  m_elements(),
26  m_boundingbox(),
27  m_accounting(),
28  m_errors(),
29  m_tracks(0),
30  m_counted(false),
31  m_file(nullptr) {
32  // retrieve the elements from DDD
33  DDValue namevalue;
34  if (TString(name.c_str()).Contains("Tracker")) {
35  namevalue = DDValue("TrackingMaterialGroup", name);
36  } else if (TString(name.c_str()).Contains("HGCal")) {
37  namevalue = DDValue("Volume", name);
38  } else if (TString(name.c_str()).Contains("HFNose")) {
39  namevalue = DDValue("Volume", name);
40  } else {
41  LogTrace("MaterialAccountingGroup") << name << std::endl;
42  edm::LogError("MaterialAccountingGroup") << "Only Tracker , HGCal and HFNose are supported" << std::endl;
43  }
44 
47  LogTrace("MaterialAccountingGroup") << "Elements within: " << name << std::endl;
48  while (fv.next()) {
49  // DD3Vector and DDTranslation are the same type as math::XYZVector
50  math::XYZVector position = fv.translation() / 10.; // mm -> cm
51  LogTrace("MaterialAccountingGroup") << "Adding element at(r,z): ("
52  << GlobalPoint(position.x(), position.y(), position.z()).perp() << ", "
53  << GlobalPoint(position.x(), position.y(), position.z()).z() << ") cm"
54  << std::endl;
55  LogTrace("MaterialAccountingGroup") << "Name of added element: " << fv.logicalPart().toString() << std::endl;
56  m_elements.push_back(GlobalPoint(position.x(), position.y(), position.z()));
57  }
58 
59  // grow the bounding box
60  for (unsigned int i = 0; i < m_elements.size(); ++i) {
62  }
64  LogTrace("MaterialAccountingGroup") << "Final BBox r_range: " << m_boundingbox.range_r().first << ", "
65  << m_boundingbox.range_r().second << std::endl
66  << "Final BBox z_range: " << m_boundingbox.range_z().first << ", "
67  << m_boundingbox.range_z().second << std::endl;
68 
69  // initialize the histograms
70  m_dedx_spectrum = new TH1F((m_name + "_dedx_spectrum").c_str(), "Energy loss spectrum", 1000, 0, 1);
71  m_radlen_spectrum = new TH1F((m_name + "_radlen_spectrum").c_str(), "Radiation lengths spectrum", 1000, 0, 1);
72  m_dedx_vs_eta = new TProfile((m_name + "_dedx_vs_eta").c_str(), "Energy loss vs. eta", 600, -3, 3);
73  m_dedx_vs_z = new TProfile((m_name + "_dedx_vs_z").c_str(), "Energy loss vs. Z", 6000, -300, 300);
74  m_dedx_vs_r = new TProfile((m_name + "_dedx_vs_r").c_str(), "Energy loss vs. R", 1200, 0, 120);
75  m_radlen_vs_eta = new TProfile((m_name + "_radlen_vs_eta").c_str(), "Radiation lengths vs. eta", 600, -3, 3);
76  m_radlen_vs_z = new TProfile((m_name + "_radlen_vs_z").c_str(), "Radiation lengths vs. Z", 6000, -300, 300);
77  m_radlen_vs_r = new TProfile((m_name + "_radlen_vs_r").c_str(), "Radiation lengths vs. R", 1200, 0, 120);
78  m_dedx_spectrum->SetDirectory(nullptr);
79  m_radlen_spectrum->SetDirectory(nullptr);
80  m_dedx_vs_eta->SetDirectory(nullptr);
81  m_dedx_vs_z->SetDirectory(nullptr);
82  m_dedx_vs_r->SetDirectory(nullptr);
83  m_radlen_vs_eta->SetDirectory(nullptr);
84  m_radlen_vs_z->SetDirectory(nullptr);
85  m_radlen_vs_r->SetDirectory(nullptr);
86 }
87 
89  delete m_dedx_spectrum;
90  delete m_dedx_vs_eta;
91  delete m_dedx_vs_z;
92  delete m_dedx_vs_r;
93  delete m_radlen_spectrum;
94  delete m_radlen_vs_eta;
95  delete m_radlen_vs_z;
96  delete m_radlen_vs_r;
97 }
98 
99 // TODO the inner check could be sped up in many ways
100 // (sorting the m_elements, partitioning the bounding box, ...)
101 // but is it worth?
102 // especially with the segmentation of the layers ?
104  const GlobalPoint& position = detector.position();
105  // first check to see if the point is inside the bounding box
106  LogTrace("MaterialAccountingGroup") << "Testing position: (x, y, z, r) = " << position.x() << ", " << position.y()
107  << ", " << position.z() << ", " << position.perp() << std::endl;
108  if (not m_boundingbox.inside(position.perp(), position.z())) {
109  LogTrace("MaterialAccountingGroup") << "r outside of: (" << m_boundingbox.range_r().first << ", "
110  << m_boundingbox.range_r().second << "), Z ouside of: ("
111  << m_boundingbox.range_z().first << ", " << m_boundingbox.range_z().second
112  << ")" << std::endl;
113  return false;
114  } else {
115  // now check if the point is actually close enough to any element
116  LogTrace("MaterialAccountingGroup") << "r within: (" << m_boundingbox.range_r().first << ", "
117  << m_boundingbox.range_r().second << "), Z within: ("
118  << m_boundingbox.range_z().first << ", " << m_boundingbox.range_z().second
119  << ")" << std::endl;
120  for (unsigned int i = 0; i < m_elements.size(); ++i) {
121  LogTrace("MaterialAccountingGroup")
122  << "Closest testing agains(x, y, z, r): (" << m_elements[i].x() << ", " << m_elements[i].y() << ", "
123  << m_elements[i].z() << ", " << m_elements[i].perp() << ") --> " << (position - m_elements[i]).mag()
124  << " vs tolerance: " << s_tolerance << std::endl;
125  if ((position - m_elements[i]).mag2() < (s_tolerance * s_tolerance))
126  return true;
127  }
128  return false;
129  }
130 }
131 
133  if (not inside(detector))
134  return false;
135 
136  // multiple hits in the same layer (overlaps, etc.) from a single track still count as one for averaging,
137  // since the energy deposits from the track have been already split between the different detectors
138  m_buffer += detector.material();
139  m_counted = true;
140 
141  return true;
142 }
143 
145  // add a detector
146  if (m_counted) {
149  ++m_tracks;
150 
151  GlobalPoint average((m_buffer.in().x() + m_buffer.out().x()) / 2.,
152  (m_buffer.in().y() + m_buffer.out().y()) / 2.,
153  (m_buffer.in().z() + m_buffer.out().z()) / 2.);
156  m_dedx_vs_eta->Fill(average.eta(), m_buffer.energyLoss(), 1.);
157  m_dedx_vs_z->Fill(average.z(), m_buffer.energyLoss(), 1.);
158  m_dedx_vs_r->Fill(average.perp(), m_buffer.energyLoss(), 1.);
159  m_radlen_vs_eta->Fill(average.eta(), m_buffer.radiationLengths(), 1.);
161  m_radlen_vs_r->Fill(average.perp(), m_buffer.radiationLengths(), 1.);
162  }
163  m_counted = false;
165 }
166 
168  TCanvas canvas(name.c_str(), plot->GetTitle(), 1280, 1024);
169  plot->SetFillColor(15); // grey
170  plot->SetLineColor(1); // black
171  plot->Draw("c e");
172  canvas.GetFrame()->SetFillColor(kWhite);
173  canvas.Draw();
174  canvas.SaveAs((name + ".png").c_str(), "");
175 
176  // store te plot into m_file
177  plot->SetDirectory(m_file);
178 }
179 
181  // Nota Bene:
182  // these "line" plots are not deleted explicitly since
183  // - deleting them before saving them to a TFile will not save them
184  // - deleting them after the TFile they're stored into results in a SEGV
185  // ROOT is probably "taking care" (read: messing things up) somehow...
186  TH1F* line =
187  new TH1F((name + "_par").c_str(), "Parametrization", 1, plot->GetXaxis()->GetXmin(), plot->GetXaxis()->GetXmax());
188  line->SetBinContent(1, average);
189 
190  TCanvas canvas(name.c_str(), plot->GetTitle(), 1280, 1024);
191  plot->SetFillColor(15); // grey
192  plot->SetLineColor(1); // black
193  plot->SetLineWidth(2);
194  plot->Draw("c e6");
195  line->SetLineColor(2); // red
196  line->SetLineWidth(2);
197  line->Draw("same");
198  canvas.GetFrame()->SetFillColor(kWhite);
199  canvas.Draw();
200  canvas.SaveAs((name + ".png").c_str(), "");
201 
202  // store te plots into m_file
203  plot->SetDirectory(m_file);
204  line->SetDirectory(m_file);
205 }
206 
209  std::stringstream out;
210  out << std::setw(48) << std::left << m_name << std::right << std::fixed;
211  ;
212  out << "BBox: " << std::setprecision(1) << std::setw(6) << m_boundingbox.range_z().first << " < Z < "
213  << std::setprecision(1) << std::setw(6) << m_boundingbox.range_z().second;
214  out << ", " << std::setprecision(1) << std::setw(5) << m_boundingbox.range_r().first << " < R < "
215  << std::setprecision(1) << std::setw(5) << m_boundingbox.range_r().second;
216  out << " Elements: " << std::setw(6) << m_elements.size();
217  return out.str();
218 }
219 
221  m_file = new TFile((m_name + ".root").c_str(), "RECREATE");
222  savePlot(m_dedx_spectrum, m_name + "_dedx_spectrum");
223  savePlot(m_radlen_spectrum, m_name + "_radlen_spectrum");
224  savePlot(m_dedx_vs_eta, averageEnergyLoss(), m_name + "_dedx_vs_eta");
225  savePlot(m_dedx_vs_z, averageEnergyLoss(), m_name + "_dedx_vs_z");
226  savePlot(m_dedx_vs_r, averageEnergyLoss(), m_name + "_dedx_vs_r");
227  savePlot(m_radlen_vs_eta, averageRadiationLengths(), m_name + "_radlen_vs_eta");
230  m_file->Write();
231  m_file->Close();
232 
233  delete m_file;
234 }
double averageRadiationLengths(void) const
return the average normalized number of radiation lengths
bool inside(const MaterialAccountingDetector &detector) const
check if detector is inside any part of this layer
MaterialAccountingGroup(const std::string &name, const DDCompactView &geometry)
explicit constructors
void endOfTrack(void)
commit the buffer and reset the "already hit by this track" flag
MaterialAccountingStep m_errors
T z() const
Definition: PV3DBase.h:61
MaterialAccountingStep m_buffer
std::vector< GlobalPoint > m_elements
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
Log< level::Error, false > LogError
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
const GlobalPoint & in(void) const
#define LogTrace(id)
std::pair< double, double > range_r() const
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
bool next()
set current node to the next node in the filtered tree
void savePlot(TH1F *plot, const std::string &name)
std::string info(void) const
get some infos
bool inside(double r, double z) const
std::string toString() const
Definition: DDBase.h:63
T perp() const
Magnitude of transverse component.
std::pair< double, double > range_z() const
const DDLogicalPart & logicalPart() const
The logical-part of the current node in the filtered-view.
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
void savePlots(void)
save the plots
double averageEnergyLoss(void) const
return the average normalized energy loss density factor for Bethe-Bloch
const GlobalPoint & out(void) const
static int position[264][3]
Definition: ReadPGInfo.cc:289
def canvas(sub, attr)
Definition: svgfig.py:482
bool addDetector(const MaterialAccountingDetector &detector)
buffer material from a detector, if the detector is inside the DetLayer bounds
~MaterialAccountingGroup(void)
destructor
double radiationLengths(void) const
MaterialAccountingStep m_accounting
const std::string & name(void) const
get the layer name
const DDTranslation & translation() const
The absolute translation of the current node.
double energyLoss(void) const
MaterialAccountingStep average(void) const
return the average normalized material accounting informations