CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ListGroups.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <iostream>
3 #include <map>
4 #include <string>
5 #include <vector>
6 
7 #include <fmt/printf.h>
8 
9 // ROOT
10 #include <TCanvas.h>
11 #include <TColor.h>
12 #include <TFrame.h>
13 #include <TLegend.h>
14 #include <TLegendEntry.h>
15 #include <TLine.h>
16 #include <TProfile2D.h>
17 #include <TROOT.h>
18 #include <TStyle.h>
19 #include <TText.h>
20 
35 
38 
39 static bool dddGetStringRaw(const DDFilteredView &view, const std::string &name, std::string &value) {
40  std::vector<const DDsvalues_type *> result;
41  view.specificsV(result);
42  for (std::vector<const DDsvalues_type *>::iterator it = result.begin(); it != result.end(); ++it) {
43  DDValue parameter(name);
44  if (DDfetch(*it, parameter)) {
45  if (parameter.strings().size() == 1) {
46  value = parameter.strings().front();
47  return true;
48  } else {
49  throw cms::Exception("Configuration") << " ERROR: multiple " << name << " tags encountered";
50  return false;
51  }
52  }
53  }
54  return false;
55 }
56 
57 /*
58 static inline
59 double dddGetDouble(const std::string & s, DDFilteredView const & view) {
60  std::string value;
61  if (dddGetStringRaw(view, s, value))
62  return double(::atof(value.c_str()));
63  else
64  return NAN;
65 }
66 */
67 
68 static inline std::string dddGetString(const std::string &s, DDFilteredView const &view) {
70  if (dddGetStringRaw(view, s, value))
71  return value;
72  else
73  return std::string();
74 }
75 
76 class ListGroups : public edm::one::EDAnalyzer<> {
77 public:
79  ~ListGroups() override;
80 
81 private:
82  void analyze(const edm::Event &, const edm::EventSetup &) override;
83  void beginJob() override {}
84  void endJob() override;
85  void fillColor();
87  void fillGradient();
88  std::vector<std::pair<std::shared_ptr<TLine>, std::shared_ptr<TText> > > overlayEtaReferences();
91  std::vector<TH2F *> m_plots;
92  std::set<std::string> m_group_names;
93  std::vector<MaterialAccountingGroup *> m_groups;
94  std::vector<unsigned int> m_color;
95  std::vector<int> m_gradient;
96 
97  // The following maps are automatically filled by the script
98  // dumpFullXML, when run with -c,--compare flag, and are injected in
99  // this code via the header ListGroupsMaterialDifference.h, which is
100  // included below. The first value of the pair in m_diff represents
101  // the relative difference ((new - old)/old * 100, in %) of
102  // radiation length, while the second referes to the energy loss (in
103  // GeV/cm) changes. The values in m_values are ordered in the very
104  // same way, ie. they contain the new values for radiation length
105  // and energy loss, respectively.
106  std::map<std::string, std::pair<float, float> > m_diff;
107  std::map<std::string, std::pair<float, float> > m_values;
108 };
109 
111  m_saveSummaryPlot = iPSet.getUntrackedParameter<bool>("SaveSummaryPlot");
112  m_plots.clear();
113  m_groups.clear();
114  TColor::InitializeColors();
115  fillColor();
117  fillGradient();
118 }
119 
121  for (auto plot : m_plots)
122  delete plot;
123 
124  if (!m_groups.empty())
125  for (auto g : m_groups)
126  delete g;
127 }
128 
130 
132  m_gradient.reserve(200);
133  unsigned int steps = 100;
134  // if no index was given, find the highest used one and start from that plus one
135  unsigned int index = ((TObjArray *)gROOT->GetListOfColors())->GetLast() + 1;
136 
137  float r1, g1, b1, r2, g2, b2;
138  static_cast<TColor *>(gROOT->GetListOfColors()->At(kBlue + 1))->GetRGB(r1, g1, b1);
139  static_cast<TColor *>(gROOT->GetListOfColors()->At(kAzure + 10))->GetRGB(r2, g2, b2);
140  float delta_r = (r2 - r1) / (steps - 1);
141  float delta_g = (g2 - g1) / (steps - 1);
142  float delta_b = (b2 - b1) / (steps - 1);
143 
144  m_gradient.push_back(kBlue + 4); // Underflow lowest bin
145  unsigned int ii = 0;
146  for (unsigned int i = 0; i < steps; ++i, ++ii) {
147  new TColor(static_cast<Int_t>(index + ii), r1 + delta_r * i, g1 + delta_g * i, b1 + delta_b * i);
148  m_gradient.push_back(index + ii);
149  }
150 
151  m_gradient.push_back(kWhite); // 0 level perfectly white
152 
153  static_cast<TColor *>(gROOT->GetListOfColors()->At(kOrange))->GetRGB(r1, g1, b1);
154  static_cast<TColor *>(gROOT->GetListOfColors()->At(kOrange + 7))->GetRGB(r2, g2, b2);
155  delta_r = (r2 - r1) / (steps - 1);
156  delta_g = (g2 - g1) / (steps - 1);
157  delta_b = (b2 - b1) / (steps - 1);
158  for (unsigned int i = 0; i < steps; ++i, ++ii) {
159  new TColor(static_cast<Int_t>(index + ii), r1 + delta_r * i, g1 + delta_g * i, b1 + delta_b * i);
160  m_gradient.push_back(index + ii);
161  }
162  m_gradient.push_back(kRed); // Overflow highest bin
163 }
164 
166  // With the introduction of the support for PhaseI and PhaseII detectors it
167  // became quite difficult to maintain a list of colors that is in sync with
168  // the real number of grouping used in the different scenarios. We therefore
169  // define some reasonable set and loop over it in case the number of grouping
170  // is larger than the number of colors.
171 
172  m_color.push_back(kBlack); // unassigned
173 
174  m_color.push_back(kAzure); // PixelBarrelLayer0_Z0
175  m_color.push_back(kAzure - 1); // PixelBarrelLayer0_Z20
176  m_color.push_back(kAzure + 1); // Layer1_Z0
177  m_color.push_back(kAzure + 2); // Layer1_Z20
178 
179  m_color.push_back(kGreen); // EndCapDisk1_R0
180  m_color.push_back(kGreen + 2); // EndcapDisk1_R11
181  m_color.push_back(kGreen + 4); // EndcapDisk1_R7
182  m_color.push_back(kSpring + 9); // EndcapDisk2_R0
183  m_color.push_back(kSpring + 4); // EndcapDisk2_R7
184  m_color.push_back(kSpring); // EndcapDisk2_R7
185 
186  m_color.push_back(kRed); // TECDisk0_R20
187  m_color.push_back(kRed + 2); // TECDisk0_R40
188  m_color.push_back(kRed - 7); // TECDisk0_R50
189  m_color.push_back(kRed - 5); // TECDisk0_R60
190  m_color.push_back(kRed - 10); // TECDisk0_R90
191  m_color.push_back(kRed - 1); // TECDisk1_Inner
192  m_color.push_back(kRed - 2); // TECDisk1_Outer
193  m_color.push_back(kRed - 3); // TECDisk1_R20
194  m_color.push_back(kPink - 2); // TECDisk2_Inner
195  m_color.push_back(kPink - 3); // TECDisk2_Outer
196  m_color.push_back(kPink - 4); // TECDisk2_R20
197  m_color.push_back(kPink + 9); // TECDisk3_Inner
198  m_color.push_back(kPink + 8); // TECDisk3_Outer
199  m_color.push_back(kPink + 7); // TECDisk3
200  m_color.push_back(kMagenta - 2); // TECDisk4_Inner
201  m_color.push_back(kMagenta - 3); // TECDisk4_Outer
202  m_color.push_back(kMagenta - 4); // TECDisk4_R33
203  m_color.push_back(kMagenta - 5); // TECDisk5_Inner
204  m_color.push_back(kMagenta - 6); // TECDisk5_Outer
205  m_color.push_back(kMagenta - 7); // TECDisk5_R33
206  m_color.push_back(kRed); // TECDisk6
207  m_color.push_back(kMagenta - 9); // TECDisk7_R40
208  m_color.push_back(kViolet); // TECDisk8
209 
210  m_color.push_back(kOrange + 9); // TIBLayer0_Z0
211  m_color.push_back(kOrange + 7); // TIBLayer0_Z20
212  m_color.push_back(kOrange + 5); // TIBLayer0_Z40
213  m_color.push_back(kOrange - 2); // TIBLayer1_Z0
214  m_color.push_back(kOrange - 3); // TIBLayer1_Z30
215  m_color.push_back(kOrange - 6); // TIBLayer1_Z60
216  m_color.push_back(kOrange + 4); // TIBLayer2_Z0
217  m_color.push_back(kOrange - 7); // TIBLayer2_Z40
218  m_color.push_back(kOrange); // TIBLayer3_Z0
219  m_color.push_back(kOrange + 10); // TIBLayer3_Z50
220 
221  m_color.push_back(kViolet + 10); // TIDDisk1_R0
222  m_color.push_back(kViolet + 6); // TIDDisk1_R30
223  m_color.push_back(kViolet + 3); // TIDDisk1_R40
224  m_color.push_back(kViolet - 7); // TIDDisk2_R25
225  m_color.push_back(kViolet - 1); // TIDDisk2_R30
226  m_color.push_back(kViolet + 9); // TIDDisk2_R40
227  m_color.push_back(kViolet - 5); // TIDDisk3_R24
228  m_color.push_back(kViolet - 3); // TIDDisk3_R30
229  m_color.push_back(kViolet); // TIDDisk3_R40
230 
231  m_color.push_back(kAzure); // TOBLayer0_Z0
232  m_color.push_back(kAzure + 8); // TOBLayer0_Z20
233  m_color.push_back(kAzure + 2); // TOBLayer0_Z70
234  m_color.push_back(kAzure + 4); // TOBLayer0_Z80
235  m_color.push_back(kCyan + 1); // TOBLayer1_Z0
236  m_color.push_back(kCyan - 9); // TOBLayer1_Z20
237  m_color.push_back(kCyan + 3); // TOBLayer1_Z80
238  m_color.push_back(kCyan + 4); // TOBLayer1_Z90
239  m_color.push_back(kAzure); // TOBLayer2_Z0
240  m_color.push_back(kAzure + 8); // TOBLayer2_Z25
241  m_color.push_back(kAzure + 2); // TOBLayer2_Z80
242  m_color.push_back(kAzure + 5); // TOBLayer2_Z90
243  m_color.push_back(kCyan + 1); // TOBLayer3_Z0
244  m_color.push_back(kCyan - 9); // TOBLayer3_Z25
245  m_color.push_back(kCyan + 3); // TOBLayer3_Z80
246  m_color.push_back(kCyan + 4); // TOBLayer3_Z90
247  m_color.push_back(kAzure); // TOBLayer4_Z0
248  m_color.push_back(kAzure + 8); // TOBLayer4_Z25
249  m_color.push_back(kAzure + 2); // TOBLayer4_Z80
250  m_color.push_back(kAzure + 5); // TOBLayer4_Z90
251  m_color.push_back(kCyan + 1); // TOBLayer5_Z0
252  m_color.push_back(kCyan - 9); // TOBLayer5_Z25
253  m_color.push_back(kCyan + 3); // TOBLayer5_Z80
254  m_color.push_back(kCyan + 4); // TOBLayer5_Z90
255 }
256 
257 std::vector<std::pair<std::shared_ptr<TLine>, std::shared_ptr<TText> > > ListGroups::overlayEtaReferences() {
258  std::vector<std::pair<std::shared_ptr<TLine>, std::shared_ptr<TText> > > lines;
259 
260  lines.reserve(40);
261  std::pair<float, float> deltaZ(293, 298);
262  std::pair<float, float> deltaR(115, 118);
263  float text_size = 0.033;
264 
265  for (float eta = 0.; eta <= 3.8; eta += 0.2) {
266  float theta = 2. * atan(exp(-eta));
267  if (eta >= 1.8) {
268  lines.push_back(std::make_pair<std::shared_ptr<TLine>, std::shared_ptr<TText> >(
269  std::make_shared<TLine>(deltaZ.first, deltaZ.first * tan(theta), deltaZ.second, deltaZ.second * tan(theta)),
270  std::make_shared<TText>(deltaZ.first, deltaZ.first * tan(theta), fmt::sprintf("%2.1f", eta).c_str())));
271  lines.back().second->SetTextFont(42);
272  lines.back().second->SetTextSize(text_size);
273  lines.back().second->SetTextAlign(33);
274  lines.push_back(std::make_pair<std::shared_ptr<TLine>, std::shared_ptr<TText> >(
275  std::make_shared<TLine>(-deltaZ.first, deltaZ.first * tan(theta), -deltaZ.second, deltaZ.second * tan(theta)),
276  std::make_shared<TText>(-deltaZ.first, deltaZ.first * tan(theta), fmt::sprintf("-%2.1f", eta).c_str())));
277  lines.back().second->SetTextFont(42);
278  lines.back().second->SetTextSize(text_size);
279  lines.back().second->SetTextAlign(13);
280  } else {
281  lines.push_back(std::make_pair<std::shared_ptr<TLine>, std::shared_ptr<TText> >(
282  std::make_shared<TLine>(deltaR.first / tan(theta), deltaR.first, deltaR.second / tan(theta), deltaR.second),
283  std::make_shared<TText>(deltaR.first / tan(theta), deltaR.first, fmt::sprintf("%2.1f", eta).c_str())));
284  lines.back().second->SetTextFont(42);
285  lines.back().second->SetTextSize(text_size);
286  lines.back().second->SetTextAlign(23);
287  if (eta != 0) {
288  lines.push_back(std::make_pair<std::shared_ptr<TLine>, std::shared_ptr<TText> >(
289  std::make_shared<TLine>(
290  -deltaR.first / tan(theta), deltaR.first, -deltaR.second / tan(theta), deltaR.second),
291  std::make_shared<TText>(-deltaR.first / tan(theta), deltaR.first, fmt::sprintf("-%2.1f", eta).c_str())));
292  lines.back().second->SetTextFont(42);
293  lines.back().second->SetTextSize(text_size);
294  lines.back().second->SetTextAlign(23);
295  }
296  }
297  }
298  return lines;
299 }
300 
302  const double scale = 10.;
303  std::vector<TText *> nukem_text;
304  static int markerStyles[10] = {kFullCircle,
305  kFullSquare,
306  kFullTriangleUp,
307  kFullTriangleDown,
308  kOpenCircle,
309  kOpenSquare,
310  kOpenTriangleUp,
311  kOpenDiamond,
312  kOpenCross,
313  kFullStar};
314 
316  setup.get<IdealGeometryRecord>().get(hDdd);
317 
318  for (const auto &n : m_group_names) {
319  m_groups.push_back(new MaterialAccountingGroup(n, *hDdd));
320  };
321 
322  std::unique_ptr<TCanvas> canvas(
323  new TCanvas("Grouping_rz", "Grouping - RZ view", (int)(600 * scale * 1.25), (int)(120 * scale * 1.50)));
324  canvas->GetFrame()->SetFillColor(kWhite);
325  gStyle->SetOptStat(0);
326 
327  unsigned int color_index = 1;
328  // Setup the legend
329  std::unique_ptr<TLegend> leg(new TLegend(0.1, 0.1, 0.23, 0.34));
330  leg->SetHeader("Tracker Material Grouping");
331  leg->SetTextFont(42);
332  leg->SetTextSize(0.008);
333  leg->SetNColumns(3);
334  std::unique_ptr<TProfile2D> radlen(
335  new TProfile2D("OverallRadLen", "OverallRadLen", 600., -300., 300, 120., 0., 120.));
336  std::unique_ptr<TProfile2D> eneloss(
337  new TProfile2D("OverallEnergyLoss", "OverallEnergyLoss", 600., -300., 300, 120., 0., 120.));
338  std::unique_ptr<TProfile2D> radlen_diff(
339  new TProfile2D("OverallDifferencesRadLen", "OverallDifferencesRadLen", 600., -300., 300, 120., 0., 120.));
340  std::unique_ptr<TProfile2D> eneloss_diff(
341  new TProfile2D("OverallDifferencesEnergyLoss", "OverallDifferencesEnergyLoss", 600., -300., 300, 120., 0., 120.));
342 
343  for (auto g : m_groups) {
344  m_plots.push_back(
345  new TH2F(g->name().c_str(), g->name().c_str(), 6000., -300., 300, 1200., 0., 120.)); // 10x10 points per cm2
346  TH2F &current = *m_plots.back();
347  current.SetMarkerColor(m_color[color_index]);
348  current.SetMarkerStyle(markerStyles[color_index % 10]);
349  current.SetMarkerSize(0.8);
350  current.SetLineWidth(1);
351  for (const auto &element : g->elements()) {
352  current.Fill(element.z(), element.perp());
353  radlen->Fill(element.z(), element.perp(), m_values[g->name()].first);
354  eneloss->Fill(element.z(), element.perp(), m_values[g->name()].second);
355  radlen_diff->Fill(element.z(), element.perp(), m_diff[g->name()].first);
356  eneloss_diff->Fill(element.z(), element.perp(), m_diff[g->name()].second);
357  }
358 
359  if (color_index == 1)
360  current.Draw();
361  else
362  current.Draw("SAME");
363 
364  leg->AddEntry(&current, g->name().c_str(), "lp")->SetTextColor(m_color[color_index]);
365  color_index++;
366 
367  // Loop over the same chromatic scale in case the number of
368  // allocated colors is not big enough.
369  color_index = color_index % m_color.size();
370  }
371  leg->Draw();
372  canvas->SaveAs("Grouping.png");
373 
374  std::vector<std::pair<std::shared_ptr<TLine>, std::shared_ptr<TText> > > lines = overlayEtaReferences();
375 
376  canvas->Clear();
377  radlen->SetMinimum(0);
378  radlen->SetMaximum(0.25);
379  radlen->Draw("COLZ");
380  for (const auto &line : lines) {
381  line.first->SetLineWidth(5);
382  line.first->Draw();
383  line.second->Draw();
384  }
385  canvas->SaveAs("RadLenValues.png");
386 
387  canvas->Clear();
388  eneloss->SetMinimum(0.00001);
389  eneloss->SetMaximum(0.0005);
390  eneloss->Draw("COLZ");
391  for (const auto &line : lines) {
392  line.first->SetLineWidth(5);
393  line.first->Draw();
394  line.second->Draw();
395  }
396  canvas->SaveAs("EnergyLossValues.png");
397 
398  canvas->Clear();
399  gStyle->SetPalette(m_gradient.size(), &m_gradient.front());
400  gStyle->SetNumberContours(m_gradient.size());
401  radlen_diff->SetMinimum(-100);
402  radlen_diff->SetMaximum(100);
403  radlen_diff->Draw("COLZ");
404  for (const auto &line : lines) {
405  line.first->SetLineWidth(5);
406  line.first->Draw();
407  line.second->Draw();
408  }
409  canvas->SaveAs("RadLenChanges.png");
410 
411  canvas->Clear();
412  eneloss_diff->SetMinimum(-100);
413  eneloss_diff->SetMaximum(100);
414  eneloss_diff->Draw("COLZ");
415  for (const auto &line : lines) {
416  line.first->SetLineWidth(5);
417  line.first->Draw();
418  line.second->Draw();
419  }
420  canvas->SaveAs("EnergyLossChanges.png");
421 
422  for (auto g : nukem_text)
423  delete g;
424 }
425 
428  setup.get<IdealGeometryRecord>().get(hDdd);
429 
430  DDSpecificsHasNamedValueFilter filter{"TrackingMaterialGroup"};
431  DDFilteredView fv(*hDdd, filter);
432 
433  while (fv.next()) {
434  // print the group name and full hierarchy of all items
435  std::cout << dddGetString("TrackingMaterialGroup", fv) << '\t';
436  m_group_names.insert(dddGetString("TrackingMaterialGroup", fv));
437 
438  // start from 2 to skip the leading /OCMS[0]/CMSE[1] part
439  const DDGeoHistory &history = fv.geoHistory();
440  std::cout << '/';
441  for (unsigned int h = 2; h < history.size(); ++h)
442  std::cout << '/' << history[h].logicalPart().name().name() << '[' << history[h].copyno() << ']';
443 
444  // DD3Vector and DDTranslation are the same type as math::XYZVector
445  math::XYZVector position = fv.translation() / 10.; // mm -> cm
446  std::cout << "\t(" << position.x() << ", " << position.y() << ", " << position.z() << ") "
447  << "[rho] " << position.Rho() << std::endl;
448  };
449  std::cout << std::endl;
450 
451  if (m_saveSummaryPlot)
453 }
454 
456 
457 //-------------------------------------------------------------------------
458 // define as a plugin
T getUntrackedParameter(std::string const &, T const &) const
void endJob() override
Definition: ListGroups.cc:455
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void fillColor()
Definition: ListGroups.cc:165
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: ListGroups.cc:426
Geom::Theta< T > theta() const
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
int ii
Definition: cuy.py:589
const DDGeoHistory & geoHistory() const
The list of ancestors up to the root-node of the current node.
std::vector< TH2F * > m_plots
Definition: ListGroups.cc:91
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:79
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 g
Definition: Activities.doc:4
tuple result
Definition: mps_fire.py:311
def canvas
Definition: svgfig.py:482
void beginJob() override
Definition: ListGroups.cc:83
std::vector< unsigned int > m_color
Definition: ListGroups.cc:94
std::vector< std::pair< std::shared_ptr< TLine >, std::shared_ptr< TText > > > overlayEtaReferences()
Definition: ListGroups.cc:257
void produceAndSaveSummaryPlot(const edm::EventSetup &)
Definition: ListGroups.cc:301
bool next()
set current node to the next node in the filtered tree
def plot
Definition: bigModule.py:18
double delta_r(const Fourvec &a, const Fourvec &b)
Find the distance between two four-vectors in the two-dimensional space .
Definition: fourvec.cc:238
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
const std::vector< std::string > & strings() const
a reference to the std::string-valued values stored in the given instance of DDValue ...
Definition: DDValue.h:61
std::map< std::string, std::pair< float, float > > m_diff
Definition: ListGroups.cc:106
std::vector< DDExpandedNode > DDGeoHistory
Geometrical &#39;path&#39; of the current node up to the root-node.
bool m_saveSummaryPlot
Definition: ListGroups.cc:90
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
void fillGradient()
Definition: ListGroups.cc:131
std::set< std::string > m_group_names
Definition: ListGroups.cc:92
std::vector< MaterialAccountingGroup * > m_groups
Definition: ListGroups.cc:93
static int position[264][3]
Definition: ReadPGInfo.cc:289
ListGroups(const edm::ParameterSet &)
Definition: ListGroups.cc:110
T get() const
Definition: EventSetup.h:88
std::vector< int > m_gradient
Definition: ListGroups.cc:95
void specificsV(std::vector< const DDsvalues_type * > &result) const
User specific data attached to the current node.
tuple cout
Definition: gather_cfg.py:144
static bool dddGetStringRaw(const DDFilteredView &view, const std::string &name, std::string &value)
Definition: ListGroups.cc:39
const DDTranslation & translation() const
The absolute translation of the current node.
static constexpr float b2
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
~ListGroups() override
Definition: ListGroups.cc:120
static constexpr float b1
std::map< std::string, std::pair< float, float > > m_values
Definition: ListGroups.cc:107
static std::string dddGetString(const std::string &s, DDFilteredView const &view)
Definition: ListGroups.cc:68