Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:46:18

0001 #ifndef CONDCORE_SIPIXELPLUGINS_SIPIXELPAYLOADINSPECTORHELPER_H
0002 #define CONDCORE_SIPIXELPLUGINS_SIPIXELPAYLOADINSPECTORHELPER_H
0003 
0004 #include <vector>
0005 #include <numeric>
0006 #include <fstream>  // std::ifstream
0007 #include <string>
0008 #include <bitset>
0009 
0010 #include "TGraph.h"
0011 #include "TH1.h"
0012 #include "TH2.h"
0013 #include "TLatex.h"
0014 #include "TLine.h"
0015 #include "TPave.h"
0016 #include "TPaveStats.h"
0017 #include "TPaveText.h"
0018 #include "TStyle.h"
0019 #include "TCanvas.h"
0020 
0021 #include "CondCore/CondDB/interface/Time.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/FileInPath.h"
0024 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0025 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0026 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0027 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0028 
0029 //#define MMDEBUG
0030 #ifdef MMDEBUG
0031 #include <iostream>
0032 #define COUT std::cout << "MM "
0033 #else
0034 #define COUT edm::LogVerbatim("")
0035 #endif
0036 
0037 namespace SiPixelPI {
0038 
0039   enum phase { zero = 0, one = 1, two = 2, undefined = 999 };
0040 
0041   // size of the phase-0 pixel detID list
0042   static const unsigned int phase0size = 1440;
0043   static const unsigned int phase1size = 1856;
0044   static const unsigned int phase2size = 3892;
0045   static const unsigned int mismatched = 9999;
0046 
0047   //============================================================================
0048   // struct to store info useful to construct topology based on the detid list
0049   struct PhaseInfo {
0050     // construct with det size
0051     PhaseInfo(unsigned int size) : m_detsize(size) {}
0052     // construct passing the phase
0053     PhaseInfo(const phase& thePhase) {
0054       switch (thePhase) {
0055         case phase::zero:
0056           m_detsize = phase0size;
0057           break;
0058         case phase::one:
0059           m_detsize = phase1size;
0060           break;
0061         case phase::two:
0062           m_detsize = phase2size;
0063           break;
0064         default:
0065           m_detsize = 99999;
0066           edm::LogError("PhaseInfo") << "undefined phase: " << thePhase;
0067       }
0068     }
0069     virtual ~PhaseInfo() { edm::LogInfo("PhaseInfo") << "PhaseInfo::~PhaseInfo()\n"; }
0070     const SiPixelPI::phase phase() const {
0071       if (m_detsize == phase0size)
0072         return phase::zero;
0073       else if (m_detsize == phase1size)
0074         return phase::one;
0075       else if (m_detsize > phase1size)
0076         return phase::two;
0077       else {
0078         throw cms::Exception("LogicError") << "this detId list size: " << m_detsize << "should not exist!";
0079       }
0080     }
0081 
0082     const char* pathToTopoXML() {
0083       if (m_detsize == phase0size)
0084         return "Geometry/TrackerCommonData/data/trackerParameters.xml";
0085       else if (m_detsize == phase1size)
0086         return "Geometry/TrackerCommonData/data/PhaseI/trackerParameters.xml";
0087       else if (m_detsize > phase1size)
0088         return "Geometry/TrackerCommonData/data/PhaseII/trackerParameters.xml";
0089       else {
0090         throw cms::Exception("LogicError") << "this detId list size: " << m_detsize << "should not exist!";
0091       }
0092     }
0093 
0094     const bool isPhase1Comparison(const PhaseInfo& theOtherPhase) const {
0095       if (phase() == phase::one || theOtherPhase.phase() == phase::one)
0096         return true;
0097       else
0098         return false;
0099     }
0100 
0101     const bool isComparedWithPhase2(const PhaseInfo& theOtherPhase) const {
0102       if ((phase() == phase::two && theOtherPhase.phase() != phase::two) ||
0103           (phase() != phase::two && theOtherPhase.phase() == phase::two)) {
0104         return true;
0105       } else {
0106         return false;
0107       }
0108     }
0109 
0110   private:
0111     size_t m_detsize;
0112   };
0113 
0114   //============================================================================
0115   inline std::pair<unsigned int, unsigned int> unpack(cond::Time_t since) {
0116     auto kLowMask = 0XFFFFFFFF;
0117     auto run = (since >> 32);
0118     auto lumi = (since & kLowMask);
0119     return std::make_pair(run, lumi);
0120   }
0121 
0122   //============================================================================
0123   // Taken from pixel naming classes
0124   // BmO (-z-x) = 1, BmI (-z+x) = 2 , BpO (+z-x) = 3 , BpI (+z+x) = 4
0125   inline int quadrant(const DetId& detid, const TrackerTopology* tTopo_, bool phase_) {
0126     if (detid.subdetId() == PixelSubdetector::PixelBarrel) {
0127       return PixelBarrelName(detid, tTopo_, phase_).shell();
0128     } else {
0129       return PixelEndcapName(detid, tTopo_, phase_).halfCylinder();
0130     }
0131   }
0132 
0133   //============================================================================
0134   // Online ladder convention taken from pixel naming class for barrel
0135   // Apply sign convention (- sign for BmO and BpO)
0136   inline int signed_ladder(const DetId& detid, const TrackerTopology& tTopo_, bool phase_) {
0137     if (detid.subdetId() != PixelSubdetector::PixelBarrel)
0138       return -9999;
0139     int signed_ladder = PixelBarrelName(detid, &tTopo_, phase_).ladderName();
0140     if (quadrant(detid, &tTopo_, phase_) % 2)
0141       signed_ladder *= -1;
0142     return signed_ladder;
0143   }
0144 
0145   //============================================================================
0146   // Online mdoule convention taken from pixel naming class for barrel
0147   // Apply sign convention (- sign for BmO and BmI)
0148   inline int signed_module(const DetId& detid, const TrackerTopology& tTopo_, bool phase_) {
0149     if (detid.subdetId() != PixelSubdetector::PixelBarrel)
0150       return -9999;
0151     int signed_module = PixelBarrelName(detid, &tTopo_, phase_).moduleName();
0152     if (quadrant(detid, &tTopo_, phase_) < 3)
0153       signed_module *= -1;
0154     return signed_module;
0155   }
0156 
0157   //============================================================================
0158   // Phase 0: Ring was not an existing convention
0159   //   but the 7 plaquettes were split by HV group
0160   //   --> Derive Ring 1/2 for them
0161   //   Panel 1 plq 1-2, Panel 2, plq 1   = Ring 1
0162   //   Panel 1 plq 3-4, Panel 2, plq 2-3 = Ring 2
0163   // Phase 1: Using pixel naming class for endcap
0164   inline int ring(const DetId& detid, const TrackerTopology& tTopo_, bool phase_) {
0165     if (detid.subdetId() != PixelSubdetector::PixelEndcap)
0166       return -9999;
0167     int ring = -9999;
0168     if (phase_ == 0) {
0169       ring = 1 + (tTopo_.pxfPanel(detid) + tTopo_.pxfModule(detid) > 3);
0170     } else if (phase_ == 1) {
0171       ring = PixelEndcapName(detid, &tTopo_, phase_).ringName();
0172     }
0173     return ring;
0174   }
0175 
0176   //============================================================================
0177   // Online blade convention taken from pixel naming class for endcap
0178   // Apply sign convention (- sign for BmO and BpO)
0179   inline int signed_blade(const DetId& detid, const TrackerTopology& tTopo_, bool phase_) {
0180     if (detid.subdetId() != PixelSubdetector::PixelEndcap)
0181       return -9999;
0182     int signed_blade = PixelEndcapName(detid, &tTopo_, phase_).bladeName();
0183     if (quadrant(detid, &tTopo_, phase_) % 2)
0184       signed_blade *= -1;
0185     return signed_blade;
0186   }
0187 
0188   //============================================================================
0189   inline int signed_blade_panel(const DetId& detid, const TrackerTopology& tTopo_, bool phase_) {
0190     if (detid.subdetId() != PixelSubdetector::PixelEndcap)
0191       return -9999;
0192     int signed_blade_panel = signed_blade(detid, tTopo_, phase_) + (tTopo_.pxfPanel(detid) - 1);
0193     return signed_blade_panel;
0194   }
0195 
0196   //============================================================================
0197   // Online disk convention
0198   // Apply sign convention (- sign for BmO and BmI)
0199   inline int signed_disk(const DetId& detid, const TrackerTopology& tTopo_, bool phase_) {
0200     if (detid.subdetId() != PixelSubdetector::PixelEndcap)
0201       return -9999;
0202     int signed_disk = tTopo_.pxfDisk(DetId(detid));
0203     if (quadrant(detid, &tTopo_, phase_) < 3)
0204       signed_disk *= -1;
0205     return signed_disk;
0206   }
0207 
0208   //============================================================================
0209   inline void draw_line(double x1, double x2, double y1, double y2, int width = 2, int style = 1, int color = 1) {
0210     TLine* l = new TLine(x1, y1, x2, y2);
0211     l->SetBit(kCanDelete);
0212     l->SetLineWidth(width);
0213     l->SetLineStyle(style);
0214     l->SetLineColor(color);
0215     l->Draw();
0216   }
0217 
0218   //============================================================================
0219   inline void dress_occup_plot(TCanvas& canv,
0220                                TH2* h,
0221                                int lay,
0222                                int ring = 0,
0223                                int phase = 0,
0224                                bool half_shift = true,
0225                                bool mark_zero = true,
0226                                bool standard_palette = true) {
0227     std::string s_title;
0228 
0229     if (lay > 0) {
0230       canv.cd(lay);
0231       s_title = "Barrel Pixel Layer " + std::to_string(lay);
0232     } else {
0233       canv.cd(ring);
0234       if (ring > 4) {
0235         ring = ring - 4;
0236       }
0237       s_title = "Forward Pixel Ring " + std::to_string(ring);
0238     }
0239 
0240     gStyle->SetPadRightMargin(0.125);
0241 
0242     if (standard_palette) {
0243       gStyle->SetPalette(1);
0244     } else {
0245       // this is the fine gradient palette
0246       const Int_t NRGBs = 5;
0247       const Int_t NCont = 255;
0248 
0249       Double_t stops[NRGBs] = {0.00, 0.34, 0.61, 0.84, 1.00};
0250       Double_t red[NRGBs] = {0.00, 0.00, 0.87, 1.00, 0.51};
0251       Double_t green[NRGBs] = {0.00, 0.81, 1.00, 0.20, 0.00};
0252       Double_t blue[NRGBs] = {0.51, 1.00, 0.12, 0.00, 0.00};
0253       TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
0254       gStyle->SetNumberContours(NCont);
0255     }
0256 
0257     h->SetMarkerSize(0.7);
0258     h->Draw("colz1");
0259 
0260     auto ltx = TLatex();
0261     ltx.SetTextFont(62);
0262     ltx.SetTextColor(1);
0263     ltx.SetTextSize(0.06);
0264     ltx.SetTextAlign(31);
0265     ltx.DrawLatexNDC(1 - gPad->GetRightMargin(), 1 - gPad->GetTopMargin() + 0.01, (s_title).c_str());
0266 
0267     // Draw Lines around modules
0268     if (lay > 0) {
0269       std::vector<std::vector<int>> nladder = {{10, 16, 22}, {6, 14, 22, 32}};
0270       int nlad = nladder[phase][lay - 1];
0271       for (int xsign = -1; xsign <= 1; xsign += 2)
0272         for (int ysign = -1; ysign <= 1; ysign += 2) {
0273           float xlow = xsign * (half_shift * 0.5);
0274           float xhigh = xsign * (half_shift * 0.5 + 4);
0275           float ylow = ysign * (half_shift * 0.5 + (phase == 0) * 0.5);
0276           float yhigh = ysign * (half_shift * 0.5 - (phase == 0) * 0.5 + nlad);
0277           // Outside box
0278           draw_line(xlow, xhigh, ylow, ylow, 1);    // bottom
0279           draw_line(xlow, xhigh, yhigh, yhigh, 1);  // top
0280           draw_line(xlow, xlow, ylow, yhigh, 1);    // left
0281           draw_line(xhigh, xhigh, ylow, yhigh, 1);  // right
0282           // Inner Horizontal lines
0283           for (int lad = 1; lad < nlad; ++lad) {
0284             float y = ysign * (lad + half_shift * 0.5);
0285             draw_line(xlow, xhigh, y, y, 1);
0286           }
0287           for (int lad = 1; lad <= nlad; ++lad)
0288             if (!(phase == 0 && (lad == 1 || lad == nlad))) {
0289               float y = ysign * (lad + half_shift * 0.5 - 0.5);
0290               draw_line(xlow, xhigh, y, y, 1, 3);
0291             }
0292           // Inner Vertical lines
0293           for (int mod = 1; mod < 4; ++mod) {
0294             float x = xsign * (mod + half_shift * 0.5);
0295             draw_line(x, x, ylow, yhigh, 1);
0296           }
0297           // Make a BOX around ROC 0
0298           // Phase 0 - ladder +1 is always non-flipped
0299           // Phase 1 - ladder +1 is always     flipped
0300           if (mark_zero) {
0301             for (int mod = 1; mod <= 4; ++mod)
0302               for (int lad = 1; lad <= nlad; ++lad) {
0303                 bool flipped = ysign == 1 ? lad % 2 == 0 : lad % 2 == 1;
0304                 if (phase == 1)
0305                   flipped = !flipped;
0306                 int roc0_orientation = flipped ? -1 : 1;
0307                 if (xsign == -1)
0308                   roc0_orientation *= -1;
0309                 if (ysign == -1)
0310                   roc0_orientation *= -1;
0311                 float x1 = xsign * (mod + half_shift * 0.5);
0312                 float x2 = xsign * (mod + half_shift * 0.5 - 1. / 8);
0313                 float y1 = ysign * (lad + half_shift * 0.5 - 0.5);
0314                 float y2 = ysign * (lad + half_shift * 0.5 - 0.5 + roc0_orientation * 1. / 2);
0315                 if (!(phase == 0 && (lad == 1 || lad == nlad) && xsign == -1)) {
0316                   if (lay == 1 && xsign <= -1) {
0317                     float x1 = xsign * ((mod - 1) + half_shift * 0.5);
0318                     float x2 = xsign * ((mod - 1) + half_shift * 0.5 + 1. / 8);
0319                     float y1 = ysign * (lad + half_shift * 0.5 - 0.5 + roc0_orientation);
0320                     float y2 = ysign * (lad + half_shift * 0.5 - 0.5 + roc0_orientation * 3. / 2);
0321                     draw_line(x1, x2, y1, y1, 1);
0322                     draw_line(x2, x2, y1, y2, 1);
0323                   } else {
0324                     draw_line(x1, x2, y1, y1, 1);
0325                     //draw_line(x1, x2, y2, y2, 1);
0326                     //draw_line(x1, x1, y1, y2, 1);
0327                     draw_line(x2, x2, y1, y2, 1);
0328                   }
0329                 }
0330               }
0331           }
0332         }
0333     } else {
0334       // FPIX
0335       for (int dsk = 1, ndsk = 2 + (phase == 1); dsk <= ndsk; ++dsk) {
0336         for (int xsign = -1; xsign <= 1; xsign += 2)
0337           for (int ysign = -1; ysign <= 1; ysign += 2) {
0338             if (phase == 0) {
0339               int first_roc = 3, nbin = 16;
0340               for (int bld = 1, nbld = 12; bld <= nbld; ++bld) {
0341                 // Horizontal lines
0342                 for (int plq = 1, nplq = 7; plq <= nplq; ++plq) {
0343                   float xlow =
0344                       xsign * (half_shift * 0.5 + dsk - 1 + (first_roc - 3 + 2 * plq + (plq == 1)) / (float)nbin);
0345                   float xhigh =
0346                       xsign * (half_shift * 0.5 + dsk - 1 + (first_roc - 3 + 2 * (plq + 1) - (plq == 7)) / (float)nbin);
0347                   float ylow = ysign * (half_shift * 0.5 + (bld - 0.5) - (2 + plq / 2) * 0.1);
0348                   float yhigh = ysign * (half_shift * 0.5 + (bld - 0.5) + (2 + plq / 2) * 0.1);
0349                   draw_line(xlow, xhigh, ylow, ylow, 1);    // bottom
0350                   draw_line(xlow, xhigh, yhigh, yhigh, 1);  // top
0351                 }
0352                 // Vertical lines
0353                 for (int plq = 1, nplq = 7 + 1; plq <= nplq; ++plq) {
0354                   float x = xsign * (half_shift * 0.5 + dsk - 1 +
0355                                      (first_roc - 3 + 2 * plq + (plq == 1) - (plq == 8)) / (float)nbin);
0356                   float ylow = ysign * (half_shift * 0.5 + (bld - 0.5) - (2 + (plq - (plq == 8)) / 2) * 0.1);
0357                   float yhigh = ysign * (half_shift * 0.5 + (bld - 0.5) + (2 + (plq - (plq == 8)) / 2) * 0.1);
0358                   draw_line(x, x, ylow, yhigh, 1);
0359                 }
0360                 // Panel 2 has dashed mid-plane
0361                 for (int plq = 2, nplq = 6; plq <= nplq; ++plq)
0362                   if (plq % 2 == 0) {
0363                     float x = xsign * (half_shift * 0.5 + dsk - 1 +
0364                                        (first_roc - 3 + 2 * plq + (plq == 1) - (plq == 8) + 1) / (float)nbin);
0365                     float ylow = ysign * (half_shift * 0.5 + (bld - 0.5) - (2 + (plq - (plq == 8)) / 2) * 0.1);
0366                     float yhigh = ysign * (half_shift * 0.5 + (bld - 0.5) + (2 + (plq - (plq == 8)) / 2) * 0.1);
0367                     draw_line(x, x, ylow, yhigh, 1, 2);
0368                   }
0369                 // Make a BOX around ROC 0
0370                 for (int plq = 1, nplq = 7; plq <= nplq; ++plq) {
0371                   float x1 =
0372                       xsign * (half_shift * 0.5 + dsk - 1 + (first_roc - 3 + 2 * plq + (plq == 1)) / (float)nbin);
0373                   float x2 =
0374                       xsign * (half_shift * 0.5 + dsk - 1 + (first_roc - 3 + 2 * plq + (plq == 1) + 1) / (float)nbin);
0375                   int sign = xsign * ysign * ((plq % 2) ? 1 : -1);
0376                   float y1 = ysign * (half_shift * 0.5 + (bld - 0.5) + sign * (2 + plq / 2) * 0.1);
0377                   float y2 = ysign * (half_shift * 0.5 + (bld - 0.5) + sign * (plq / 2) * 0.1);
0378                   //draw_line(x1, x2, y1, y1, 1);
0379                   draw_line(x1, x2, y2, y2, 1);
0380                   //draw_line(x1, x1, y1, y2, 1);
0381                   draw_line(x2, x2, y1, y2, 1);
0382                 }
0383               }
0384             } else if (phase == 1) {
0385               if (ring == 0) {  // both
0386                 for (int ring = 1; ring <= 2; ++ring)
0387                   for (int bld = 1, nbld = 5 + ring * 6; bld <= nbld; ++bld) {
0388                     float scale = (ring == 1) ? 1.5 : 1;
0389                     Color_t p1_color = 1, p2_color = 1;
0390                     // Horizontal lines
0391                     // Panel 2 has dashed mid-plane
0392                     float x1 = xsign * (half_shift * 0.5 + dsk - 1 + (ring - 1) * 0.5);
0393                     float x2 = xsign * (half_shift * 0.5 + dsk - 1 + ring * 0.5);
0394                     int sign = ysign;
0395                     float y1 = ysign * (half_shift * 0.5 - 0.5 + scale * bld + sign * 0.5);
0396                     //float yp1_mid = ysign * (half_shift*0.5 - 0.5 + scale*bld + sign*0.25);
0397                     float y2 = ysign * (half_shift * 0.5 - 0.5 + scale * bld);
0398                     float yp2_mid = ysign * (half_shift * 0.5 - 0.5 + scale * bld - sign * 0.25);
0399                     float y3 = ysign * (half_shift * 0.5 - 0.5 + scale * bld - sign * 0.5);
0400                     draw_line(x1, x2, y1, y1, 1, 1, p1_color);
0401                     //draw_line(x1, x2, yp1_mid, yp1_mid, 1, 3);
0402                     draw_line(x1, x2, y2, y2, 1, 1, p1_color);
0403                     draw_line(x1, x2, yp2_mid, yp2_mid, 1, 2);
0404                     draw_line(x1, x2, y3, y3, 1, 1, p2_color);
0405                     // Vertical lines
0406                     float x = xsign * (half_shift * 0.5 + dsk - 1 + (ring - 1) * 0.5);
0407                     draw_line(x, x, y1, y2, 1, 1, p1_color);
0408                     draw_line(x, x, y2, y3, 1, 1, p2_color);
0409                     if (ring == 2) {
0410                       //draw_line(x,  x,  y2,  y3, 1, 1, p1_color);
0411                       x = xsign * (half_shift * 0.5 + dsk);
0412                       draw_line(x, x, y1, y2, 1, 1, p1_color);
0413                       draw_line(x, x, y2, y3, 1, 1, p2_color);
0414                     }
0415                     // Make a BOX around ROC 0
0416                     x1 = xsign * (half_shift * 0.5 + dsk - 1 + ring * 0.5 - 1 / 16.);
0417                     x2 = xsign * (half_shift * 0.5 + dsk - 1 + ring * 0.5);
0418                     float y1_p1 = ysign * (half_shift * 0.5 - 0.5 + scale * bld + sign * 0.25);
0419                     float y2_p1 = ysign * (half_shift * 0.5 - 0.5 + scale * bld + sign * 0.25 + xsign * ysign * 0.25);
0420                     draw_line(x1, x2, y1_p1, y1_p1, 1);
0421                     //draw_line(x1, x2, y2_p1, y2_p1, 1);
0422                     draw_line(x1, x1, y1_p1, y2_p1, 1);
0423                     //draw_line(x2, x2, y1_p1, y2_p1, 1);
0424                     float y1_p2 = ysign * (half_shift * 0.5 - 0.5 + scale * bld - sign * 0.25);
0425                     float y2_p2 = ysign * (half_shift * 0.5 - 0.5 + scale * bld - sign * 0.25 - xsign * ysign * 0.25);
0426                     draw_line(x1, x2, y1_p2, y1_p2, 1);
0427                     //draw_line(x1, x2, y2_p2, y2_p2, 1);
0428                     draw_line(x1, x1, y1_p2, y2_p2, 1);
0429                     //draw_line(x2, x2, y1_p2, y2_p2, 1);
0430                   }
0431               } else {  // only one ring, 1 or 2
0432                 for (int bld = 1, nbld = 5 + ring * 6; bld <= nbld; ++bld) {
0433                   Color_t p1_color = 1, p2_color = 1;
0434                   // Horizontal lines
0435                   // Panel 2 has dashed mid-plane
0436                   float x1 = xsign * (half_shift * 0.5 + dsk - 1);
0437                   float x2 = xsign * (half_shift * 0.5 + dsk);
0438                   int sign = ysign;
0439                   float y1 = ysign * (half_shift * 0.5 - 0.5 + bld + sign * 0.5);
0440                   //float yp1_mid = ysign * (half_shift*0.5 - 0.5 + bld + sign*0.25);
0441                   float y2 = ysign * (half_shift * 0.5 - 0.5 + bld);
0442                   float yp2_mid = ysign * (half_shift * 0.5 - 0.5 + bld - sign * 0.25);
0443                   float y3 = ysign * (half_shift * 0.5 - 0.5 + bld - sign * 0.5);
0444                   draw_line(x1, x2, y1, y1, 1, 1, p1_color);
0445                   //draw_line(x1, x2, yp1_mid, yp1_mid, 1, 3);
0446                   draw_line(x1, x2, y2, y2, 1, 1, p1_color);
0447                   draw_line(x1, x2, yp2_mid, yp2_mid, 1, 2);
0448                   draw_line(x1, x2, y3, y3, 1, 1, p2_color);
0449                   // Vertical lines
0450                   float x = xsign * (half_shift * 0.5 + dsk - 1);
0451                   draw_line(x, x, y1, y2, 1, 1, p1_color);
0452                   draw_line(x, x, y2, y3, 1, 1, p2_color);
0453                   if (ring == 2) {
0454                     //draw_line(x,  x,  y2,  y3, 1, 1, p1_color);
0455                     x = xsign * (half_shift * 0.5 + dsk);
0456                     draw_line(x, x, y1, y2, 1, 1, p1_color);
0457                     draw_line(x, x, y2, y3, 1, 1, p2_color);
0458                   }
0459                   // Make a BOX around ROC 0
0460                   x1 = xsign * (half_shift * 0.5 + dsk - 1 / 8.);
0461                   x2 = xsign * (half_shift * 0.5 + dsk);
0462                   float y1_p1 = ysign * (half_shift * 0.5 - 0.5 + bld + sign * 0.25);
0463                   float y2_p1 = ysign * (half_shift * 0.5 - 0.5 + bld + sign * 0.25 + xsign * ysign * 0.25);
0464                   draw_line(x1, x2, y1_p1, y1_p1, 1);
0465                   //draw_line(x1, x2, y2_p1, y2_p1, 1);
0466                   draw_line(x1, x1, y1_p1, y2_p1, 1);
0467                   //draw_line(x2, x2, y1_p1, y2_p1, 1);
0468                   float y1_p2 = ysign * (half_shift * 0.5 - 0.5 + bld - sign * 0.25);
0469                   float y2_p2 = ysign * (half_shift * 0.5 - 0.5 + bld - sign * 0.25 - xsign * ysign * 0.25);
0470                   draw_line(x1, x2, y1_p2, y1_p2, 1);
0471                   //draw_line(x1, x2, y2_p2, y2_p2, 1);
0472                   draw_line(x1, x1, y1_p2, y2_p2, 1);
0473                   //draw_line(x2, x2, y1_p2, y2_p2, 1);
0474                 }
0475               }
0476             }
0477           }
0478       }
0479       // Special shifted "rebin" for Phase 0
0480       // Y axis should always have at least half-roc granularity because
0481       // there are half-ROC size shifts implemented in the coordinates
0482       // To remove this and show full ROC granularity
0483       // We merge bin contents in each pair of bins corresponding to one ROC
0484       // TODO: make sure this works for Profiles
0485       if (phase == 0 && h->GetNbinsY() == 250 && h->GetNbinsX() == 80) {
0486         int nentries = h->GetEntries();
0487         for (int binx = 1; binx <= 80; ++binx) {
0488           double sum = 0;
0489           for (int biny = 1; biny <= 250; ++biny) {
0490             bool odd_nrocy = (binx - 1 < 40) != (((binx - 1) / 4) % 2);
0491             if (biny % 2 == odd_nrocy)
0492               sum += h->GetBinContent(binx, biny);
0493             else {
0494               sum += h->GetBinContent(binx, biny);
0495               if (sum) {
0496                 h->SetBinContent(binx, biny, sum);
0497                 h->SetBinContent(binx, biny - 1, sum);
0498               }
0499               sum = 0;
0500             }
0501           }
0502         }
0503         h->SetEntries(nentries);
0504       }
0505     }
0506   }
0507 
0508   /*--------------------------------------------------------------------*/
0509   inline void adjustCanvasMargins(TVirtualPad* pad, float top, float bottom, float left, float right)
0510   /*--------------------------------------------------------------------*/
0511   {
0512     if (top > 0)
0513       pad->SetTopMargin(top);
0514     if (bottom > 0)
0515       pad->SetBottomMargin(bottom);
0516     if (left > 0)
0517       pad->SetLeftMargin(left);
0518     if (right > 0)
0519       pad->SetRightMargin(right);
0520   }
0521 
0522   /*--------------------------------------------------------------------*/
0523   inline void adjustStats(TPaveStats* stats, float X1, float Y1, float X2, float Y2)
0524   /*--------------------------------------------------------------------*/
0525   {
0526     stats->SetX1NDC(X1);  //new x start position
0527     stats->SetY1NDC(Y1);  //new y start position
0528     stats->SetX2NDC(X2);  //new x end position
0529     stats->SetY2NDC(Y2);  //new y end position
0530   }
0531 
0532   /*--------------------------------------------------------------------*/
0533   inline std::pair<float, float> getExtrema(TH1* h1, TH1* h2)
0534   /*--------------------------------------------------------------------*/
0535   {
0536     float theMax(-9999.);
0537     float theMin(9999.);
0538     theMax = h1->GetMaximum() > h2->GetMaximum() ? h1->GetMaximum() : h2->GetMaximum();
0539     theMin = h1->GetMinimum() < h2->GetMaximum() ? h1->GetMinimum() : h2->GetMinimum();
0540 
0541     float add_min = theMin > 0. ? -0.05 : 0.05;
0542     float add_max = theMax > 0. ? 0.05 : -0.05;
0543 
0544     auto result = std::make_pair(theMin * (1 + add_min), theMax * (1 + add_max));
0545     return result;
0546   }
0547 
0548   /*--------------------------------------------------------------------*/
0549   inline void makeNicePlotStyle(TH1* hist)
0550   /*--------------------------------------------------------------------*/
0551   {
0552     hist->SetStats(kFALSE);
0553     hist->SetLineWidth(2);
0554     hist->GetXaxis()->CenterTitle(true);
0555     hist->GetYaxis()->CenterTitle(true);
0556     hist->GetXaxis()->SetTitleFont(42);
0557     hist->GetYaxis()->SetTitleFont(42);
0558     hist->GetXaxis()->SetTitleSize(0.05);
0559     hist->GetYaxis()->SetTitleSize(0.05);
0560     hist->GetXaxis()->SetTitleOffset(1.1);
0561     hist->GetYaxis()->SetTitleOffset(1.3);
0562     hist->GetXaxis()->SetLabelFont(42);
0563     hist->GetYaxis()->SetLabelFont(42);
0564     hist->GetYaxis()->SetLabelSize(.05);
0565     hist->GetXaxis()->SetLabelSize(.05);
0566 
0567     if (hist->InheritsFrom(TH2::Class())) {
0568       hist->GetZaxis()->SetLabelFont(42);
0569       hist->GetZaxis()->SetLabelFont(42);
0570       hist->GetZaxis()->SetLabelSize(.05);
0571       hist->GetZaxis()->SetLabelSize(.05);
0572     }
0573   }
0574 
0575   enum DetType { t_barrel = 0, t_forward = 1, t_all = 2 };
0576   const std::array<std::string, 3> DetNames = {{"Barrel", "End Caps", "Whole"}};
0577 
0578   enum regions {
0579     BPixL1o,        //0  Barrel Pixel Layer 1 outer
0580     BPixL1i,        //1  Barrel Pixel Layer 1 inner
0581     BPixL2o,        //2  Barrel Pixel Layer 2 outer
0582     BPixL2i,        //3  Barrel Pixel Layer 2 inner
0583     BPixL3o,        //4  Barrel Pixel Layer 3 outer
0584     BPixL3i,        //5  Barrel Pixel Layer 3 inner
0585     BPixL4o,        //6  Barrel Pixel Layer 4 outer
0586     BPixL4i,        //7  Barrel Pixel Layer 4 inner
0587     FPixmL1,        //8  Forward Pixel Minus side Disk 1
0588     FPixmL2,        //9  Forward Pixel Minus side Disk 2
0589     FPixmL3,        //10 Forward Pixel Minus side Disk 3
0590     FPixpL1,        //11 Forward Pixel Plus side Disk 1
0591     FPixpL2,        //12 Forward Pixel Plus side Disk 2
0592     FPixpL3,        //13 Forward Pixel Plus side Disk 3
0593     NUM_OF_REGIONS  //14 -- default
0594   };
0595 
0596   /*--------------------------------------------------------------------*/
0597   inline std::string getStringFromRegionEnum(SiPixelPI::regions e)
0598   /*--------------------------------------------------------------------*/
0599   {
0600     switch (e) {
0601       case SiPixelPI::BPixL1o:
0602         return "BPix L1/o";
0603       case SiPixelPI::BPixL1i:
0604         return "BPix L1/i";
0605       case SiPixelPI::BPixL2o:
0606         return "BPix L2/o";
0607       case SiPixelPI::BPixL2i:
0608         return "BPix L2/i";
0609       case SiPixelPI::BPixL3o:
0610         return "BPix L3/o";
0611       case SiPixelPI::BPixL3i:
0612         return "BPix L3/i";
0613       case SiPixelPI::BPixL4o:
0614         return "BPix L4/o";
0615       case SiPixelPI::BPixL4i:
0616         return "BPix L4/i";
0617       case SiPixelPI::FPixmL1:
0618         return "FPix- D1";
0619       case SiPixelPI::FPixmL2:
0620         return "FPix- D2";
0621       case SiPixelPI::FPixmL3:
0622         return "FPix- D3";
0623       case SiPixelPI::FPixpL1:
0624         return "FPix+ D1";
0625       case SiPixelPI::FPixpL2:
0626         return "FPix+ D2";
0627       case SiPixelPI::FPixpL3:
0628         return "FPix+ D3";
0629       default:
0630         edm::LogWarning("LogicError") << "Unknown partition: " << e;
0631         return "";
0632     }
0633   }
0634 
0635   /*--------------------------------------------------------------------*/
0636   inline bool isBPixOuterLadder(const DetId& detid, const TrackerTopology& tTopo, bool isPhase0)
0637   /*--------------------------------------------------------------------*/
0638   {
0639     // Using TrackerTopology
0640     // Ladders have a staggered structure
0641     // Non-flipped ladders are on the outer radius
0642     // Phase 0: Outer ladders are odd for layer 1,3 and even for layer 2
0643     // Phase 1: Outer ladders are odd for layer 1,2,3 and even for layer 4
0644     bool isOuter = false;
0645     int layer = tTopo.pxbLayer(detid.rawId());
0646     bool odd_ladder = tTopo.pxbLadder(detid.rawId()) % 2;
0647     if (isPhase0) {
0648       if (layer == 2)
0649         isOuter = !odd_ladder;
0650       else
0651         isOuter = odd_ladder;
0652     } else {
0653       if (layer == 4)
0654         isOuter = !odd_ladder;
0655       else
0656         isOuter = odd_ladder;
0657     }
0658     return isOuter;
0659   }
0660 
0661   // ancillary struct to manage the topology
0662   // info in a more compact way
0663 
0664   struct topolInfo {
0665   private:
0666     uint32_t m_rawid;
0667     int m_subdetid;
0668     int m_layer;
0669     int m_side;
0670     int m_ring;
0671     bool m_isInternal;
0672     SiPixelPI::phase m_Phase;
0673 
0674   public:
0675     void init();
0676     void fillGeometryInfo(const DetId& detId, const TrackerTopology& tTopo, const SiPixelPI::phase& ph);
0677     SiPixelPI::regions filterThePartition();
0678     bool sanityCheck();
0679     int subDetId() { return m_subdetid; }
0680     int layer() { return m_layer; }
0681     int side() { return m_side; }
0682     int ring() { return m_ring; }
0683     bool isInternal() { return m_isInternal; }
0684 
0685     void printAll(std::stringstream& ss) const;
0686     virtual ~topolInfo() {}
0687   };
0688 
0689   /*--------------------------------------------------------------------*/
0690   inline void topolInfo::printAll(std::stringstream& ss) const
0691   /*--------------------------------------------------------------------*/
0692   {
0693     ss << " detId: " << m_rawid << " subdetid: " << m_subdetid << " layer: " << m_layer << " side: " << m_side
0694        << " ring: " << m_ring << " isInternal: " << m_isInternal;
0695   }
0696 
0697   /*--------------------------------------------------------------------*/
0698   inline void topolInfo::init()
0699   /*--------------------------------------------------------------------*/
0700   {
0701     m_Phase = SiPixelPI::undefined;
0702     m_rawid = 0;
0703     m_subdetid = -1;
0704     m_layer = -1;
0705     m_side = -1;
0706     m_ring = -1;
0707     m_isInternal = false;
0708   };
0709 
0710   /*--------------------------------------------------------------------*/
0711   inline bool topolInfo::sanityCheck()
0712   /*--------------------------------------------------------------------*/
0713   {
0714     if (m_layer == 0 || (m_subdetid == 1 && m_layer > 4) || (m_subdetid == 2 && m_layer > 3)) {
0715       return false;
0716     } else {
0717       return true;
0718     }
0719   }
0720   /*--------------------------------------------------------------------*/
0721   inline void topolInfo::fillGeometryInfo(const DetId& detId, const TrackerTopology& tTopo, const SiPixelPI::phase& ph)
0722   /*--------------------------------------------------------------------*/
0723   {
0724     // set the phase
0725     m_Phase = ph;
0726     unsigned int subdetId = static_cast<unsigned int>(detId.subdetId());
0727 
0728     m_rawid = detId.rawId();
0729     m_subdetid = subdetId;
0730     if (subdetId == PixelSubdetector::PixelBarrel) {
0731       m_layer = tTopo.pxbLayer(detId.rawId());
0732       m_isInternal = !SiPixelPI::isBPixOuterLadder(detId, tTopo, (ph == SiPixelPI::phase::zero));
0733     } else if (subdetId == PixelSubdetector::PixelEndcap) {
0734       m_layer = tTopo.pxfDisk(detId.rawId());
0735       m_side = tTopo.pxfSide(detId.rawId());
0736     } else
0737       edm::LogWarning("LogicError") << "Unknown subdetid: " << subdetId;
0738   }
0739 
0740   // ------------ method to assign a partition based on the topology struct info ---------------
0741 
0742   /*--------------------------------------------------------------------*/
0743   inline SiPixelPI::regions topolInfo::filterThePartition()
0744   /*--------------------------------------------------------------------*/
0745   {
0746     SiPixelPI::regions ret = SiPixelPI::NUM_OF_REGIONS;
0747 
0748     if (m_Phase == SiPixelPI::undefined) {
0749       throw cms::Exception("LogicError") << "Cannot call filterThePartition BEFORE filling the geometry info!";
0750     }
0751 
0752     // BPix
0753     if (m_subdetid == 1) {
0754       switch (m_layer) {
0755         case 1:
0756           m_isInternal ? ret = SiPixelPI::BPixL1i : ret = SiPixelPI::BPixL1o;
0757           break;
0758         case 2:
0759           m_isInternal ? ret = SiPixelPI::BPixL2i : ret = SiPixelPI::BPixL2o;
0760           break;
0761         case 3:
0762           m_isInternal ? ret = SiPixelPI::BPixL3i : ret = SiPixelPI::BPixL3o;
0763           break;
0764         case 4:
0765           m_isInternal ? ret = SiPixelPI::BPixL4i : ret = SiPixelPI::BPixL4o;
0766           break;
0767         default:
0768           edm::LogWarning("LogicError") << "Unknow BPix layer: " << m_layer;
0769           break;
0770       }
0771       // FPix
0772     } else if (m_subdetid == 2) {
0773       switch (m_layer) {
0774         case 1:
0775           m_side > 1 ? ret = SiPixelPI::FPixpL1 : ret = SiPixelPI::FPixmL1;
0776           break;
0777         case 2:
0778           m_side > 1 ? ret = SiPixelPI::FPixpL2 : ret = SiPixelPI::FPixmL2;
0779           break;
0780         case 3:
0781           m_side > 1 ? ret = SiPixelPI::FPixpL3 : ret = SiPixelPI::FPixmL3;
0782           break;
0783         default:
0784           if (m_Phase < SiPixelPI::phase::two) {
0785             // warning message only if the phase2 is < 2
0786             edm::LogWarning("LogicError") << "Unknow FPix disk: " << m_layer;
0787           }
0788           break;
0789       }
0790     }
0791     return ret;
0792   }
0793 
0794   /*--------------------------------------------------------------------*/
0795   inline void displayNotSupported(TCanvas& canv, const unsigned int size)
0796   /*--------------------------------------------------------------------*/
0797   {
0798     std::string phase = (size < SiPixelPI::phase1size) ? "Phase-0" : "Phase-2";
0799     canv.cd();
0800     TLatex t2;
0801     t2.SetTextAlign(21);
0802     t2.SetTextSize(0.1);
0803     t2.SetTextAngle(45);
0804     t2.SetTextColor(kRed);
0805     if (size != SiPixelPI::mismatched) {
0806       t2.DrawLatexNDC(0.6, 0.50, Form("%s  NOT SUPPORTED!", phase.c_str()));
0807     } else {
0808       t2.DrawLatexNDC(0.6, 0.50, "MISMATCHED PAYLOAD SIZE!");
0809     }
0810   }
0811 
0812   /*--------------------------------------------------------------------*/
0813   template <typename T>
0814   std::pair<T, T> findMinMaxInMap(const std::map<unsigned int, T>& theMap)
0815   /*--------------------------------------------------------------------*/
0816   {
0817     using pairtype = std::pair<unsigned int, T>;
0818     auto max = *std::max_element(
0819         theMap.begin(), theMap.end(), [](const pairtype& p1, const pairtype& p2) { return p1.second < p2.second; });
0820     auto min = *std::min_element(
0821         theMap.begin(), theMap.end(), [](const pairtype& p1, const pairtype& p2) { return p1.second < p2.second; });
0822     return std::make_pair(min.second, max.second);
0823   }
0824 
0825   /*--------------------------------------------------------------------*/
0826   inline bool checkAnswerOK(std::string& answer, bool& result)
0827   /*--------------------------------------------------------------------*/
0828   {
0829     std::transform(answer.begin(), answer.end(), answer.begin(), [](unsigned char x) { return ::tolower(x); });
0830 
0831     bool answer_valid = (answer == "y") || (answer == "n") || (answer == "yes") || (answer == "no") ||
0832                         (answer == "true") || (answer == "false") || (answer == "1") || (answer == "0");
0833 
0834     result = answer_valid && (answer[0] == 'y' || answer[0] == 't' || answer[0] == '1');
0835     return answer_valid;
0836   }
0837 };  // namespace SiPixelPI
0838 #endif