Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:18

0001 #ifndef CommonTools_Utils_ExpressionHisto_h
0002 #define CommonTools_Utils_ExpressionHisto_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     UtilAlgos
0006 // Class  :     ExpressionHisto
0007 //
0008 /**\class ExpressionHisto ExpressionHisto.h CommonTools/Utils/interface/ExpressionHisto.h
0009 
0010  Description: Histogram tool using expressions 
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author: Benedikt HEGNER
0018 //         Created:  Fri Jun  1 14:35:22 CEST 2007
0019 // $Id: ExpressionHisto.h,v 1.2 2009/07/09 10:52:01 gpetrucc Exp $
0020 //
0021 
0022 // system include files
0023 
0024 // user include files
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "CommonTools/Utils/interface/TFileDirectory.h"
0027 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0028 
0029 #include "TFile.h"
0030 #include "TH1F.h"
0031 #include "TH1.h"
0032 
0033 template <typename T>
0034 class ExpressionHisto {
0035 public:
0036   ExpressionHisto(const edm::ParameterSet& iConfig);
0037   ~ExpressionHisto();
0038 
0039   void initialize(TFileDirectory& fs);
0040   /** Plot the quantity for the specified element and index.
0041     Returns true if the quantity has been plotted, false otherwise.
0042     A return value of "false" means "please don't send any more elements".
0043     The default "i = 0" is to keep backwards compatibility with usages outside
0044     HistoAnalyzer */
0045   bool fill(const T& element, double weight = 1.0, uint32_t i = 0);
0046 
0047 private:
0048   double min, max;
0049   int nbins;
0050   std::string name, description;
0051   uint32_t nhistos;
0052   bool separatePlots;
0053   TH1F** hist;
0054   StringObjectFunction<T> function;
0055 };
0056 
0057 template <typename T>
0058 ExpressionHisto<T>::ExpressionHisto(const edm::ParameterSet& iConfig)
0059     : min(iConfig.template getUntrackedParameter<double>("min")),
0060       max(iConfig.template getUntrackedParameter<double>("max")),
0061       nbins(iConfig.template getUntrackedParameter<int>("nbins")),
0062       name(iConfig.template getUntrackedParameter<std::string>("name")),
0063       description(iConfig.template getUntrackedParameter<std::string>("description")),
0064       function(iConfig.template getUntrackedParameter<std::string>("plotquantity"),
0065                iConfig.template getUntrackedParameter<bool>("lazyParsing", false)) {
0066   int32_t itemsToPlot = iConfig.template getUntrackedParameter<int32_t>("itemsToPlot", -1);
0067   if (itemsToPlot <= 0) {
0068     nhistos = 1;
0069     separatePlots = false;
0070   } else {
0071     nhistos = itemsToPlot;
0072     separatePlots = true;
0073   }
0074 }
0075 
0076 template <typename T>
0077 ExpressionHisto<T>::~ExpressionHisto() {}
0078 
0079 template <typename T>
0080 void ExpressionHisto<T>::initialize(TFileDirectory& fs) {
0081   hist = new TH1F*[nhistos];
0082   char buff[1024], baff[1024];
0083   if (separatePlots) {
0084     for (uint32_t i = 0; i < nhistos; i++) {
0085       if (strstr(name.c_str(), "%d") != nullptr) {
0086         snprintf(buff, 1024, name.c_str(), i + 1);
0087       } else {
0088         snprintf(buff, 1024, "%s [#%d]", name.c_str(), i + 1);
0089       }
0090       if (strstr(description.c_str(), "%d") != nullptr) {
0091         snprintf(baff, 1024, description.c_str(), i + 1);
0092       } else {
0093         snprintf(baff, 1024, "%s [#%d]", description.c_str(), i + 1);
0094       }
0095       hist[i] = fs.make<TH1F>(buff, baff, nbins, min, max);
0096     }
0097   } else {
0098     hist[0] = fs.make<TH1F>(name.c_str(), description.c_str(), nbins, min, max);
0099   }
0100 }
0101 
0102 template <typename T>
0103 bool ExpressionHisto<T>::fill(const T& element, double weight, uint32_t i) {
0104   if (!separatePlots)
0105     hist[0]->Fill(function(element), weight);
0106   else if (i < nhistos)
0107     hist[i]->Fill(function(element), weight);
0108   else
0109     return false;
0110   return true;
0111 }
0112 
0113 #endif