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 }
MaterialAccountingGroup::m_buffer
MaterialAccountingStep m_buffer
Definition: MaterialAccountingGroup.h:148
svgfig.canvas
def canvas(*sub, **attr)
Definition: svgfig.py:482
MaterialAccountingGroup::name
const std::string & name(void) const
get the layer name
Definition: MaterialAccountingGroup.h:123
alignBH_cfg.fixed
fixed
Definition: alignBH_cfg.py:54
mps_fire.i
i
Definition: mps_fire.py:428
MaterialAccountingGroup::m_dedx_vs_z
TProfile * m_dedx_vs_z
Definition: MaterialAccountingGroup.h:155
funct::false
false
Definition: Factorize.h:29
MaterialAccountingStep.h
PV3DBase::x
T x() const
Definition: PV3DBase.h:59
MaterialAccountingGroup::m_tracks
unsigned int m_tracks
Definition: MaterialAccountingGroup.h:146
MaterialAccountingGroup::m_radlen_vs_r
TProfile * m_radlen_vs_r
Definition: MaterialAccountingGroup.h:159
MaterialAccountingGroup.h
DDFilteredView::logicalPart
const DDLogicalPart & logicalPart() const
The logical-part of the current node in the filtered-view.
Definition: DDFilteredView.cc:16
MaterialAccountingGroup::m_accounting
MaterialAccountingStep m_accounting
Definition: MaterialAccountingGroup.h:144
geometry
Definition: geometry.py:1
MaterialAccountingGroup::averageRadiationLengths
double averageRadiationLengths(void) const
return the average normalized number of radiation lengths
Definition: MaterialAccountingGroup.h:97
MaterialAccountingGroup::s_tolerance
static const double s_tolerance
Definition: MaterialAccountingGroup.h:165
MaterialAccountingStep::in
const GlobalPoint & in(void) const
Definition: MaterialAccountingStep.h:38
perp
T perp() const
Magnitude of transverse component.
Definition: Basic3DVectorLD.h:133
MaterialAccountingGroup::m_errors
MaterialAccountingStep m_errors
Definition: MaterialAccountingGroup.h:145
plotFactory.plot
plot
Definition: plotFactory.py:109
DDCompactView.h
MaterialAccountingGroup::average
MaterialAccountingStep average(void) const
return the average normalized material accounting informations
Definition: MaterialAccountingGroup.h:91
PV3DBase::z
T z() const
Definition: PV3DBase.h:61
MaterialAccountingGroup::m_file
TFile * m_file
Definition: MaterialAccountingGroup.h:162
DDFilteredView.h
MaterialAccountingGroup::inside
bool inside(const MaterialAccountingDetector &detector) const
check if detector is inside any part of this layer
Definition: MaterialAccountingGroup.cc:103
MaterialAccountingDetector
Definition: MaterialAccountingDetector.h:15
MaterialAccountingGroup::m_boundingbox
BoundingBox m_boundingbox
Definition: MaterialAccountingGroup.h:143
MaterialAccountingGroup::savePlot
void savePlot(TH1F *plot, const std::string &name)
Definition: MaterialAccountingGroup.cc:167
MaterialAccountingGroup::endOfTrack
void endOfTrack(void)
commit the buffer and reset the "already hit by this track" flag
Definition: MaterialAccountingGroup.cc:144
DDBase::toString
std::string toString() const
Definition: DDBase.h:63
DDFilteredView::next
bool next()
set current node to the next node in the filtered tree
Definition: DDFilteredView.cc:67
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
MaterialAccountingGroup::BoundingBox::range_z
std::pair< double, double > range_z() const
Definition: MaterialAccountingGroup.h:51
DDAxes::z
MaterialAccountingGroup::BoundingBox::range_r
std::pair< double, double > range_r() const
Definition: MaterialAccountingGroup.h:49
MaterialAccountingGroup::BoundingBox::grow
void grow(double r, double z)
Definition: MaterialAccountingGroup.h:29
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
Point3DBase< float, GlobalTag >
ALCARECOTkAlBeamHalo_cff.filter
filter
Definition: ALCARECOTkAlBeamHalo_cff.py:27
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
MaterialAccountingGroup::m_radlen_vs_z
TProfile * m_radlen_vs_z
Definition: MaterialAccountingGroup.h:158
MaterialAccountingStep::out
const GlobalPoint & out(void) const
Definition: MaterialAccountingStep.h:40
MaterialAccountingGroup::m_elements
std::vector< GlobalPoint > m_elements
Definition: MaterialAccountingGroup.h:142
MaterialAccountingGroup::addDetector
bool addDetector(const MaterialAccountingDetector &detector)
buffer material from a detector, if the detector is inside the DetLayer bounds
Definition: MaterialAccountingGroup.cc:132
MaterialAccountingGroup::m_radlen_vs_eta
TProfile * m_radlen_vs_eta
Definition: MaterialAccountingGroup.h:157
MaterialAccountingGroup::~MaterialAccountingGroup
~MaterialAccountingGroup(void)
destructor
Definition: MaterialAccountingGroup.cc:88
MaterialAccountingGroup::averageEnergyLoss
double averageEnergyLoss(void) const
return the average normalized energy loss density factor for Bethe-Bloch
Definition: MaterialAccountingGroup.h:100
math::XYZVector
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
position
static int position[264][3]
Definition: ReadPGInfo.cc:289
PV3DBase::y
T y() const
Definition: PV3DBase.h:60
MaterialAccountingGroup::info
std::string info(void) const
get some infos
Definition: MaterialAccountingGroup.cc:208
average
Definition: average.py:1
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
mag
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Definition: Basic3DVectorLD.h:127
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
DDValue
Definition: DDValue.h:21
DDFilteredView::translation
const DDTranslation & translation() const
The absolute translation of the current node.
Definition: DDFilteredView.cc:26
MaterialAccountingGroup::MaterialAccountingGroup
MaterialAccountingGroup(const std::string &name, const DDCompactView &geometry)
explicit constructors
Definition: MaterialAccountingGroup.cc:23
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
MaterialAccountingGroup::savePlots
void savePlots(void)
save the plots
Definition: MaterialAccountingGroup.cc:220
MaterialAccountingGroup::m_dedx_vs_r
TProfile * m_dedx_vs_r
Definition: MaterialAccountingGroup.h:156
DDSpecificsMatchesValueFilter
Definition: DDFilter.h:70
MaterialAccountingGroup::m_dedx_vs_eta
TProfile * m_dedx_vs_eta
Definition: MaterialAccountingGroup.h:154
hgcalTestNeighbor_cfi.detector
detector
Definition: hgcalTestNeighbor_cfi.py:6
DDFilteredView
Definition: DDFilteredView.h:20
MaterialAccountingDetector.h
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
MaterialAccountingGroup::m_dedx_spectrum
TH1F * m_dedx_spectrum
Definition: MaterialAccountingGroup.h:151
MaterialAccountingGroup::m_radlen_spectrum
TH1F * m_radlen_spectrum
Definition: MaterialAccountingGroup.h:152
LogTrace
#define LogTrace(id)
Definition: MessageLogger.h:224
MaterialAccountingGroup::m_name
std::string m_name
Definition: MaterialAccountingGroup.h:141
mps_splice.line
line
Definition: mps_splice.py:76
Vector3D.h
GlobalPoint.h
MaterialAccountingGroup::m_counted
bool m_counted
Definition: MaterialAccountingGroup.h:147
MaterialAccountingGroup::BoundingBox::inside
bool inside(double r, double z) const
Definition: MaterialAccountingGroup.h:47