CMS 3D CMS Logo

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