ExpressionHisto

Macros

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
#ifndef CommonTools_Utils_ExpressionHisto_h
#define CommonTools_Utils_ExpressionHisto_h
// -*- C++ -*-
//
// Package:     UtilAlgos
// Class  :     ExpressionHisto
//
/**\class ExpressionHisto ExpressionHisto.h CommonTools/Utils/interface/ExpressionHisto.h

 Description: Histogram tool using expressions 

 Usage:
    <usage>

*/
//
// Original Author: Benedikt HEGNER
//         Created:  Fri Jun  1 14:35:22 CEST 2007
// $Id: ExpressionHisto.h,v 1.2 2009/07/09 10:52:01 gpetrucc Exp $
//

// system include files

// user include files
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "CommonTools/Utils/interface/TFileDirectory.h"
#include "CommonTools/Utils/interface/StringObjectFunction.h"

#include "TFile.h"
#include "TH1F.h"
#include "TH1.h"

template <typename T>
class ExpressionHisto {
public:
  ExpressionHisto(const edm::ParameterSet& iConfig);
  ~ExpressionHisto();

  void initialize(TFileDirectory& fs);
  /** Plot the quantity for the specified element and index.
    Returns true if the quantity has been plotted, false otherwise.
    A return value of "false" means "please don't send any more elements".
    The default "i = 0" is to keep backwards compatibility with usages outside
    HistoAnalyzer */
  bool fill(const T& element, double weight = 1.0, uint32_t i = 0);

private:
  double min, max;
  int nbins;
  std::string name, description;
  uint32_t nhistos;
  bool separatePlots;
  TH1F** hist;
  StringObjectFunction<T> function;
};

template <typename T>
ExpressionHisto<T>::ExpressionHisto(const edm::ParameterSet& iConfig)
    : min(iConfig.template getUntrackedParameter<double>("min")),
      max(iConfig.template getUntrackedParameter<double>("max")),
      nbins(iConfig.template getUntrackedParameter<int>("nbins")),
      name(iConfig.template getUntrackedParameter<std::string>("name")),
      description(iConfig.template getUntrackedParameter<std::string>("description")),
      function(iConfig.template getUntrackedParameter<std::string>("plotquantity"),
               iConfig.template getUntrackedParameter<bool>("lazyParsing", false)) {
  int32_t itemsToPlot = iConfig.template getUntrackedParameter<int32_t>("itemsToPlot", -1);
  if (itemsToPlot <= 0) {
    nhistos = 1;
    separatePlots = false;
  } else {
    nhistos = itemsToPlot;
    separatePlots = true;
  }
}

template <typename T>
ExpressionHisto<T>::~ExpressionHisto() {}

template <typename T>
void ExpressionHisto<T>::initialize(TFileDirectory& fs) {
  hist = new TH1F*[nhistos];
  char buff[1024], baff[1024];
  if (separatePlots) {
    for (uint32_t i = 0; i < nhistos; i++) {
      if (strstr(name.c_str(), "%d") != nullptr) {
        snprintf(buff, 1024, name.c_str(), i + 1);
      } else {
        snprintf(buff, 1024, "%s [#%d]", name.c_str(), i + 1);
      }
      if (strstr(description.c_str(), "%d") != nullptr) {
        snprintf(baff, 1024, description.c_str(), i + 1);
      } else {
        snprintf(baff, 1024, "%s [#%d]", description.c_str(), i + 1);
      }
      hist[i] = fs.make<TH1F>(buff, baff, nbins, min, max);
    }
  } else {
    hist[0] = fs.make<TH1F>(name.c_str(), description.c_str(), nbins, min, max);
  }
}

template <typename T>
bool ExpressionHisto<T>::fill(const T& element, double weight, uint32_t i) {
  if (!separatePlots)
    hist[0]->Fill(function(element), weight);
  else if (i < nhistos)
    hist[i]->Fill(function(element), weight);
  else
    return false;
  return true;
}

#endif