CMS 3D CMS Logo

TrackingMaterialAnalyser.cc
Go to the documentation of this file.
1 #include <iostream> // FIXME: switch to MessagLogger & friends
2 #include <fstream>
3 #include <sstream>
4 #include <vector>
5 #include <string>
6 #include <stdexcept>
7 #include <cstring>
8 #include <cstdlib>
9 
10 #include <fmt/printf.h>
11 
17 
22 
28 
29 //-------------------------------------------------------------------------
32  consumes<std::vector<MaterialAccountingTrack> >(iPSet.getParameter<edm::InputTag>("MaterialAccounting"));
34  m_groupNames = iPSet.getParameter<std::vector<std::string> >("Groups");
35  const std::string& splitmode = iPSet.getParameter<std::string>("SplitMode");
36  if (strcasecmp(splitmode.c_str(), "NearestLayer") == 0) {
38  } else if (strcasecmp(splitmode.c_str(), "InnerLayer") == 0) {
40  } else if (strcasecmp(splitmode.c_str(), "OuterLayer") == 0) {
42  } else {
45  << "Invalid SplitMode \"" << splitmode
46  << "\". Acceptable values are \"NearestLayer\", \"InnerLayer\", \"OuterLayer\".";
47  }
48  m_skipAfterLastDetector = iPSet.getParameter<bool>("SkipAfterLastDetector");
49  m_skipBeforeFirstDetector = iPSet.getParameter<bool>("SkipBeforeFirstDetector");
50  m_saveSummaryPlot = iPSet.getParameter<bool>("SaveSummaryPlot");
51  m_saveDetailedPlots = iPSet.getParameter<bool>("SaveDetailedPlots");
52  m_saveParameters = iPSet.getParameter<bool>("SaveParameters");
53  m_saveXml = iPSet.getParameter<bool>("SaveXML");
54  m_isHGCal = iPSet.getParameter<bool>("isHGCal");
55  m_isHFNose = iPSet.getParameter<bool>("isHFNose");
56  if (m_saveSummaryPlot) {
57  if (m_isHGCal) {
58  m_plotter = new TrackingMaterialPlotter(550., 300., 10);
59  } else if (m_isHFNose) {
60  m_plotter = new TrackingMaterialPlotter(1200., 350., 10);
61  } else {
62  m_plotter = new TrackingMaterialPlotter(300., 120., 10);
63  } // 10x10 points per cm2
64  } else {
65  m_plotter = nullptr;
66  }
67 }
68 
69 //-------------------------------------------------------------------------
71  if (m_plotter)
72  delete m_plotter;
73 }
74 
75 //-------------------------------------------------------------------------
77  std::ofstream parameters(name);
78  std::cout << std::endl;
79  for (unsigned int i = 0; i < m_groups.size(); ++i) {
81  std::cout << layer.name() << std::endl;
82  std::cout << fmt::sprintf("\tnumber of hits: %9d", layer.tracks()) << std::endl;
83  std::cout << fmt::sprintf(
84  "\tnormalized segment length: %9.1f ± %9.1f cm", layer.averageLength(), layer.sigmaLength())
85  << std::endl;
86  std::cout << fmt::sprintf("\tnormalized radiation lengths: %9.3f ± %9.3f",
87  layer.averageRadiationLengths(),
88  layer.sigmaRadiationLengths())
89  << std::endl;
90  std::cout << fmt::sprintf("\tnormalized energy loss: %6.5fe-03 ± %6.5fe-03 GeV",
91  layer.averageEnergyLoss(),
92  layer.sigmaEnergyLoss())
93  << std::endl;
94  parameters << fmt::sprintf("%-20s\t%7d\t%5.1f ± %5.1f cm\t%6.4f ± %6.4f \t%6.4fe-03 ± %6.4fe-03 GeV",
95  layer.name(),
96  layer.tracks(),
97  layer.averageLength(),
98  layer.sigmaLength(),
99  layer.averageRadiationLengths(),
100  layer.sigmaRadiationLengths(),
101  layer.averageEnergyLoss(),
102  layer.sigmaEnergyLoss())
103  << std::endl;
104  }
105  std::cout << std::endl;
106 
107  parameters.close();
108 }
109 
110 //-------------------------------------------------------------------------
112  std::ofstream xml(name);
113  xml << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl;
114  xml << "<Groups>" << std::endl;
115  for (unsigned int i = 0; i < m_groups.size(); ++i) {
117  xml << " <Group name=\"" << layer.name() << "\">\n"
118  << " <Parameter name=\"TrackerRadLength\" value=\"" << layer.averageRadiationLengths() << "\"/>\n"
119  << " <Parameter name=\"TrackerXi\" value=\"" << layer.averageEnergyLoss() << "\"/>\n"
120  << " </Group>\n"
121  << std::endl;
122  }
123  xml << "</Groups>" << std::endl;
124 }
125 
126 //-------------------------------------------------------------------------
128  for (unsigned int i = 0; i < m_groups.size(); ++i) {
130  layer.savePlots();
131  }
132 }
133 
134 //-------------------------------------------------------------------------
136  if (m_saveParameters)
137  saveParameters("parameters");
138 
139  if (m_saveXml)
140  saveXml("parameters.xml");
141 
143  saveLayerPlots();
144 
145  if (m_saveSummaryPlot and m_plotter) {
146  m_plotter->normalize();
147  m_plotter->draw();
148  }
149 }
150 
151 //-------------------------------------------------------------------------
152 
153 //-------------------------------------------------------------------------
155  using namespace edm;
156  auto hDDD = setup.getHandle(m_dddToken);
157 
158  m_groups.reserve(m_groupNames.size());
159  // Initialize m_groups iff it has size equal to zero, so that we are
160  // sure it will never be repopulated with the same entries over and
161  // over again in the eventloop, at each call of the analyze method.
162  if (m_groups.empty()) {
163  for (unsigned int i = 0; i < m_groupNames.size(); ++i)
164  m_groups.push_back(new MaterialAccountingGroup(m_groupNames[i], *hDDD));
165 
166  LogInfo("TrackingMaterialAnalyser") << "TrackingMaterialAnalyser: List of the tracker groups: " << std::endl;
167  for (unsigned int i = 0; i < m_groups.size(); ++i)
168  LogInfo("TrackingMaterialAnalyser") << i << " TrackingMaterialAnalyser:\t" << m_groups[i]->info() << std::endl;
169  }
171  event.getByToken(m_materialToken, h_tracks);
172 
173  for (std::vector<MaterialAccountingTrack>::const_iterator t = h_tracks->begin(), end = h_tracks->end(); t != end;
174  ++t) {
176  split(track);
177  }
178 }
179 
180 //-------------------------------------------------------------------
181 // split a track in segments, each associated to a sensitive detector
182 // in a DetLayer; then, associate each step to one segment, splitting
183 // the steps across the segment boundaries
184 //
185 // Nota Bene: this implementation assumes that the steps stored along
186 // each track are consecutive and adjacent, and that no step can span
187 // across 3 layers, since all steps should split at layer boundaries
188 
190  using namespace edm;
191  // group sensitive detectors by their DetLayer
192  std::vector<int> group(track.detectors().size());
193  for (unsigned int i = 0; i < track.detectors().size(); ++i)
194  group[i] = findLayer(track.detectors()[i]);
195 
196  for (unsigned int i = 0; i < group.size(); ++i)
197  if (group[i] > 0)
198  LogInfo("TrackingMaterialAnalyser") << "For detector i: " << i << " index: " << group[i]
199  << " R-ranges: " << m_groups[group[i] - 1]->getBoundingR().first << ", "
200  << m_groups[group[i] - 1]->getBoundingR().second << group[i]
201  << " Z-ranges: " << m_groups[group[i] - 1]->getBoundingZ().first << ", "
202  << m_groups[group[i] - 1]->getBoundingZ().second << std::endl;
203 
204  unsigned int detectors = track.detectors().size();
205  if (detectors == 0) {
206  // the track doesn't cross any active detector:
207  // keep al material as unassigned
208  if (m_plotter)
209  for (unsigned int i = 1; i < track.steps().size(); ++i)
211  } else {
212  const double TOLERANCE = 0.0001; // 1 um tolerance
213  std::vector<double> limits(detectors + 2);
214 
215  // define the trivial limits
217  limits[0] = track.detectors()[0].m_curvilinearIn - TOLERANCE;
218  else
219  limits[0] = -TOLERANCE;
221  limits[detectors] = track.detectors()[detectors - 1].m_curvilinearOut + TOLERANCE;
222  else
223  limits[detectors] = track.summary().length() + TOLERANCE;
224  limits[detectors + 1] = INFINITY; // this is probably no more needed, but doesn't harm...
225 
226  // pick the algorithm to define the non-trivial limits
227  switch (m_splitMode) {
228  // assign each segment to the the nearest layer
229  // e.g. the material between pixel barrel 3 and TIB 1 will be split among the two
230  case NEAREST_LAYER:
231  for (unsigned int i = 1; i < detectors; ++i) {
232  limits[i] = (track.detectors()[i - 1].m_curvilinearOut + track.detectors()[i].m_curvilinearIn) / 2.;
233  }
234  break;
235 
236  // assign each segment to the the inner layer
237  // e.g. all material between pixel barrel 3 and TIB 1 will go into the pixel barrel
238  case INNER_LAYER:
239  for (unsigned int i = 1; i < detectors; ++i)
240  limits[i] = track.detectors()[i].m_curvilinearIn - TOLERANCE;
241  break;
242 
243  // assign each segment to the the outer layer
244  // e.g. all material between pixel barrel 3 and TIB 1 will go into the TIB
245  case OUTER_LAYER:
246  for (unsigned int i = 1; i < detectors; ++i)
247  limits[i] = track.detectors()[i - 1].m_curvilinearOut + TOLERANCE;
248  break;
249 
250  case UNDEFINED:
251  [[fallthrough]];
252 
253  default:
254  // throw something
255  throw edm::Exception(edm::errors::LogicError) << "Invalid SplitMode";
256  }
257 
258  double begin = 0.; // beginning of step, along the track
259  double end = 0.; // end of step, along the track
260  unsigned int i = 1; // step conter
261 
262  // skip the material before the first layer
263  while (end < limits[0]) {
264  const MaterialAccountingStep& step = track.steps()[i++];
265  end = begin + step.length();
266 
267  // do not account material before the first layer
268  if (m_plotter)
270 
271  begin = end;
272  }
273 
274  unsigned int index = 0; // which detector
275  while (i < track.steps().size()) {
276  const MaterialAccountingStep& step = track.steps()[i++];
277 
278  end = begin + step.length();
279 
280  if (begin > limits[detectors]) {
281  // segment after last layer and skipping requested in configuation
282  if (m_plotter)
284  begin = end;
285  continue;
286  }
287 
288  // from here onwards we should be in the accountable region, either completely in a single layer:
289  // limits[index] <= begin < end <= limits[index+1]
290  // or possibly split between 2 layers
291  // limits[index] < begin < limits[index+1] < end < limits[index+2]
292  if (begin < limits[index] or end > limits[index + 2]) {
293  // sanity check
294  std::cerr << "MaterialAccountingTrack::split(): ERROR: internal logic error, expected " << limits[index]
295  << " < " << begin << " < " << limits[index + 1] << std::endl;
296  break;
297  }
298 
299  if (limits[index] <= begin and end <= limits[index + 1]) {
300  // step completely inside current detector range
301  track.detectors()[index].account(step, begin, end);
302  if (m_plotter)
304  } else {
305  // step shared beteewn two detectors, transition at limits[index+1]
306  double fraction = (limits[index + 1] - begin) / (end - begin);
307  assert(fraction < 1.);
308  std::pair<MaterialAccountingStep, MaterialAccountingStep> parts = step.split(fraction);
309 
310  if (m_plotter) {
311  if (index > 0)
313  else
314  // track outside acceptance, keep as unassocated
316 
317  if (index + 1 < detectors)
319  else
320  // track outside acceptance, keep as unassocated
322  }
323 
324  track.detectors()[index].account(parts.first, begin, limits[index + 1]);
325  ++index; // next layer
326  if (index < detectors)
327  track.detectors()[index].account(parts.second, limits[index + 1], end);
328  }
329  begin = end;
330  }
331  }
332 
333  // add the material from each detector to its layer (if there is one and only one)
334  for (unsigned int i = 0; i < track.detectors().size(); ++i)
335  if (group[i] != 0)
336  m_groups[group[i] - 1]->addDetector(track.detectors()[i]);
337 
338  // end of track: commit internal buffers and reset the m_groups internal state for a new track
339  for (unsigned int i = 0; i < m_groups.size(); ++i)
340  m_groups[i]->endOfTrack();
341 }
342 
343 //-------------------------------------------------------------------------
344 // find the layer index (0: none, 1-3: PixelBarrel,
345 // 4-7: TID, 8-13: TOB, 14-15,28-29: PixelEndcap,
346 // 16-18,30-32: TID, 19-27,33-41: TEC)
348  int index = 0;
349  size_t inside = 0;
350  for (size_t i = 0; i < m_groups.size(); ++i)
351  if (m_groups[i]->inside(detector)) {
352  ++inside;
353  index = i + 1;
354  }
355  if (inside == 0) {
356  index = 0;
357  std::cerr << "TrackingMaterialAnalyser::findLayer(...): ERROR: detector does not belong to any DetLayer"
358  << std::endl;
359  std::cerr << "TrackingMaterialAnalyser::findLayer(...): detector position: " << std::fixed
360  << " (r: " << std::setprecision(1) << std::setw(5) << detector.position().perp()
361  << ", z: " << std::setprecision(1) << std::setw(6) << detector.position().z()
362  << ", phi: " << std::setprecision(3) << std::setw(6) << detector.position().phi() << ")" << std::endl;
363  }
364  if (inside > 1) {
365  index = 0;
366  std::cerr << "TrackingMaterialAnalyser::findLayer(...): ERROR: detector belongs to " << inside << " DetLayers"
367  << std::endl;
368  std::cerr << "TrackingMaterialAnalyser::findLayer(...): detector position: " << std::fixed
369  << " (r: " << std::setprecision(1) << std::setw(5) << detector.position().perp()
370  << ", z: " << std::setprecision(1) << std::setw(6) << detector.position().z()
371  << ", phi: " << std::setprecision(3) << std::setw(6) << detector.position().phi() << ")" << std::endl;
372  }
373 
374  return index;
375 }
376 
377 //-------------------------------------------------------------------------
378 // define as a plugin
void plotSegmentUnassigned(const MaterialAccountingStep &step)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
assert(be >=bs)
void split(MaterialAccountingTrack &track)
constexpr std::array< uint8_t, layerIndexSize > layer
void saveParameters(const char *name)
int findLayer(const MaterialAccountingDetector &detector)
def detectors(dt=True, csc=True, me42=False, chambers=True, superlayers=False, layers=False)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void analyze(const edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< std::vector< MaterialAccountingTrack > > m_materialToken
Log< level::Info, false > LogInfo
std::vector< std::string > m_groupNames
TrackingMaterialPlotter * m_plotter
std::vector< MaterialAccountingGroup * > m_groups
HLT enums.
TrackingMaterialAnalyser(const edm::ParameterSet &)
void plotSegmentInLayer(const MaterialAccountingStep &step, int layer)
step
Definition: StallMonitor.cc:98
edm::ESGetToken< DDCompactView, IdealGeometryRecord > m_dddToken
Definition: event.py:1