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
#include "DPGAnalysis/SiStripTools/interface/RunHistogramManager.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TProfile.h"
#include "TProfile2D.h"

BaseHistoParams::BaseHistoParams() {}

BaseHistoParams::~BaseHistoParams() {}

/*
void BaseHistoParams::beginRun(const edm::Run& iRun, TFileDirectory& subrun) {

  beginRun(iRun.run(),subrun);

}
*/

RunHistogramManager::RunHistogramManager(edm::ConsumesCollector& iC, const bool fillHistograms)
    : _fillHistograms(fillHistograms),
      _histograms(),
      _conditionsInRunToken(iC.consumes<edm::ConditionsInRunBlock, edm::InRun>(edm::InputTag("conditionsInEdm"))) {}

RunHistogramManager::RunHistogramManager(edm::ConsumesCollector&& iC, const bool fillHistograms)
    : _fillHistograms(fillHistograms),
      _histograms(),
      _conditionsInRunToken(iC.consumes<edm::ConditionsInRunBlock, edm::InRun>(edm::InputTag("conditionsInEdm"))) {}

TH1F** RunHistogramManager::makeTH1F(
    const char* name, const char* title, const unsigned int nbinx, const double xmin, const double xmax) {
  TH1F** pointer = new TH1F*(nullptr);

  BaseHistoParams* hp = new HistoParams<TH1F>(pointer, "TH1F", name, title, nbinx, xmin, xmax);
  _histograms.push_back(hp);

  LogDebug("TH1Fmade") << "Histogram " << name << " " << title << " pre-booked:" << _histograms.size();

  return pointer;
}

RunHistogramManager::~RunHistogramManager() {
  for (std::vector<BaseHistoParams*>::const_iterator hp = _histograms.begin(); hp != _histograms.end(); ++hp) {
    delete *hp;
  }
  LogDebug("Destructor") << "All BaseHistoParams destroyed ";
}

TProfile** RunHistogramManager::makeTProfile(
    const char* name, const char* title, const unsigned int nbinx, const double xmin, const double xmax) {
  TProfile** pointer = new TProfile*(nullptr);

  BaseHistoParams* hp = new HistoParams<TProfile>(pointer, "TProfile", name, title, nbinx, xmin, xmax);
  _histograms.push_back(hp);

  LogDebug("TProfilemade") << "Histogram " << name << " " << title << " pre-booked:" << _histograms.size();

  return pointer;
}

TH2F** RunHistogramManager::makeTH2F(const char* name,
                                     const char* title,
                                     const unsigned int nbinx,
                                     const double xmin,
                                     const double xmax,
                                     const unsigned int nbiny,
                                     const double ymin,
                                     const double ymax) {
  TH2F** pointer = new TH2F*(nullptr);

  BaseHistoParams* hp = new HistoParams<TH2F>(pointer, "TH2F", name, title, nbinx, xmin, xmax, nbiny, ymin, ymax);
  _histograms.push_back(hp);

  LogDebug("TH2Fmade") << "Histogram " << name << " " << title << " pre-booked :" << _histograms.size();

  return pointer;
}

TProfile2D** RunHistogramManager::makeTProfile2D(const char* name,
                                                 const char* title,
                                                 const unsigned int nbinx,
                                                 const double xmin,
                                                 const double xmax,
                                                 const unsigned int nbiny,
                                                 const double ymin,
                                                 const double ymax) {
  TProfile2D** pointer = new TProfile2D*(nullptr);

  BaseHistoParams* hp =
      new HistoParams<TProfile2D>(pointer, "TProfile2D", name, title, nbinx, xmin, xmax, nbiny, ymin, ymax);
  _histograms.push_back(hp);

  LogDebug("TProfile2Dmade") << "Histogram " << name << " " << title << " pre-booked :" << _histograms.size();

  return pointer;
}

void RunHistogramManager::beginRun(const edm::Run& iRun) {
  edm::Service<TFileService> tfserv;
  beginRun(iRun, tfserv->tFileDirectory());
}

void RunHistogramManager::beginRun(const edm::Run& iRun, TFileDirectory& subdir) {
  if (!_fillHistograms) {
    beginRun(iRun.run(), subdir);
  } else {
    unsigned int fillnum = 0;

    edm::Handle<edm::ConditionsInRunBlock> cirb;
    iRun.getByToken(_conditionsInRunToken, cirb);

    if (!cirb.failedToGet() && cirb.isValid())
      fillnum = cirb->lhcFillNumber;

    beginRun(fillnum, subdir);
  }
}

void RunHistogramManager::beginRun(const unsigned int irun) {
  edm::Service<TFileService> tfserv;
  beginRun(irun, tfserv->tFileDirectory());
}

void RunHistogramManager::beginRun(const unsigned int irun, TFileDirectory& subdir) {
  // create/go to the run subdirectory

  char fillrun[30];

  if (!_fillHistograms) {
    sprintf(fillrun, "%s", "run");
  } else {
    sprintf(fillrun, "%s", "fill");
  }

  char dirname[300];
  sprintf(dirname, "%s_%d", fillrun, irun);
  TFileDirectory subrun = subdir.mkdir(dirname);

  // loop on the histograms and update the pointer references

  for (unsigned int ih = 0; ih < _histograms.size(); ++ih) {
    _histograms[ih]->beginRun(irun, subrun, fillrun);
  }
}