Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:08:58

0001 #ifndef DPGAnalysis_SiStripTools_RunHistogramManager_H
0002 #define DPGAnalysis_SiStripTools_RunHistogramManager_H
0003 
0004 #include <vector>
0005 #include <map>
0006 #include <string>
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/Framework/interface/Run.h"
0009 #include "FWCore/Framework/interface/ConsumesCollector.h"
0010 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0011 #include "DataFormats/Common/interface/ConditionsInEdm.h"
0012 #include "TH2F.h"
0013 #include "TProfile2D.h"
0014 
0015 class TH1F;
0016 class TProfile;
0017 
0018 class BaseHistoParams {
0019 public:
0020   BaseHistoParams();
0021   virtual ~BaseHistoParams();
0022 
0023   //   virtual void beginRun(const edm::Run& iRun, TFileDirectory& subrun);
0024   virtual void beginRun(const unsigned int irun, TFileDirectory& subrun, const char* fillrun) = 0;
0025 };
0026 
0027 template <class T>
0028 class HistoParams : public BaseHistoParams {
0029 public:
0030   HistoParams(T** pointer,
0031               const std::string type,
0032               const std::string name,
0033               const std::string title,
0034               const unsigned int nbinx = -1,
0035               const double xmin = -1.,
0036               const double xmax = -1.,
0037               const unsigned int nbiny = -1,
0038               const double ymin = -1.,
0039               const double ymax = -1.)
0040       : BaseHistoParams(),
0041         _pointer(pointer),
0042         _type(type),
0043         _name(name),
0044         _title(title),
0045         _nbinx(nbinx),
0046         _xmin(xmin),
0047         _xmax(xmax),
0048         _nbiny(nbiny),
0049         _ymin(ymin),
0050         _ymax(ymax),
0051         _runpointers() {}
0052 
0053   ~HistoParams() override {
0054     delete _pointer;
0055     LogDebug("Destructor") << "Destroy " << _name;
0056   }
0057 
0058   void beginRun(const unsigned int irun, TFileDirectory& subrun, const char* fillrun) override {
0059     if (_runpointers.find(irun) != _runpointers.end()) {
0060       *_pointer = _runpointers[irun];
0061       LogDebug("TH1Fbooked") << "Histogram " << _name.c_str() << " already exists " << _runpointers[irun];
0062 
0063     } else {
0064       char title[400];
0065       sprintf(title, "%s %s %d", _title.c_str(), fillrun, irun);
0066 
0067       _runpointers[irun] = subrun.make<T>(_name.c_str(), title, _nbinx, _xmin, _xmax);
0068 
0069       *_pointer = _runpointers[irun];
0070       LogDebug("TH1Fbooked") << "Histogram " << _name.c_str() << " booked " << _runpointers[irun];
0071     }
0072   }
0073 
0074 private:
0075   T** _pointer;
0076   std::string _type;
0077   std::string _name;
0078   std::string _title;
0079   unsigned int _nbinx;
0080   double _xmin;
0081   double _xmax;
0082   unsigned int _nbiny;
0083   double _ymin;
0084   double _ymax;
0085   std::map<unsigned int, T*> _runpointers;
0086 };
0087 
0088 template <>
0089 class HistoParams<TH2F> : public BaseHistoParams {
0090 public:
0091   HistoParams(TH2F** pointer,
0092               const std::string type,
0093               const std::string name,
0094               const std::string title,
0095               const unsigned int nbinx = -1,
0096               const double xmin = -1.,
0097               const double xmax = -1.,
0098               const unsigned int nbiny = -1,
0099               const double ymin = -1.,
0100               const double ymax = -1.)
0101       : BaseHistoParams(),
0102         _pointer(pointer),
0103         _type(type),
0104         _name(name),
0105         _title(title),
0106         _nbinx(nbinx),
0107         _xmin(xmin),
0108         _xmax(xmax),
0109         _nbiny(nbiny),
0110         _ymin(ymin),
0111         _ymax(ymax),
0112         _runpointers() {}
0113 
0114   ~HistoParams() override {
0115     delete _pointer;
0116     LogDebug("TH2FDestructor") << "Destroy " << _name;
0117   }
0118 
0119   void beginRun(const unsigned int irun, TFileDirectory& subrun, const char* fillrun) override {
0120     if (_runpointers.find(irun) != _runpointers.end()) {
0121       *_pointer = _runpointers[irun];
0122       LogDebug("TH2Fbooked") << "Histogram " << _name.c_str() << " already exists " << _runpointers[irun];
0123 
0124     } else {
0125       char title[400];
0126       sprintf(title, "%s %s %d", _title.c_str(), fillrun, irun);
0127 
0128       _runpointers[irun] = subrun.make<TH2F>(_name.c_str(), title, _nbinx, _xmin, _xmax, _nbiny, _ymin, _ymax);
0129 
0130       *_pointer = _runpointers[irun];
0131       LogDebug("TH2Fbooked") << "Histogram " << _name.c_str() << " booked " << _runpointers[irun];
0132     }
0133   }
0134 
0135 private:
0136   TH2F** _pointer;
0137   std::string _type;
0138   std::string _name;
0139   std::string _title;
0140   unsigned int _nbinx;
0141   double _xmin;
0142   double _xmax;
0143   unsigned int _nbiny;
0144   double _ymin;
0145   double _ymax;
0146   std::map<unsigned int, TH2F*> _runpointers;
0147 };
0148 
0149 template <>
0150 class HistoParams<TProfile2D> : public BaseHistoParams {
0151 public:
0152   HistoParams(TProfile2D** pointer,
0153               const std::string type,
0154               const std::string name,
0155               const std::string title,
0156               const unsigned int nbinx = -1,
0157               const double xmin = -1.,
0158               const double xmax = -1.,
0159               const unsigned int nbiny = -1,
0160               const double ymin = -1.,
0161               const double ymax = -1.)
0162       : BaseHistoParams(),
0163         _pointer(pointer),
0164         _type(type),
0165         _name(name),
0166         _title(title),
0167         _nbinx(nbinx),
0168         _xmin(xmin),
0169         _xmax(xmax),
0170         _nbiny(nbiny),
0171         _ymin(ymin),
0172         _ymax(ymax),
0173         _runpointers() {}
0174 
0175   ~HistoParams() override {
0176     delete _pointer;
0177     LogDebug("TProfile2DDestructor") << "Destroy " << _name;
0178   }
0179 
0180   void beginRun(const unsigned int irun, TFileDirectory& subrun, const char* fillrun) override {
0181     if (_runpointers.find(irun) != _runpointers.end()) {
0182       *_pointer = _runpointers[irun];
0183       LogDebug("TProfile2Dbooked") << "Histogram " << _name.c_str() << " already exists " << _runpointers[irun];
0184 
0185     } else {
0186       char title[400];
0187       sprintf(title, "%s %s %d", _title.c_str(), fillrun, irun);
0188 
0189       _runpointers[irun] = subrun.make<TProfile2D>(_name.c_str(), title, _nbinx, _xmin, _xmax, _nbiny, _ymin, _ymax);
0190 
0191       *_pointer = _runpointers[irun];
0192       LogDebug("TProfile2Dbooked") << "Histogram " << _name.c_str() << " booked " << _runpointers[irun];
0193     }
0194   }
0195 
0196 private:
0197   TProfile2D** _pointer;
0198   std::string _type;
0199   std::string _name;
0200   std::string _title;
0201   unsigned int _nbinx;
0202   double _xmin;
0203   double _xmax;
0204   unsigned int _nbiny;
0205   double _ymin;
0206   double _ymax;
0207   std::map<unsigned int, TProfile2D*> _runpointers;
0208 };
0209 
0210 class RunHistogramManager {
0211 public:
0212   RunHistogramManager(edm::ConsumesCollector&& iC, const bool fillHistograms = false);
0213   RunHistogramManager(edm::ConsumesCollector& iC, const bool fillHistograms = false);
0214   ~RunHistogramManager();
0215 
0216   TH1F** makeTH1F(const char* name, const char* title, const unsigned int nbinx, const double xmin, const double xmax);
0217   TProfile** makeTProfile(
0218       const char* name, const char* title, const unsigned int nbinx, const double xmin, const double xmax);
0219   TH2F** makeTH2F(const char* name,
0220                   const char* title,
0221                   const unsigned int nbinx,
0222                   const double xmin,
0223                   const double xmax,
0224                   const unsigned int nbiny,
0225                   const double ymin,
0226                   const double ymax);
0227   TProfile2D** makeTProfile2D(const char* name,
0228                               const char* title,
0229                               const unsigned int nbinx,
0230                               const double xmin,
0231                               const double xmax,
0232                               const unsigned int nbiny,
0233                               const double ymin,
0234                               const double ymax);
0235 
0236   void beginRun(const edm::Run& iRun);
0237   void beginRun(const edm::Run& iRun, TFileDirectory& subdir);
0238   void beginRun(const unsigned int irun);
0239   void beginRun(const unsigned int irun, TFileDirectory& subdir);
0240 
0241 private:
0242   bool _fillHistograms;
0243   std::vector<BaseHistoParams*> _histograms;
0244   edm::EDGetTokenT<edm::ConditionsInRunBlock> _conditionsInRunToken;
0245 };
0246 
0247 #endif  // DPGAnalysis_SiStripTools_RunHistogramManager_H