CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_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 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 
00013 #include "CLHEP/Random/RandPoissonQ.h"
00014 #include "CLHEP/Random/RandFlat.h"
00015 
00016 #include "TRandom.h"
00017 #include "TFile.h"
00018 #include "TH1F.h"
00019 
00020 class TFile;
00021 class TH1F;
00022 
00023 namespace CLHEP {
00024   class RandPoissonQ;
00025   class RandPoisson;
00026 }
00027 
00028 
00029 
00030 namespace edm {
00031   class PileUp {
00032   public:
00033     explicit PileUp(ParameterSet const& pset, double averageNumber, TH1F* const histo, const bool playback);
00034     ~PileUp();
00035 
00036     template<typename T>
00037       void readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int NumPU );
00038 
00039     template<typename T>
00040       void playPileUp(const std::vector<edm::EventID> &ids, T eventOperator);
00041 
00042     double averageNumber() const {return averageNumber_;}
00043     bool poisson() const {return poisson_;}
00044     bool doPileUp() {return none_ ? false :  averageNumber_>0.;}
00045     void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
00046       input_->dropUnwantedBranches(wantedBranches);
00047     }
00048     void endJob () {
00049       input_->doEndJob();
00050     }
00051 
00052     void reload(const edm::EventSetup & setup);
00053 
00054     void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions);
00055 
00056     //template<typename T>
00057     // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
00058         //                        std::vector<edm::EventID> &ids, T& eventOperator);
00059 
00060     const unsigned int & input()const{return inputType_;}
00061     void input(unsigned int s){inputType_=s;}
00062 
00063   private:
00064     unsigned int  inputType_;
00065     std::string type_;
00066     double averageNumber_;
00067     int const intAverage_;
00068     TH1F* histo_;
00069     bool histoDistribution_;
00070     bool probFunctionDistribution_;
00071     bool poisson_;
00072     bool fixed_;
00073     bool none_;
00074     bool manage_OOT_;
00075     bool poisson_OOT_;
00076     bool fixed_OOT_;
00077 
00078     bool PU_Study_;
00079     std::string Study_type_;
00080 
00081     int  intFixed_OOT_;
00082     int  intFixed_ITPU_;
00083 
00084     VectorInputSource * const input_;
00085     CLHEP::RandPoissonQ *poissonDistribution_;
00086     CLHEP::RandPoisson  *poissonDistr_OOT_;
00087 
00088 
00089     TH1F *h1f;
00090     TH1F *hprobFunction;
00091     TFile *probFileHisto;
00092     
00093     //playback info
00094     bool playback_;
00095 
00096     // sequential reading
00097     bool sequential_;
00098 
00099     // force reading pileup events from the same lumisection as the signal event
00100     bool samelumi_;
00101     
00102     // read the seed for the histo and probability function cases
00103     int seed_;
00104   };
00105 
00106 
00107 
00108   template<typename T>
00109   class RecordEventID
00110   {
00111   private:
00112     std::vector<edm::EventID>& ids_;
00113     T& eventOperator_;
00114     int eventCount ;
00115   public:
00116     RecordEventID(std::vector<edm::EventID>& ids, T& eventOperator)
00117       : ids_(ids), eventOperator_(eventOperator), eventCount( 0 ) {}
00118     void operator()(EventPrincipal const& eventPrincipal) {
00119       ids_.push_back(eventPrincipal.id());
00120       eventOperator_(eventPrincipal, ++eventCount);
00121     }
00122   };
00123 
00124 
00135   template<typename T>
00136   void
00137     PileUp::readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int pileEventCnt) {
00138 
00139     // One reason PileUp is responsible for recording event IDs is
00140     // that it is the one that knows how many events will be read.
00141     ids.reserve(pileEventCnt);
00142     RecordEventID<T> recorder(ids,eventOperator);
00143     int read;
00144     if (samelumi_) {
00145       const edm::LuminosityBlockID lumi(signal.run(), signal.luminosityBlock());
00146       if (sequential_)
00147         read = input_->loopSequentialWithID(lumi, pileEventCnt, recorder);
00148       else
00149         read = input_->loopRandomWithID(lumi, pileEventCnt, recorder);
00150     } else {
00151       if (sequential_) {
00152         // boost::bind creates a functor from recordEventForPlayback
00153         // so that recordEventForPlayback can insert itself before
00154         // the original eventOperator.
00155 
00156         read = input_->loopSequential(pileEventCnt, recorder);
00157         //boost::bind(&PileUp::recordEventForPlayback<T>,
00158         //                    boost::ref(*this), _1, boost::ref(ids),
00159         //                             boost::ref(eventOperator))
00160         //  );
00161           
00162       } else  {
00163         read = input_->loopRandom(pileEventCnt, recorder);
00164         //               boost::bind(&PileUp::recordEventForPlayback<T>,
00165         //                             boost::ref(*this), _1, boost::ref(ids),
00166         //                             boost::ref(eventOperator))
00167         //                 );
00168       }
00169     }
00170     if (read != pileEventCnt)
00171       edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
00172   }
00173 
00174 
00175 
00176   template<typename T>
00177   void
00178     PileUp::playPileUp(const std::vector<edm::EventID> &ids, T eventOperator) {
00179     //TrueNumInteractions.push_back( ids.size() ) ;
00180     input_->loopSpecified(ids,eventOperator);
00181   }
00182 
00183 
00184 
00187   /*  template<typename T>
00188     void recordEventForPlayback(EventPrincipal const& eventPrincipal,
00189                              std::vector<edm::EventID> &ids, T& eventOperator)
00190     {
00191       ids.push_back(eventPrincipal.id());
00192       eventOperator(eventPrincipal);
00193     }
00194   */
00195 }
00196 
00197 
00198 #endif