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"));
33  m_groupNames = iPSet.getParameter<std::vector<std::string> >("Groups");
34  const std::string& splitmode = iPSet.getParameter<std::string>("SplitMode");
35  if (strcasecmp(splitmode.c_str(), "NearestLayer") == 0) {
37  } else if (strcasecmp(splitmode.c_str(), "InnerLayer") == 0) {
39  } else if (strcasecmp(splitmode.c_str(), "OuterLayer") == 0) {
41  } else {
44  << "Invalid SplitMode \"" << splitmode
45  << "\". Acceptable values are \"NearestLayer\", \"InnerLayer\", \"OuterLayer\".";
46  }
47  m_skipAfterLastDetector = iPSet.getParameter<bool>("SkipAfterLastDetector");
48  m_skipBeforeFirstDetector = iPSet.getParameter<bool>("SkipBeforeFirstDetector");
49  m_saveSummaryPlot = iPSet.getParameter<bool>("SaveSummaryPlot");
50  m_saveDetailedPlots = iPSet.getParameter<bool>("SaveDetailedPlots");
51  m_saveParameters = iPSet.getParameter<bool>("SaveParameters");
52  m_saveXml = iPSet.getParameter<bool>("SaveXML");
53  m_isHGCal = iPSet.getParameter<bool>("isHGCal");
54  m_isHFNose = iPSet.getParameter<bool>("isHFNose");
55  if (m_saveSummaryPlot) {
56  if (m_isHGCal) {
57  m_plotter = new TrackingMaterialPlotter(550., 300., 10);
58  } else if (m_isHFNose) {
59  m_plotter = new TrackingMaterialPlotter(1200., 350., 10);
60  } else {
61  m_plotter = new TrackingMaterialPlotter(300., 120., 10);
62  } // 10x10 points per cm2
63  } else {
64  m_plotter = nullptr;
65  }
66 }
67 
68 //-------------------------------------------------------------------------
70  if (m_plotter)
71  delete m_plotter;
72 }
73 
74 //-------------------------------------------------------------------------
76  std::ofstream parameters(name);
77  std::cout << std::endl;
78  for (unsigned int i = 0; i < m_groups.size(); ++i) {
79  MaterialAccountingGroup& layer = *(m_groups[i]);
80  std::cout << layer.name() << std::endl;
81  std::cout << fmt::sprintf("\tnumber of hits: %9d", layer.tracks()) << std::endl;
82  std::cout << fmt::sprintf(
83  "\tnormalized segment length: %9.1f ± %9.1f cm", layer.averageLength(), layer.sigmaLength())
84  << std::endl;
85  std::cout << fmt::sprintf("\tnormalized radiation lengths: %9.3f ± %9.3f",
87  layer.sigmaRadiationLengths())
88  << std::endl;
89  std::cout << fmt::sprintf("\tnormalized energy loss: %6.5fe-03 ± %6.5fe-03 GeV",
90  layer.averageEnergyLoss(),
91  layer.sigmaEnergyLoss())
92  << std::endl;
93  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",
94  layer.name(),
95  layer.tracks(),
96  layer.averageLength(),
97  layer.sigmaLength(),
99  layer.sigmaRadiationLengths(),
100  layer.averageEnergyLoss(),
101  layer.sigmaEnergyLoss())
102  << std::endl;
103  }
104  std::cout << std::endl;
105 
106  parameters.close();
107 }
108 
109 //-------------------------------------------------------------------------
111  std::ofstream xml(name);
112  xml << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl;
113  xml << "<Groups>" << std::endl;
114  for (unsigned int i = 0; i < m_groups.size(); ++i) {
115  MaterialAccountingGroup& layer = *(m_groups[i]);
116  xml << " <Group name=\"" << layer.name() << "\">\n"
117  << " <Parameter name=\"TrackerRadLength\" value=\"" << layer.averageRadiationLengths() << "\"/>\n"
118  << " <Parameter name=\"TrackerXi\" value=\"" << layer.averageEnergyLoss() << "\"/>\n"
119  << " </Group>\n"
120  << std::endl;
121  }
122  xml << "</Groups>" << std::endl;
123 }
124 
125 //-------------------------------------------------------------------------
127  for (unsigned int i = 0; i < m_groups.size(); ++i) {
128  MaterialAccountingGroup& layer = *(m_groups[i]);
129  layer.savePlots();
130  }
131 }
132 
133 //-------------------------------------------------------------------------
135  if (m_saveParameters)
136  saveParameters("parameters");
137 
138  if (m_saveXml)
139  saveXml("parameters.xml");
140 
142  saveLayerPlots();
143 
144  if (m_saveSummaryPlot and m_plotter) {
145  m_plotter->normalize();
146  m_plotter->draw();
147  }
148 }
149 
150 //-------------------------------------------------------------------------
151 
152 //-------------------------------------------------------------------------
154  using namespace edm;
156  setup.get<IdealGeometryRecord>().get(hDDD);
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
TrackingMaterialAnalyser::m_groupNames
std::vector< std::string > m_groupNames
Definition: TrackingMaterialAnalyser.h:46
MaterialAccountingGroup::name
const std::string & name(void) const
get the layer name
Definition: MaterialAccountingGroup.h:123
MuonGeometrySanityCheck_cfi.detectors
def detectors(dt=True, csc=True, me42=False, chambers=True, superlayers=False, layers=False)
Definition: MuonGeometrySanityCheck_cfi.py:13
alignBH_cfg.fixed
fixed
Definition: alignBH_cfg.py:54
BeamSpotPI::parameters
parameters
Definition: BeamSpotPayloadInspectorHelper.h:29
mps_fire.i
i
Definition: mps_fire.py:428
HLT_FULL_cff.track
track
Definition: HLT_FULL_cff.py:11713
ESTransientHandle.h
MaterialAccountingGroup::sigmaEnergyLoss
double sigmaEnergyLoss(void) const
return the sigma of the normalized energy loss density factor for Bethe-Bloch
Definition: MaterialAccountingGroup.h:115
MaterialAccountingStep.h
step
step
Definition: StallMonitor.cc:94
edm::errors::LogicError
Definition: EDMException.h:37
TrackingMaterialAnalyser::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: TrackingMaterialAnalyser.cc:153
MaterialAccountingGroup.h
edm
HLT enums.
Definition: AlignableModifier.h:19
gather_cfg.cout
cout
Definition: gather_cfg.py:144
TrackingMaterialAnalyser::saveLayerPlots
void saveLayerPlots()
Definition: TrackingMaterialAnalyser.cc:126
MaterialAccountingGroup::averageRadiationLengths
double averageRadiationLengths(void) const
return the average normalized number of radiation lengths
Definition: MaterialAccountingGroup.h:97
TrackingMaterialPlotter::plotSegmentUnassigned
void plotSegmentUnassigned(const MaterialAccountingStep &step)
Definition: TrackingMaterialPlotter.cc:125
TrackingMaterialAnalyser::findLayer
int findLayer(const MaterialAccountingDetector &detector)
Definition: TrackingMaterialAnalyser.cc:347
TrackingMaterialAnalyser::saveXml
void saveXml(const char *name)
Definition: TrackingMaterialAnalyser.cc:110
cms::cuda::assert
assert(be >=bs)
TrackingMaterialPlotter::draw
void draw(void)
Definition: TrackingMaterialPlotter.cc:147
MaterialAccountingGroup::sigmaRadiationLengths
double sigmaRadiationLengths(void) const
return the sigma of the normalized number of radiation lengths
Definition: MaterialAccountingGroup.h:108
TrackingMaterialAnalyser::OUTER_LAYER
Definition: TrackingMaterialAnalyser.h:22
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
TrackingMaterialPlotter::normalize
void normalize(void)
Definition: TrackingMaterialPlotter.h:24
edm::Handle
Definition: AssociativeIterator.h:50
DDCompactView.h
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
EDMException.h
MakerMacros.h
contentValuesFiles.parts
parts
Definition: contentValuesFiles.py:58
DDFilteredView.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
MaterialAccountingDetector
Definition: MaterialAccountingDetector.h:15
HLT_FULL_cff.fraction
fraction
Definition: HLT_FULL_cff.py:52795
MaterialAccountingTrack.h
mps_fire.end
end
Definition: mps_fire.py:242
TrackingMaterialPlotter.h
TrackingMaterialAnalyser::TrackingMaterialAnalyser
TrackingMaterialAnalyser(const edm::ParameterSet &)
Definition: TrackingMaterialAnalyser.cc:30
TrackingMaterialAnalyser::NEAREST_LAYER
Definition: TrackingMaterialAnalyser.h:22
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TrackingMaterialAnalyser::UNDEFINED
Definition: TrackingMaterialAnalyser.h:22
TrackingMaterialAnalyser::m_saveDetailedPlots
bool m_saveDetailedPlots
Definition: TrackingMaterialAnalyser.h:40
TrackingMaterialAnalyser::m_plotter
TrackingMaterialPlotter * m_plotter
Definition: TrackingMaterialAnalyser.h:47
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
MaterialAccountingGroup
Definition: MaterialAccountingGroup.h:19
MaterialAccountingGroup::averageEnergyLoss
double averageEnergyLoss(void) const
return the average normalized energy loss density factor for Bethe-Bloch
Definition: MaterialAccountingGroup.h:100
DDMaterial.h
TrackingMaterialPlotter
Definition: TrackingMaterialPlotter.h:16
ModuleDef.h
TrackingMaterialAnalyser::m_skipBeforeFirstDetector
bool m_skipBeforeFirstDetector
Definition: TrackingMaterialAnalyser.h:38
MaterialAccountingTrack
Definition: MaterialAccountingTrack.h:10
TrackingMaterialAnalyser::m_saveXml
bool m_saveXml
Definition: TrackingMaterialAnalyser.h:42
IdealGeometryRecord.h
TrackingMaterialAnalyser
Definition: TrackingMaterialAnalyser.h:16
edm::EventSetup
Definition: EventSetup.h:57
TrackingMaterialPlotter::plotSegmentInLayer
void plotSegmentInLayer(const MaterialAccountingStep &step, int layer)
Definition: TrackingMaterialPlotter.cc:136
get
#define get
MaterialAccountingGroup::sigmaLength
double sigmaLength(void) const
return the sigma of the normalized layer thickness
Definition: MaterialAccountingGroup.h:103
edm::ESTransientHandle
Definition: ESTransientHandle.h:41
MaterialAccountingGroup::tracks
unsigned int tracks(void) const
return the number of tracks that hit this layer
Definition: MaterialAccountingGroup.h:120
TH2PolyOfflineMaps.limits
limits
Definition: TH2PolyOfflineMaps.py:45
TrackingMaterialAnalyser::m_isHGCal
bool m_isHGCal
Definition: TrackingMaterialAnalyser.h:43
TrackingMaterialAnalyser::m_splitMode
SplitMode m_splitMode
Definition: TrackingMaterialAnalyser.h:36
TrackingMaterialAnalyser::INNER_LAYER
Definition: TrackingMaterialAnalyser.h:22
MaterialAccountingStep
Definition: MaterialAccountingStep.h:9
TrackingMaterialAnalyser::m_isHFNose
bool m_isHFNose
Definition: TrackingMaterialAnalyser.h:44
TrackingMaterialAnalyser::m_saveParameters
bool m_saveParameters
Definition: TrackingMaterialAnalyser.h:41
TrackingMaterialAnalyser::endJob
void endJob() override
Definition: TrackingMaterialAnalyser.cc:134
Exception
Definition: hltDiff.cc:246
TrackingMaterialAnalyser::m_materialToken
edm::EDGetTokenT< std::vector< MaterialAccountingTrack > > m_materialToken
Definition: TrackingMaterialAnalyser.h:35
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
or
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
MaterialAccountingGroup::savePlots
void savePlots(void)
save the plots
Definition: MaterialAccountingGroup.cc:220
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
TrackingMaterialAnalyser::saveParameters
void saveParameters(const char *name)
Definition: TrackingMaterialAnalyser.cc:75
TrackingMaterialAnalyser::m_skipAfterLastDetector
bool m_skipAfterLastDetector
Definition: TrackingMaterialAnalyser.h:37
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
MaterialAccountingGroup::averageLength
double averageLength(void) const
return the average normalized layer thickness
Definition: MaterialAccountingGroup.h:94
hgcalTestNeighbor_cfi.detector
detector
Definition: hgcalTestNeighbor_cfi.py:6
TrackingMaterialAnalyser::m_groups
std::vector< MaterialAccountingGroup * > m_groups
Definition: TrackingMaterialAnalyser.h:45
ParameterSet.h
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
edm::InputTag
Definition: InputTag.h:15
IdealGeometryRecord
Definition: IdealGeometryRecord.h:25
TrackingMaterialAnalyser::m_saveSummaryPlot
bool m_saveSummaryPlot
Definition: TrackingMaterialAnalyser.h:39
TrackingMaterialAnalyser.h
TrackingMaterialAnalyser::~TrackingMaterialAnalyser
~TrackingMaterialAnalyser() override
Definition: TrackingMaterialAnalyser.cc:69
TrackingMaterialAnalyser::split
void split(MaterialAccountingTrack &track)
Definition: TrackingMaterialAnalyser.cc:189
watchdog.group
group
Definition: watchdog.py:82