CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CommonTools/Utils/interface/ExpressionHisto.h

Go to the documentation of this file.
00001 #ifndef CommonTools_Utils_ExpressionHisto_h
00002 #define CommonTools_Utils_ExpressionHisto_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     UtilAlgos
00006 // Class  :     ExpressionHisto
00007 // 
00016 //
00017 // Original Author: Benedikt HEGNER
00018 //         Created:  Fri Jun  1 14:35:22 CEST 2007
00019 // $Id: ExpressionHisto.h,v 1.1 2010/05/04 10:36:06 hegner Exp $
00020 //
00021 
00022 // system include files
00023 
00024 // user include files
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 #include "CommonTools/Utils/interface/TFileDirectory.h"
00027 #include "CommonTools/Utils/interface/StringObjectFunction.h"
00028 
00029 #include "TFile.h"
00030 #include "TH1F.h"
00031 #include "TH1.h"
00032 
00033 template<typename T>
00034 class ExpressionHisto {
00035 public:
00036   ExpressionHisto(const edm::ParameterSet& iConfig);
00037   ~ExpressionHisto();
00038   
00039   void initialize(TFileDirectory& fs);
00045   bool fill(const T& element, double weight=1.0, uint32_t i=0);  
00046   
00047 private:
00048   double min, max;
00049   int nbins;
00050   std::string name, description;
00051   uint32_t nhistos;
00052   bool     separatePlots;
00053   TH1F ** hist;
00054   StringObjectFunction<T> function;      
00055 };
00056 
00057 template<typename T>
00058 ExpressionHisto<T>::ExpressionHisto(const edm::ParameterSet& iConfig):
00059   min(iConfig.template getUntrackedParameter<double>("min")),
00060   max(iConfig.template getUntrackedParameter<double>("max")),
00061   nbins(iConfig.template getUntrackedParameter<int>("nbins")),
00062   name(iConfig.template getUntrackedParameter<std::string>("name")),
00063   description(iConfig.template getUntrackedParameter<std::string>("description")),
00064   function(iConfig.template getUntrackedParameter<std::string>("plotquantity"), 
00065            iConfig.template getUntrackedParameter<bool>("lazyParsing", false)) {
00066   int32_t itemsToPlot = iConfig.template getUntrackedParameter<int32_t>("itemsToPlot", -1);
00067   if (itemsToPlot <= 0) {
00068       nhistos = 1; separatePlots = false;
00069   } else {
00070       nhistos = itemsToPlot; separatePlots = true;
00071   }
00072 }
00073 
00074 template<typename T>
00075 ExpressionHisto<T>::~ExpressionHisto() {
00076 }
00077 
00078 template<typename T>
00079 void ExpressionHisto<T>::initialize(TFileDirectory& fs) 
00080 {
00081    hist = new TH1F*[nhistos];
00082    char buff[1024],baff[1024];
00083    if (separatePlots) {
00084        for (uint32_t i = 0; i < nhistos; i++) {
00085                if (strstr(name.c_str(), "%d") != 0) {
00086                        snprintf(buff, 1024, name.c_str(), i+1);
00087                } else {
00088                        snprintf(buff, 1024, "%s [#%d]", name.c_str(), i+1);
00089                }
00090                if (strstr(description.c_str(), "%d") != 0) {
00091                        snprintf(baff, 1024, description.c_str(), i+1);
00092                } else {
00093                        snprintf(baff, 1024, "%s [#%d]", description.c_str(), i+1);
00094                }
00095                hist[i] = fs.make<TH1F>(buff,baff,nbins,min,max);
00096        }
00097     } else {
00098        hist[0] = fs.make<TH1F>(name.c_str(),description.c_str(),nbins,min,max);
00099     }
00100 }
00101 
00102 template<typename T>
00103 bool ExpressionHisto<T>::fill(const T& element, double weight, uint32_t i) 
00104 {
00105   if (!separatePlots) hist[0]->Fill( function(element), weight );
00106   else if (i < nhistos)  hist[i]->Fill( function(element), weight );
00107   else return false;
00108   return true;
00109 }
00110 
00111 #endif