Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:43

0001 #include "DPGAnalysis/SiStripTools/interface/RunHistogramManager.h"
0002 #include "FWCore/Framework/interface/Run.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ServiceRegistry/interface/Service.h"
0005 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0006 #include "TH1F.h"
0007 #include "TH2F.h"
0008 #include "TProfile.h"
0009 #include "TProfile2D.h"
0010 
0011 BaseHistoParams::BaseHistoParams() {}
0012 
0013 BaseHistoParams::~BaseHistoParams() {}
0014 
0015 /*
0016 void BaseHistoParams::beginRun(const edm::Run& iRun, TFileDirectory& subrun) {
0017 
0018   beginRun(iRun.run(),subrun);
0019 
0020 }
0021 */
0022 
0023 RunHistogramManager::RunHistogramManager(edm::ConsumesCollector& iC, const bool fillHistograms)
0024     : _fillHistograms(fillHistograms),
0025       _histograms(),
0026       _conditionsInRunToken(iC.consumes<edm::ConditionsInRunBlock, edm::InRun>(edm::InputTag("conditionsInEdm"))) {}
0027 
0028 RunHistogramManager::RunHistogramManager(edm::ConsumesCollector&& iC, const bool fillHistograms)
0029     : _fillHistograms(fillHistograms),
0030       _histograms(),
0031       _conditionsInRunToken(iC.consumes<edm::ConditionsInRunBlock, edm::InRun>(edm::InputTag("conditionsInEdm"))) {}
0032 
0033 TH1F** RunHistogramManager::makeTH1F(
0034     const char* name, const char* title, const unsigned int nbinx, const double xmin, const double xmax) {
0035   TH1F** pointer = new TH1F*(nullptr);
0036 
0037   BaseHistoParams* hp = new HistoParams<TH1F>(pointer, "TH1F", name, title, nbinx, xmin, xmax);
0038   _histograms.push_back(hp);
0039 
0040   LogDebug("TH1Fmade") << "Histogram " << name << " " << title << " pre-booked:" << _histograms.size();
0041 
0042   return pointer;
0043 }
0044 
0045 RunHistogramManager::~RunHistogramManager() {
0046   for (std::vector<BaseHistoParams*>::const_iterator hp = _histograms.begin(); hp != _histograms.end(); ++hp) {
0047     delete *hp;
0048   }
0049   LogDebug("Destructor") << "All BaseHistoParams destroyed ";
0050 }
0051 
0052 TProfile** RunHistogramManager::makeTProfile(
0053     const char* name, const char* title, const unsigned int nbinx, const double xmin, const double xmax) {
0054   TProfile** pointer = new TProfile*(nullptr);
0055 
0056   BaseHistoParams* hp = new HistoParams<TProfile>(pointer, "TProfile", name, title, nbinx, xmin, xmax);
0057   _histograms.push_back(hp);
0058 
0059   LogDebug("TProfilemade") << "Histogram " << name << " " << title << " pre-booked:" << _histograms.size();
0060 
0061   return pointer;
0062 }
0063 
0064 TH2F** RunHistogramManager::makeTH2F(const char* name,
0065                                      const char* title,
0066                                      const unsigned int nbinx,
0067                                      const double xmin,
0068                                      const double xmax,
0069                                      const unsigned int nbiny,
0070                                      const double ymin,
0071                                      const double ymax) {
0072   TH2F** pointer = new TH2F*(nullptr);
0073 
0074   BaseHistoParams* hp = new HistoParams<TH2F>(pointer, "TH2F", name, title, nbinx, xmin, xmax, nbiny, ymin, ymax);
0075   _histograms.push_back(hp);
0076 
0077   LogDebug("TH2Fmade") << "Histogram " << name << " " << title << " pre-booked :" << _histograms.size();
0078 
0079   return pointer;
0080 }
0081 
0082 TProfile2D** RunHistogramManager::makeTProfile2D(const char* name,
0083                                                  const char* title,
0084                                                  const unsigned int nbinx,
0085                                                  const double xmin,
0086                                                  const double xmax,
0087                                                  const unsigned int nbiny,
0088                                                  const double ymin,
0089                                                  const double ymax) {
0090   TProfile2D** pointer = new TProfile2D*(nullptr);
0091 
0092   BaseHistoParams* hp =
0093       new HistoParams<TProfile2D>(pointer, "TProfile2D", name, title, nbinx, xmin, xmax, nbiny, ymin, ymax);
0094   _histograms.push_back(hp);
0095 
0096   LogDebug("TProfile2Dmade") << "Histogram " << name << " " << title << " pre-booked :" << _histograms.size();
0097 
0098   return pointer;
0099 }
0100 
0101 void RunHistogramManager::beginRun(const edm::Run& iRun) {
0102   edm::Service<TFileService> tfserv;
0103   beginRun(iRun, tfserv->tFileDirectory());
0104 }
0105 
0106 void RunHistogramManager::beginRun(const edm::Run& iRun, TFileDirectory& subdir) {
0107   if (!_fillHistograms) {
0108     beginRun(iRun.run(), subdir);
0109   } else {
0110     unsigned int fillnum = 0;
0111 
0112     edm::Handle<edm::ConditionsInRunBlock> cirb;
0113     iRun.getByToken(_conditionsInRunToken, cirb);
0114 
0115     if (!cirb.failedToGet() && cirb.isValid())
0116       fillnum = cirb->lhcFillNumber;
0117 
0118     beginRun(fillnum, subdir);
0119   }
0120 }
0121 
0122 void RunHistogramManager::beginRun(const unsigned int irun) {
0123   edm::Service<TFileService> tfserv;
0124   beginRun(irun, tfserv->tFileDirectory());
0125 }
0126 
0127 void RunHistogramManager::beginRun(const unsigned int irun, TFileDirectory& subdir) {
0128   // create/go to the run subdirectory
0129 
0130   char fillrun[30];
0131 
0132   if (!_fillHistograms) {
0133     sprintf(fillrun, "%s", "run");
0134   } else {
0135     sprintf(fillrun, "%s", "fill");
0136   }
0137 
0138   char dirname[300];
0139   sprintf(dirname, "%s_%d", fillrun, irun);
0140   TFileDirectory subrun = subdir.mkdir(dirname);
0141 
0142   // loop on the histograms and update the pointer references
0143 
0144   for (unsigned int ih = 0; ih < _histograms.size(); ++ih) {
0145     _histograms[ih]->beginRun(irun, subrun, fillrun);
0146   }
0147 }