Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
#include <iostream>
#include <cassert>
#include <sstream>
#include <cfloat>
#include <algorithm>
#include <memory>

#include "CondTools/Hcal/interface/visualizeHFPhase1PMTParams.h"
#include "CondTools/Hcal/interface/parseHcalDetId.h"

#include "TCanvas.h"
#include "TPad.h"
#include "TGraph.h"
#include "TAxis.h"
#include "TH1F.h"

typedef std::vector<std::shared_ptr<TGraph> > Garbage;

//
// Return a usable root name (without spaces) for the given detector id
//
static std::string rootDetectorName(const HcalDetId& id) {
  using namespace std;
  ostringstream os;
  os << hcalSubdetectorName(id.subdet()) << '_' << id.ieta() << '_' << id.iphi() << '_' << id.depth();
  return os.str();
}

static TH1F* plotFunctors(TVirtualPad* pad,
                          const char* plotTitle,
                          const char* yAxisTitle,
                          const double cut,
                          const double ymin,
                          const double ymax,
                          const AbsHcalFunctor& f1,
                          const AbsHcalFunctor& f2,
                          const VisualizationOptions& opts,
                          Garbage& garbage,
                          const bool isReference,
                          TH1F* frame) {
  const int lineWidth = 2;
  const int refLineWidth = 4;

  assert(opts.plotPoints > 1);
  const unsigned nDraw = opts.plotPoints + 1;
  std::vector<double> xvec(nDraw);
  std::vector<double> y1(nDraw);
  std::vector<double> y2(nDraw);
  const double xrange = opts.maxCharge - cut;
  const double yrange = ymax - ymin;
  xvec[0] = cut;
  y1[0] = ymin + 0.05 * yrange;
  y2[0] = ymax - 0.05 * yrange;
  const double h = xrange / (opts.plotPoints - 1);
  for (unsigned i = 1; i <= opts.plotPoints; ++i) {
    const double x = cut + h * (i - 1);
    xvec[i] = x;
    y1[i] = f1(x);
    y2[i] = f2(x);
  }

  // Root was designed by (insert your favorite explective here),
  // so the plot frame on the pad is of type TH1F (a histogram!)
  //
  if (frame == nullptr) {
    frame = pad->DrawFrame(opts.minCharge, ymin, opts.maxCharge + 0.1 * xrange, ymax);
    frame->GetXaxis()->SetTitle("Q [fC]");
    frame->GetYaxis()->SetTitle(yAxisTitle);
    frame->SetTitle(plotTitle);
  }

  std::shared_ptr<TGraph> gr1(new TGraph(nDraw, &xvec[0], &y1[0]));
  gr1->SetLineWidth(isReference ? refLineWidth : lineWidth);
  gr1->SetLineColor(isReference ? kGreen : kBlue);
  gr1->Draw("L");
  garbage.push_back(gr1);

  std::shared_ptr<TGraph> gr2(new TGraph(nDraw, &xvec[0], &y2[0]));
  gr2->SetLineWidth(isReference ? refLineWidth : lineWidth);
  gr2->SetLineColor(isReference ? kGreen : kRed);
  gr2->Draw("L");
  garbage.push_back(gr2);

  return frame;
}

static void plotConfig(const HcalDetId& id,
                       const unsigned tableIndex,
                       const HFPhase1PMTData& cut,
                       const VisualizationOptions& opts,
                       const HFPhase1PMTData* ref) {
  using namespace std;

  const double canvasWidth = 1000;
  const double canvasHeight = 300;

  // Root was designed by idiots oblivious to smart pointers and unable
  // to carry their objects around. Because of this, most plottable
  // object in "Root" must have a unique name, even if it makes no sense
  // whatsoever to name such an object. Keep it in mind that idiots are
  // incapable of handling spaces inside the names.
  //
  const bool isDefault = tableIndex == HcalIndexLookup::InvalidIndex;
  const std::string& detName = rootDetectorName(id);
  const std::string& canvName = isDefault ? std::string("Default") : detName;
  ostringstream ostitle;
  if (isDefault)
    ostitle << "Default Cuts";
  else
    ostitle << "Cuts for " << detName;
  const std::string& canvTitle = ostitle.str();

  TCanvas* canv = new TCanvas(canvName.c_str(), canvTitle.c_str(), canvasWidth, canvasHeight);

  // Root was designed by idiots oblivious to the difference between
  // window size and canvas size. Because of this, the canvas size
  // has to be set explicitly again, despite the fact that the
  // constructor already had these arguments.
  //
  canv->SetCanvasSize(canvasWidth, canvasHeight);
  canv->Divide(3, 1);

  // Root was designed by idiots changing their minds too often.
  // Graphs are an exception from the naming scheme and ownership
  // convention. They are not managed by root, so graphs on all
  // pads must persist until the whole canvas is written out.
  // This is why we need a garbage bin -- to manage these things.
  //
  Garbage bin;

  TVirtualPad* pad = canv->cd(1);
  {
    TH1F* frame = nullptr;
    if (ref)
      frame = plotFunctors(pad,
                           "PMT Anode 0",
                           "Rise Time Cuts [ns]",
                           ref->minCharge0(),
                           opts.minTDC,
                           opts.maxTDC,
                           ref->cut(HFPhase1PMTData::T_0_MIN),
                           ref->cut(HFPhase1PMTData::T_0_MAX),
                           opts,
                           bin,
                           true,
                           frame);
    plotFunctors(pad,
                 "PMT Anode 0",
                 "Rise Time Cuts [ns]",
                 cut.minCharge0(),
                 opts.minTDC,
                 opts.maxTDC,
                 cut.cut(HFPhase1PMTData::T_0_MIN),
                 cut.cut(HFPhase1PMTData::T_0_MAX),
                 opts,
                 bin,
                 false,
                 frame);
  }
  pad->Draw();

  ostringstream midtitle;
  midtitle << "PMT Anode 1 ";
  if (isDefault)
    midtitle << "(Default Cuts)";
  else
    midtitle << '(' << detName << ", Index " << tableIndex << ')';
  const string& mtstr = midtitle.str();
  pad = canv->cd(2);
  {
    TH1F* frame = nullptr;
    if (ref)
      frame = plotFunctors(pad,
                           mtstr.c_str(),
                           "Rise Time Cuts [ns]",
                           ref->minCharge1(),
                           opts.minTDC,
                           opts.maxTDC,
                           ref->cut(HFPhase1PMTData::T_1_MIN),
                           ref->cut(HFPhase1PMTData::T_1_MAX),
                           opts,
                           bin,
                           true,
                           frame);
    plotFunctors(pad,
                 mtstr.c_str(),
                 "Rise Time Cuts [ns]",
                 cut.minCharge1(),
                 opts.minTDC,
                 opts.maxTDC,
                 cut.cut(HFPhase1PMTData::T_1_MIN),
                 cut.cut(HFPhase1PMTData::T_1_MAX),
                 opts,
                 bin,
                 false,
                 frame);
  }
  pad->Draw();

  pad = canv->cd(3);
  {
    TH1F* frame = nullptr;
    if (ref)
      frame = plotFunctors(pad,
                           "Charge Asymmetry",
                           "Asymmetry Cuts",
                           ref->minChargeAsymm(),
                           opts.minAsymm,
                           opts.maxAsymm,
                           ref->cut(HFPhase1PMTData::ASYMM_MIN),
                           ref->cut(HFPhase1PMTData::ASYMM_MAX),
                           opts,
                           bin,
                           true,
                           frame);
    plotFunctors(pad,
                 "Charge Asymmetry",
                 "Asymmetry Cuts",
                 cut.minChargeAsymm(),
                 opts.minAsymm,
                 opts.maxAsymm,
                 cut.cut(HFPhase1PMTData::ASYMM_MIN),
                 cut.cut(HFPhase1PMTData::ASYMM_MAX),
                 opts,
                 bin,
                 false,
                 frame);
  }
  pad->Draw();

  canv->Write();
}

void visualizeHFPhase1PMTParams(const std::vector<HcalDetId>& idVec,
                                const HFPhase1PMTParams& cuts,
                                const VisualizationOptions& options,
                                const HFPhase1PMTParams* reference) {
  using namespace std;

  const unsigned n = idVec.size();
  if (options.verbose) {
    cout << "Cut visualization is requested for the following PMTs:\n";
    for (unsigned i = 0; i < n; ++i)
      cout << idVec[i] << '\n';
    cout.flush();
  }

  // Plot the default cut
  const HFPhase1PMTData* defaultCut = cuts.getDefault();
  const HFPhase1PMTData* defaultRef = nullptr;
  if (reference)
    defaultRef = reference->getDefault();
  if (defaultCut)
    plotConfig(HcalDetId(), HcalIndexLookup::InvalidIndex, *defaultCut, options, defaultRef);

  // Plot all other cuts
  for (unsigned i = 0; i < n; ++i) {
    const HcalDetId& id = idVec[i];
    const unsigned tableIndex = cuts.getIndex(id);
    if (tableIndex == HcalIndexLookup::InvalidIndex) {
      if (defaultCut)
        cout << "PMT " << id << " is using default config" << endl;
      else
        cout << "ERROR! No configuration found for PMT " << id << endl;
    } else {
      cout << "PMT " << id << " is using config " << tableIndex << endl;
      const HFPhase1PMTData* cut = cuts.getByIndex(tableIndex);
      assert(cut);
      const HFPhase1PMTData* ref = nullptr;
      if (reference)
        ref = reference->getByIndex(tableIndex);
      plotConfig(id, tableIndex, *cut, options, ref);
    }
  }
}