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
#include "DQMOffline/RecoB/plugins/BTagPerformanceHarvester.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DQMOffline/RecoB/interface/Tools.h"
#include "DQMOffline/RecoB/interface/TagInfoPlotterFactory.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/MakerMacros.h"

using namespace edm;
using namespace std;
using namespace RecoBTag;

BTagPerformanceHarvester::BTagPerformanceHarvester(const edm::ParameterSet& pSet)
    : etaRanges(pSet.getParameter<vector<double>>("etaRanges")),
      ptRanges(pSet.getParameter<vector<double>>("ptRanges")),
      produceEps(pSet.getParameter<bool>("produceEps")),
      producePs(pSet.getParameter<bool>("producePs")),
      moduleConfig(pSet.getParameter<vector<edm::ParameterSet>>("tagConfig")),
      flavPlots_(pSet.getParameter<std::string>("flavPlots")),
      makeDiffPlots_(pSet.getParameter<bool>("differentialPlots")) {
  //mcPlots_ : 1=b+c+l+ni; 2=all+1; 3=1+d+u+s+g; 4=3+all . Default is 2. Don't use 0.
  if (flavPlots_.find("dusg") < 15) {
    if (flavPlots_.find("all") < 15)
      mcPlots_ = 4;
    else
      mcPlots_ = 3;
  } else if (flavPlots_.find("bcl") < 15) {
    if (flavPlots_.find("all") < 15)
      mcPlots_ = 2;
    else
      mcPlots_ = 1;
  } else
    mcPlots_ = 0;

  if (etaRanges.size() <= 1)
    etaRanges = {pSet.getParameter<double>("etaMin"), pSet.getParameter<double>("etaMax")};
  if (ptRanges.size() <= 1)
    ptRanges = {pSet.getParameter<double>("ptRecJetMin"), pSet.getParameter<double>("ptRecJetMax")};

  for (vector<edm::ParameterSet>::const_iterator iModule = moduleConfig.begin(); iModule != moduleConfig.end();
       ++iModule) {
    const string& dataFormatType = iModule->exists("type") ? iModule->getParameter<string>("type") : "JetTag";
    if (dataFormatType == "JetTag") {
      const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
      jetTagInputTags.push_back(moduleLabel);
      binJetTagPlotters.push_back(vector<std::shared_ptr<JetTagPlotter>>());
      if (mcPlots_ && makeDiffPlots_) {
        differentialPlots.push_back(vector<std::unique_ptr<BTagDifferentialPlot>>());
      }
    } else if (dataFormatType == "TagCorrelation") {
      const InputTag& label1 = iModule->getParameter<InputTag>("label1");
      const InputTag& label2 = iModule->getParameter<InputTag>("label2");
      tagCorrelationInputTags.push_back(std::pair<edm::InputTag, edm::InputTag>(label1, label2));
      binTagCorrelationPlotters.push_back(vector<std::unique_ptr<TagCorrelationPlotter>>());
    } else {
      tagInfoInputTags.push_back(vector<edm::InputTag>());
      tiDataFormatType.push_back(dataFormatType);
      binTagInfoPlotters.push_back(vector<std::unique_ptr<BaseTagInfoPlotter>>());
    }
  }
}

EtaPtBin BTagPerformanceHarvester::getEtaPtBin(const int& iEta, const int& iPt) {
  // DEFINE BTagBin:
  bool etaActive_, ptActive_;
  double etaMin_, etaMax_, ptMin_, ptMax_;

  if (iEta != -1) {
    etaActive_ = true;
    etaMin_ = etaRanges[iEta];
    etaMax_ = etaRanges[iEta + 1];
  } else {
    etaActive_ = false;
    etaMin_ = etaRanges[0];
    etaMax_ = etaRanges[etaRanges.size() - 1];
  }

  if (iPt != -1) {
    ptActive_ = true;
    ptMin_ = ptRanges[iPt];
    ptMax_ = ptRanges[iPt + 1];
  } else {
    ptActive_ = false;
    ptMin_ = ptRanges[0];
    ptMax_ = ptRanges[ptRanges.size() - 1];
  }
  return EtaPtBin(etaActive_, etaMin_, etaMax_, ptActive_, ptMin_, ptMax_);
}

BTagPerformanceHarvester::~BTagPerformanceHarvester() {}

void BTagPerformanceHarvester::dqmEndJob(DQMStore::IBooker& ibook, DQMStore::IGetter& iget) {
  // Book all histograms.

  // iterate over ranges:
  const int iEtaStart = -1;  // this will be the inactive one
  const int iEtaEnd = etaRanges.size() > 2 ? etaRanges.size() - 1
                                           : 0;  // if there is only one bin defined, leave it as the inactive one
  const int iPtStart = -1;                       // this will be the inactive one
  const int iPtEnd =
      ptRanges.size() > 2 ? ptRanges.size() - 1 : 0;  // if there is only one bin defined, leave it as the inactive one
  setTDRStyle();

  TagInfoPlotterFactory theFactory;
  int iTag = -1;
  int iTagCorr = -1;
  int iInfoTag = -1;
  for (vector<edm::ParameterSet>::const_iterator iModule = moduleConfig.begin(); iModule != moduleConfig.end();
       ++iModule) {
    const string& dataFormatType = iModule->exists("type") ? iModule->getParameter<string>("type") : "JetTag";
    const bool& doCTagPlots = iModule->exists("doCTagPlots") ? iModule->getParameter<bool>("doCTagPlots") : false;

    if (dataFormatType == "JetTag") {
      iTag++;
      const string& folderName = iModule->getParameter<string>("folder");

      // Contains plots for each bin of rapidity and pt.
      auto differentialPlotsConstantEta = std::make_unique<std::vector<std::unique_ptr<BTagDifferentialPlot>>>();
      auto differentialPlotsConstantPt = std::make_unique<std::vector<std::unique_ptr<BTagDifferentialPlot>>>();
      if (mcPlots_ && makeDiffPlots_) {
        // the constant b-efficiency for the differential plots versus pt and eta
        const double& effBConst =
            iModule->getParameter<edm::ParameterSet>("parameters").getParameter<double>("effBConst");

        // the objects for the differential plots vs. eta,pt for
        for (int iEta = iEtaStart; iEta < iEtaEnd; iEta++) {
          std::unique_ptr<BTagDifferentialPlot> etaConstDifferentialPlot =
              std::make_unique<BTagDifferentialPlot>(effBConst, BTagDifferentialPlot::constETA, folderName, mcPlots_);
          differentialPlotsConstantEta->push_back(std::move(etaConstDifferentialPlot));
        }

        for (int iPt = iPtStart; iPt < iPtEnd; iPt++) {
          // differentialPlots for this pt bin
          std::unique_ptr<BTagDifferentialPlot> ptConstDifferentialPlot =
              std::make_unique<BTagDifferentialPlot>(effBConst, BTagDifferentialPlot::constPT, folderName, mcPlots_);
          differentialPlotsConstantPt->push_back(std::move(ptConstDifferentialPlot));
        }
      }
      // eta loop
      for (int iEta = iEtaStart; iEta < iEtaEnd; iEta++) {
        // pt loop
        for (int iPt = iPtStart; iPt < iPtEnd; iPt++) {
          const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);

          // Instantiate the generic b tag plotter
          bool doDifferentialPlots =
              iModule->exists("differentialPlots") && iModule->getParameter<bool>("differentialPlots") == true;
          std::shared_ptr<JetTagPlotter> jetTagPlotter =
              std::make_shared<JetTagPlotter>(folderName,
                                              etaPtBin,
                                              iModule->getParameter<edm::ParameterSet>("parameters"),
                                              mcPlots_,
                                              true,
                                              ibook,
                                              doCTagPlots,
                                              doDifferentialPlots);
          binJetTagPlotters.at(iTag).push_back(jetTagPlotter);

          // Add to the corresponding differential plotters
          if (mcPlots_ && makeDiffPlots_) {
            (*differentialPlotsConstantEta)[iEta + 1]->addBinPlotter(jetTagPlotter);
            (*differentialPlotsConstantPt)[iPt + 1]->addBinPlotter(jetTagPlotter);
          }
        }
      }
      // the objects for the differential plots vs. eta, pt: collect all from constant eta and constant pt
      if (mcPlots_ && makeDiffPlots_) {
        differentialPlots.at(iTag).reserve(differentialPlotsConstantEta->size() + differentialPlotsConstantPt->size());
        differentialPlots.at(iTag).insert(differentialPlots.at(iTag).end(),
                                          std::make_move_iterator(differentialPlotsConstantEta->begin()),
                                          std::make_move_iterator(differentialPlotsConstantEta->end()));
        differentialPlots.at(iTag).insert(differentialPlots.at(iTag).end(),
                                          std::make_move_iterator(differentialPlotsConstantPt->begin()),
                                          std::make_move_iterator(differentialPlotsConstantPt->end()));

        edm::LogInfo("Info") << "====>>>> ## sizeof differentialPlots = " << differentialPlots.size();
      }
    } else if (dataFormatType == "TagCorrelation") {
      iTagCorr++;
      const InputTag& label1 = iModule->getParameter<InputTag>("label1");
      const InputTag& label2 = iModule->getParameter<InputTag>("label2");

      // eta loop
      for (int iEta = iEtaStart; iEta != iEtaEnd; ++iEta) {
        // pt loop
        for (int iPt = iPtStart; iPt != iPtEnd; ++iPt) {
          const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
          // Instantiate the generic b tag correlation plotter
          std::unique_ptr<TagCorrelationPlotter> tagCorrelationPlotter =
              std::make_unique<TagCorrelationPlotter>(label1.label(),
                                                      label2.label(),
                                                      etaPtBin,
                                                      iModule->getParameter<edm::ParameterSet>("parameters"),
                                                      mcPlots_,
                                                      doCTagPlots,
                                                      true,
                                                      ibook);
          binTagCorrelationPlotters.at(iTagCorr).push_back(std::move(tagCorrelationPlotter));
        }
      }
    } else {
      iInfoTag++;
      // tag info retrievel is deferred(needs availability of EventSetup)
      const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
      const string& folderName = iModule->getParameter<string>("folder");
      // eta loop
      for (int iEta = iEtaStart; iEta < iEtaEnd; iEta++) {
        // pt loop
        for (int iPt = iPtStart; iPt < iPtEnd; iPt++) {
          const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);

          // Instantiate the tagInfo plotter

          std::unique_ptr<BaseTagInfoPlotter> jetTagPlotter =
              theFactory.buildPlotter(dataFormatType,
                                      moduleLabel.label(),
                                      etaPtBin,
                                      iModule->getParameter<edm::ParameterSet>("parameters"),
                                      folderName,
                                      mcPlots_,
                                      true,
                                      ibook);
          binTagInfoPlotters.at(iInfoTag).push_back(std::move(jetTagPlotter));
        }
      }
      edm::LogInfo("Info") << "====>>>> ## sizeof differentialPlots = " << differentialPlots.size();
    }
  }

  setTDRStyle();
  for (unsigned int iJetLabel = 0; iJetLabel != binJetTagPlotters.size(); ++iJetLabel) {
    int plotterSize = binJetTagPlotters[iJetLabel].size();
    for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
      binJetTagPlotters[iJetLabel][iPlotter]->finalize(ibook, iget);
      if (producePs)
        (*binJetTagPlotters[iJetLabel][iPlotter]).psPlot(psBaseName);
      if (produceEps)
        (*binJetTagPlotters[iJetLabel][iPlotter]).epsPlot(epsBaseName);
    }

    if (makeDiffPlots_) {
      for (auto& iPlotter : differentialPlots[iJetLabel]) {
        iPlotter->process(ibook);
        if (producePs)
          iPlotter->psPlot(psBaseName);
        if (produceEps)
          iPlotter->epsPlot(epsBaseName);
      }
    }
  }

  for (auto& iJetLabel : binTagInfoPlotters) {
    for (auto& iPlotter : iJetLabel) {
      iPlotter->finalize(ibook, iget);
      if (producePs)
        iPlotter->psPlot(psBaseName);
      if (produceEps)
        iPlotter->epsPlot(epsBaseName);
    }
  }
  for (unsigned int iJetLabel = 0; iJetLabel != binTagCorrelationPlotters.size(); ++iJetLabel) {
    int plotterSize = binTagCorrelationPlotters[iJetLabel].size();
    for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
      binTagCorrelationPlotters[iJetLabel][iPlotter]->finalize(ibook, iget);
      if (producePs)
        (*binTagCorrelationPlotters[iJetLabel][iPlotter]).psPlot(psBaseName);
      if (produceEps)
        (*binTagCorrelationPlotters[iJetLabel][iPlotter]).epsPlot(epsBaseName);
    }
  }
}

//define this as a plug-in
DEFINE_FWK_MODULE(BTagPerformanceHarvester);