CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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     int  intFixed_OOT_;
00078 
00079     VectorInputSource * const input_;
00080     CLHEP::RandPoissonQ *poissonDistribution_;
00081     CLHEP::RandPoisson  *poissonDistr_OOT_;
00082 
00083 
00084     TH1F *h1f;
00085     TH1F *hprobFunction;
00086     TFile *probFileHisto;
00087     
00088     //playback info
00089     bool playback_;
00090 
00091     // sequential reading
00092     bool sequential_;
00093 
00094     // force reading pileup events from the same lumisection as the signal event
00095     bool samelumi_;
00096     
00097     // read the seed for the histo and probability function cases
00098     int seed_;
00099   };
00100 
00101 
00102 
00103   template<typename T>
00104   class RecordEventID
00105   {
00106   private:
00107     std::vector<edm::EventID>& ids_;
00108     T& eventOperator_;
00109     int eventCount ;
00110   public:
00111     RecordEventID(std::vector<edm::EventID>& ids, T& eventOperator)
00112       : ids_(ids), eventOperator_(eventOperator), eventCount( 0 ) {}
00113     void operator()(EventPrincipal const& eventPrincipal) {
00114       ids_.push_back(eventPrincipal.id());
00115       eventOperator_(eventPrincipal, ++eventCount);
00116     }
00117   };
00118 
00119 
00130   template<typename T>
00131   void
00132     PileUp::readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int pileEventCnt) {
00133 
00134     // One reason PileUp is responsible for recording event IDs is
00135     // that it is the one that knows how many events will be read.
00136     ids.reserve(pileEventCnt);
00137     RecordEventID<T> recorder(ids,eventOperator);
00138     int read;
00139     if (samelumi_) {
00140       const edm::LuminosityBlockID lumi(signal.run(), signal.luminosityBlock());
00141       if (sequential_)
00142         read = input_->loopSequentialWithID(lumi, pileEventCnt, recorder);
00143       else
00144         read = input_->loopRandomWithID(lumi, pileEventCnt, recorder);
00145     } else {
00146       if (sequential_) {
00147         // boost::bind creates a functor from recordEventForPlayback
00148         // so that recordEventForPlayback can insert itself before
00149         // the original eventOperator.
00150 
00151         read = input_->loopSequential(pileEventCnt, recorder);
00152         //boost::bind(&PileUp::recordEventForPlayback<T>,
00153         //                    boost::ref(*this), _1, boost::ref(ids),
00154         //                             boost::ref(eventOperator))
00155         //  );
00156           
00157       } else  {
00158         read = input_->loopRandom(pileEventCnt, recorder);
00159         //               boost::bind(&PileUp::recordEventForPlayback<T>,
00160         //                             boost::ref(*this), _1, boost::ref(ids),
00161         //                             boost::ref(eventOperator))
00162         //                 );
00163       }
00164     }
00165     if (read != pileEventCnt)
00166       edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
00167   }
00168 
00169 
00170 
00171   template<typename T>
00172   void
00173     PileUp::playPileUp(const std::vector<edm::EventID> &ids, T eventOperator) {
00174     //TrueNumInteractions.push_back( ids.size() ) ;
00175     input_->loopSpecified(ids,eventOperator);
00176   }
00177 
00178 
00179 
00182   /*  template<typename T>
00183     void recordEventForPlayback(EventPrincipal const& eventPrincipal,
00184                              std::vector<edm::EventID> &ids, T& eventOperator)
00185     {
00186       ids.push_back(eventPrincipal.id());
00187       eventOperator(eventPrincipal);
00188     }
00189   */
00190 }
00191 
00192 
00193 #endif