Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:03

0001 #include <iostream>
0002 
0003 #include <TROOT.h>
0004 #include <TObjArray.h>
0005 #include <TColor.h>
0006 #include <TStyle.h>
0007 #include <TCanvas.h>
0008 #include <TFrame.h>
0009 #include "DD4hep_XHistogram.h"
0010 #include "DD4hep_TrackingMaterialPlotter.h"
0011 
0012 #include "SimDataFormats/ValidationFormats/interface/MaterialAccountingStep.h"
0013 
0014 void DD4hep_TrackingMaterialPlotter::fill_color(void) {
0015   m_color.emplace_back(kBlack);        // unassigned
0016   m_color.emplace_back(kAzure);        // PixelBarrel
0017   m_color.emplace_back(kAzure + 1);    //
0018   m_color.emplace_back(kAzure + 1);    //
0019   m_color.emplace_back(kAzure + 3);    //
0020   m_color.emplace_back(kAzure + 3);    //
0021   m_color.emplace_back(kGreen);        // TIB
0022   m_color.emplace_back(kGreen);        //
0023   m_color.emplace_back(kGreen + 2);    //
0024   m_color.emplace_back(kGreen + 2);    //
0025   m_color.emplace_back(kGreen - 3);    //
0026   m_color.emplace_back(kGreen - 3);    //
0027   m_color.emplace_back(kGreen - 1);    //
0028   m_color.emplace_back(kGreen - 1);    //
0029   m_color.emplace_back(kRed);          // TOB
0030   m_color.emplace_back(kRed);          //
0031   m_color.emplace_back(kRed);          //
0032   m_color.emplace_back(kRed + 3);      //
0033   m_color.emplace_back(kRed + 3);      //
0034   m_color.emplace_back(kRed + 3);      //
0035   m_color.emplace_back(kRed - 3);      //
0036   m_color.emplace_back(kRed - 3);      //
0037   m_color.emplace_back(kRed - 3);      //
0038   m_color.emplace_back(kOrange + 9);   //
0039   m_color.emplace_back(kOrange + 9);   //
0040   m_color.emplace_back(kOrange + 9);   //
0041   m_color.emplace_back(kOrange + 7);   //
0042   m_color.emplace_back(kOrange + 7);   //
0043   m_color.emplace_back(kOrange + 7);   //
0044   m_color.emplace_back(kOrange + 5);   //
0045   m_color.emplace_back(kOrange + 5);   //
0046   m_color.emplace_back(kOrange + 5);   //
0047   m_color.emplace_back(kOrange + 8);   // PixelEndcap Z-
0048   m_color.emplace_back(kOrange + 10);  //
0049   m_color.emplace_back(kOrange - 3);   //
0050   m_color.emplace_back(kOrange - 1);   // PixelEndcap Z+
0051   m_color.emplace_back(kOrange - 8);   //
0052   m_color.emplace_back(kYellow);       // TID Z-
0053   m_color.emplace_back(kYellow);       //
0054   m_color.emplace_back(kYellow + 2);   //
0055   m_color.emplace_back(kYellow + 2);   //
0056   m_color.emplace_back(kYellow + 2);   //
0057   m_color.emplace_back(kYellow + 3);   //
0058   m_color.emplace_back(kMagenta);      //
0059   m_color.emplace_back(kMagenta);      //
0060   m_color.emplace_back(kMagenta);      //
0061   m_color.emplace_back(kMagenta);      //
0062   m_color.emplace_back(kMagenta);      //
0063   m_color.emplace_back(kMagenta + 1);  //
0064   m_color.emplace_back(kMagenta + 2);  //
0065   m_color.emplace_back(kMagenta + 3);  //
0066   m_color.emplace_back(kMagenta + 4);  //
0067   m_color.emplace_back(kMagenta + 5);  //
0068   m_color.emplace_back(kMagenta + 6);  //
0069   m_color.emplace_back(kMagenta + 7);  //
0070   m_color.emplace_back(kMagenta + 8);  //
0071 }
0072 
0073 unsigned int DD4hep_TrackingMaterialPlotter::fill_gradient(const TColor& first,
0074                                                            const TColor& last,
0075                                                            unsigned int steps /*= 100*/,
0076                                                            unsigned int index /* = 0*/) {
0077   if (index == 0) {
0078     // if no index was given, find the highest used one and start from that plus one
0079     index = ((TObjArray*)gROOT->GetListOfColors())->GetLast() + 1;
0080   }
0081 
0082   float r1, g1, b1, r2, g2, b2;
0083   first.GetRGB(r1, g1, b1);
0084   last.GetRGB(r2, g2, b2);
0085   float delta_r = (r2 - r1) / (steps - 1);
0086   float delta_g = (g2 - g1) / (steps - 1);
0087   float delta_b = (b2 - b1) / (steps - 1);
0088 
0089   m_gradient.resize(steps);
0090   for (unsigned int i = 0; i < steps; ++i) {
0091     new TColor(static_cast<Int_t>(index + i), r1 + delta_r * i, g1 + delta_g * i, b1 + delta_b * i);
0092     m_gradient[i] = index + i;
0093   }
0094 
0095   return index;
0096 }
0097 
0098 unsigned int DD4hep_TrackingMaterialPlotter::fill_gradient(const unsigned int& first,
0099                                                            const unsigned int& last,
0100                                                            const unsigned int& steps,
0101                                                            const unsigned int& index) {
0102   return fill_gradient(
0103       *(TColor*)gROOT->GetListOfColors()->At(first), *(TColor*)gROOT->GetListOfColors()->At(last), steps, index);
0104 }
0105 
0106 DD4hep_TrackingMaterialPlotter::DD4hep_TrackingMaterialPlotter(float maxZ, float maxR, float resolution) {
0107   const float rzMinZ = -maxZ;
0108   const float rzMaxZ = maxZ;
0109   const float rzMinR = 0.;
0110   const float rzMaxR = maxR;
0111   const int rzBinsZ = (int)(2. * maxZ * resolution);
0112   const int rzBinsR = (int)(maxR * resolution);
0113 
0114   std::vector<double> max;
0115   max.emplace_back(0.08);
0116   max.emplace_back(0.00016);
0117   m_tracker = DD4hep_XHistogram(
0118       2, rzBinsZ, rzBinsR, std::make_pair(rzMinZ, rzMaxZ), std::make_pair(rzMinR, rzMaxR), m_color.size(), max);
0119 
0120   TColor::InitializeColors();
0121   fill_color();
0122   fill_gradient(kWhite, kBlack, 100);  // 100-steps gradient from white to black
0123 }
0124 
0125 void DD4hep_TrackingMaterialPlotter::plotSegmentUnassigned(const MaterialAccountingStep& step) {
0126   std::vector<double> w(2);
0127   w[0] = step.radiationLengths();
0128   w[1] = step.energyLoss();
0129   m_tracker.fill(std::make_pair(step.in().z(), step.out().z()),
0130                  std::make_pair(step.in().perp(), step.out().perp()),
0131                  w,
0132                  step.length(),
0133                  1);  // 0 is empty, 1 is unassigned
0134 }
0135 
0136 void DD4hep_TrackingMaterialPlotter::plotSegmentInLayer(const MaterialAccountingStep& step, int layer) {
0137   std::vector<double> w(2);
0138   w[0] = step.radiationLengths();
0139   w[1] = step.energyLoss();
0140   m_tracker.fill(std::make_pair(step.in().z(), step.out().z()),
0141                  std::make_pair(step.in().perp(), step.out().perp()),
0142                  w,
0143                  step.length(),
0144                  layer + 1);  // layer is 1-based, but plot uses: 0 is empty, 1 is unassigned
0145 }
0146 
0147 void DD4hep_TrackingMaterialPlotter::draw(void) {
0148   const double scale = 10.;
0149   std::unique_ptr<TCanvas> canvas;
0150 
0151   DD4hep_XHistogram::Histogram* radlen = m_tracker.get(0);
0152   canvas = std::make_unique<TCanvas>(
0153       "radlen_rz", "RadiationLengths - RZ view", (int)(600 * scale * 1.25), (int)(120 * scale * 1.50));
0154   gStyle->SetOptStat(0);
0155   gStyle->SetPalette(m_gradient.size(), &m_gradient.front());
0156   gStyle->SetNumberContours(m_gradient.size());
0157   canvas->GetFrame()->SetFillColor(kWhite);
0158   radlen->Draw("colz");
0159   radlen->Draw("same axis y+");
0160   radlen->SaveAs("radlen.root");
0161   canvas->SaveAs("radlen.png");
0162   // Replicate RainBow palette, with White in the first white_slots
0163   // positions
0164   int white_slots = 1;
0165   int MyPalette[100];
0166   double stops[9] = {0.0000, 0.1250, 0.2500, 0.3750, 0.5000, 0.6250, 0.7500, 0.8750, 1.0000};
0167   double red[9] = {
0168       0. / 255., 5. / 255., 15. / 255., 35. / 255., 102. / 255., 196. / 255., 208. / 255., 199. / 255., 110. / 255.};
0169   double green[9] = {
0170       0. / 255., 48. / 255., 124. / 255., 192. / 255., 206. / 255., 226. / 255., 97. / 255., 16. / 255., 0. / 255.};
0171   double blue[9] = {
0172       99. / 255., 142. / 255., 198. / 255., 201. / 255., 90. / 255., 22. / 255., 13. / 255., 8. / 255., 2. / 255.};
0173   int palette_index = TColor::CreateGradientColorTable(9, stops, red, green, blue, 100 - white_slots);
0174   for (int i = 0; i < white_slots; i++)
0175     MyPalette[i] = kWhite;
0176   for (int i = 0; i < 100 - white_slots; i++)
0177     MyPalette[i + white_slots] = palette_index + i;
0178   canvas->Clear();
0179   gStyle->SetNumberContours(100);
0180   gStyle->SetPalette(100, MyPalette);  // ROOT Rainbow color palette
0181   radlen->Draw("colz");
0182   radlen->Draw("same axis y+");
0183   canvas->SaveAs("radlenColor.png");
0184 
0185   DD4hep_XHistogram::Histogram* dedx = m_tracker.get(1);
0186   canvas->SetName("dedx_rz");
0187   canvas->SetTitle("-dE/dx term - RZ view");
0188   canvas->GetFrame()->SetFillColor(kWhite);
0189   dedx->Draw("colz");
0190   dedx->Draw("same axis y+");
0191   dedx->SaveAs("dedx.root");
0192   canvas->SaveAs("dedx.png");
0193   canvas->Clear();
0194   gStyle->SetNumberContours(100);
0195   gStyle->SetPalette(100, MyPalette);  // ROOT Rainbow color palette
0196   dedx->Draw("colz");
0197   dedx->Draw("same axis y+");
0198   canvas->SaveAs("dedxColor.png");
0199 
0200   DD4hep_XHistogram::ColorMap* colormap = m_tracker.colormap();
0201   canvas->SetName("layer_rz");
0202   canvas->SetTitle("Layers - RZ view");
0203   canvas->GetFrame()->SetFillColor(kWhite);
0204   gStyle->SetOptStat(0);
0205   gStyle->SetPalette(m_color.size(), &m_color.front());
0206   gStyle->SetNumberContours(m_color.size());
0207   colormap->SetMinimum(1);
0208   colormap->SetMaximum(m_color.size());
0209   colormap->Draw("col");
0210   colormap->Draw("same axis y+");
0211   colormap->SaveAs("layers.root");
0212   canvas->SaveAs("layers.png");
0213 }