CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Mixing/Base/interface/PileUp.h

Go to the documentation of this file.
00001 #ifndef Base_PileUp_h
00002 #define Base_PileUp_h
00003 
00004 #include <string>
00005 #include <vector>
00006 #include <boost/bind.hpp>
00007 #include "FWCore/Framework/interface/Frameworkfwd.h"
00008 #include "FWCore/Sources/interface/VectorInputSource.h"
00009 #include "DataFormats/Provenance/interface/EventID.h"
00010 #include "FWCore/Framework/interface/EventPrincipal.h"
00011 
00012 #include "CLHEP/Random/RandPoissonQ.h"
00013 #include "CLHEP/Random/RandFlat.h"
00014 
00015 #include "TRandom.h"
00016 #include "TFile.h"
00017 #include "TH1F.h"
00018 
00019 class TFile;
00020 class TH1F;
00021 
00022 namespace CLHEP {
00023   class RandPoissonQ;
00024   class RandPoisson;
00025 }
00026 
00027 
00028 
00029 namespace edm {
00030   class PileUp {
00031   public:
00032     explicit PileUp(ParameterSet const& pset, double averageNumber, TH1F* const histo, const bool playback);
00033     ~PileUp();
00034 
00035     template<typename T>
00036       void readPileUp(std::vector<edm::EventID> &ids, T eventOperator, const int NumPU );
00037 
00038     template<typename T>
00039       void playPileUp(const std::vector<edm::EventID> &ids, T eventOperator);
00040 
00041     double averageNumber() const {return averageNumber_;}
00042     bool poisson() const {return poisson_;}
00043     bool doPileUp() {return none_ ? false :  averageNumber_>0.;}
00044     void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
00045       input_->dropUnwantedBranches(wantedBranches);
00046     }
00047     void endJob () {
00048       input_->doEndJob();
00049     }
00050 
00051     void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions);
00052 
00053     //template<typename T>
00054     // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
00055         //                        std::vector<edm::EventID> &ids, T& eventOperator);
00056 
00057   private:
00058     std::string const type_;
00059     double const averageNumber_;
00060     int const intAverage_;
00061     TH1F* const histo_;
00062     bool const histoDistribution_;
00063     bool const probFunctionDistribution_;
00064     bool const poisson_;
00065     bool const fixed_;
00066     bool const none_;
00067     bool manage_OOT_;
00068     bool poisson_OOT_;
00069     bool fixed_OOT_;
00070     int  intFixed_OOT_;
00071 
00072     VectorInputSource * const input_;
00073     CLHEP::RandPoissonQ *poissonDistribution_;
00074     CLHEP::RandPoisson  *poissonDistr_OOT_;
00075 
00076 
00077     TH1F *h1f;
00078     TH1F *hprobFunction;
00079     TFile *probFileHisto;
00080     
00081     //playback info
00082     bool playback_;
00083 
00084     // sequential reading
00085     bool sequential_;
00086     
00087     // read the seed for the histo and probability function cases
00088     int seed_;
00089   };
00090 
00091 
00092 
00093   template<typename T>
00094   class RecordEventID
00095   {
00096   private:
00097     std::vector<edm::EventID>& ids_;
00098     T& eventOperator_;
00099     int eventCount ;
00100   public:
00101     RecordEventID(std::vector<edm::EventID>& ids, T& eventOperator)
00102       : ids_(ids), eventOperator_(eventOperator), eventCount( 0 ) {}
00103     void operator()(EventPrincipal const& eventPrincipal) {
00104       ids_.push_back(eventPrincipal.id());
00105       eventOperator_(eventPrincipal, ++eventCount);
00106     }
00107   };
00108 
00109 
00117   template<typename T>
00118   void
00119     PileUp::readPileUp(std::vector<edm::EventID> &ids, T eventOperator, const int pileEventCnt) {
00120 
00121     // One reason PileUp is responsible for recording event IDs is
00122     // that it is the one that knows how many events will be read.
00123     ids.reserve(pileEventCnt);
00124     RecordEventID<T> recorder(ids,eventOperator);
00125     if (sequential_) {
00126       // boost::bind creates a functor from recordEventForPlayback
00127       // so that recordEventForPlayback can insert itself before
00128       // the original eventOperator.
00129 
00130       input_->loopSequential(pileEventCnt, recorder);
00131       //boost::bind(&PileUp::recordEventForPlayback<T>,
00132       //                    boost::ref(*this), _1, boost::ref(ids),
00133       //                             boost::ref(eventOperator))
00134       //  );
00135         
00136     } else  {
00137       input_->loopRandom(pileEventCnt, recorder);
00138       //               boost::bind(&PileUp::recordEventForPlayback<T>,
00139       //                             boost::ref(*this), _1, boost::ref(ids),
00140       //                             boost::ref(eventOperator))
00141       //                 );
00142     }
00143   }
00144 
00145 
00146 
00147   template<typename T>
00148   void
00149     PileUp::playPileUp(const std::vector<edm::EventID> &ids, T eventOperator) {
00150     //TrueNumInteractions.push_back( ids.size() ) ;
00151     input_->loopSpecified(ids,eventOperator);
00152   }
00153 
00154 
00155 
00158   /*  template<typename T>
00159     void recordEventForPlayback(EventPrincipal const& eventPrincipal,
00160                              std::vector<edm::EventID> &ids, T& eventOperator)
00161     {
00162       ids.push_back(eventPrincipal.id());
00163       eventOperator(eventPrincipal);
00164     }
00165   */
00166 }
00167 
00168 
00169 #endif