CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Mixing/Base/src/PileUp.cc

Go to the documentation of this file.
00001 #include "Mixing/Base/interface/PileUp.h"
00002 #include "FWCore/Framework/interface/EventPrincipal.h"
00003 #include "FWCore/Framework/interface/InputSourceDescription.h"
00004 #include "FWCore/Sources/interface/VectorInputSourceFactory.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "FWCore/Utilities/interface/Exception.h"
00008 
00009 #include "FWCore/ServiceRegistry/interface/Service.h"
00010 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00011 
00012 #include <algorithm>
00013 
00014 namespace edm {
00015   PileUp::PileUp(ParameterSet const& pset, double averageNumber, TH1F * const histo, const bool playback) :
00016     type_(pset.getParameter<std::string>("type")),
00017     averageNumber_(averageNumber),
00018     intAverage_(static_cast<int>(averageNumber)),
00019     histo_(histo),
00020     histoDistribution_(type_ == "histo"),
00021     probFunctionDistribution_(type_ == "probFunction"),
00022     poisson_(type_ == "poisson"),
00023     fixed_(type_ == "fixed"),
00024     none_(type_ == "none"),
00025     input_(VectorInputSourceFactory::get()->makeVectorInputSource(pset, InputSourceDescription()).release()),
00026     poissonDistribution_(0),
00027     poissonDistr_OOT_(0),
00028     playback_(playback),
00029     sequential_(pset.getUntrackedParameter<bool>("sequential", false)),
00030     seed_(pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed",0))
00031    {
00032 
00033     
00034     edm::Service<edm::RandomNumberGenerator> rng;
00035     if (!rng.isAvailable()) {
00036       throw cms::Exception("Configuration")
00037         << "PileUp requires the RandomNumberGeneratorService\n"
00038         "which is not present in the configuration file.  You must add the service\n"
00039         "in the configuration file or remove the modules that require it.";
00040     }
00041     
00042     CLHEP::HepRandomEngine& engine = rng->getEngine();
00043     poissonDistribution_ = new CLHEP::RandPoissonQ(engine, averageNumber_);
00044     
00045     // Get seed for the case when using user histogram or probability function
00046     if (histoDistribution_ || probFunctionDistribution_){ 
00047       if(seed_ !=0) {
00048         gRandom->SetSeed(seed_);
00049         LogInfo("MixingModule") << " Change seed for " << type_ << " mode. The seed is set to " << seed_;
00050       }
00051       else {
00052         gRandom->SetSeed(engine.getSeed());
00053       }
00054     } 
00055      
00056         
00057     if (!(histoDistribution_ || probFunctionDistribution_ || poisson_ || fixed_ || none_)) {
00058       throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
00059         << "'type' parameter (a string) has a value of '" << type_ << "'.\n"
00060         << "Legal values are 'poisson', 'fixed', or 'none'\n";
00061     }
00062 
00063     manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);
00064 
00065     if(manage_OOT_) { // figure out what the parameters are
00066 
00067       if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
00068         << " manage_OOT option not allowed with playback ";
00069 
00070       std::string OOT_type = pset.getUntrackedParameter<std::string>("OOT_type");
00071 
00072       if(OOT_type == "Poisson" || OOT_type == "poisson") {
00073         poisson_OOT_ = true;
00074         poissonDistr_OOT_ = new CLHEP::RandPoisson(engine);
00075       }
00076       else if(OOT_type == "Fixed" || OOT_type == "fixed") {
00077         fixed_OOT_ = true;
00078         // read back the fixed number requested out-of-time
00079         intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
00080         if(intFixed_OOT_ < 0) {
00081           throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)") 
00082             << " Fixed out-of-time pileup requested, but no fixed value given ";
00083         }
00084       }
00085       else {
00086         throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
00087           << "'OOT_type' parameter (a string) has a value of '" << OOT_type << "'.\n"
00088           << "Legal values are 'poisson' or 'fixed'\n";
00089       }
00090       edm::LogInfo("MixingModule") <<" Out-of-time pileup will be generated with a " << OOT_type << " distribution. " ;
00091     }
00092     
00093   }
00094 
00095   PileUp::~PileUp() {
00096     delete poissonDistribution_;
00097     delete poissonDistr_OOT_ ;
00098   }
00099 
00100   void PileUp::CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions) {
00101 
00102     // if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
00103     // crossing zero first, save it for later.
00104 
00105     int nzero_crossing = -1;
00106     double Fnzero_crossing = -1;
00107 
00108     if(manage_OOT_) {
00109       if (none_){
00110         nzero_crossing = 0;
00111       }else if (poisson_){
00112         nzero_crossing =  poissonDistribution_->fire() ;
00113       }else if (fixed_){
00114         nzero_crossing =  intAverage_ ;
00115       }else if (histoDistribution_ || probFunctionDistribution_){
00116         double d = histo_->GetRandom();
00117         //n = (int) floor(d + 0.5);  // incorrect for bins with integer edges
00118         Fnzero_crossing =  d;
00119       }
00120 
00121     }
00122 
00123     for(int bx = MinBunch; bx < MaxBunch+1; ++bx) {
00124 
00125       if(manage_OOT_) {
00126         if(bx==0 && !poisson_OOT_) { 
00127           PileupSelection.push_back(nzero_crossing) ;
00128           TrueNumInteractions.push_back( nzero_crossing );
00129         }
00130         else{
00131           if(poisson_OOT_) {
00132             PileupSelection.push_back(poissonDistr_OOT_->fire(Fnzero_crossing)) ;
00133             TrueNumInteractions.push_back( Fnzero_crossing );
00134           }
00135           else {
00136             PileupSelection.push_back(intFixed_OOT_) ;
00137             TrueNumInteractions.push_back( intFixed_OOT_ );
00138           }  
00139         }
00140       }
00141       else {
00142         if (none_){
00143           PileupSelection.push_back(0);
00144           TrueNumInteractions.push_back( 0. );
00145         }else if (poisson_){
00146           PileupSelection.push_back(poissonDistribution_->fire());
00147           TrueNumInteractions.push_back( averageNumber_ );
00148         }else if (fixed_){
00149           PileupSelection.push_back(intAverage_);
00150           TrueNumInteractions.push_back( intAverage_ );
00151         }else if (histoDistribution_ || probFunctionDistribution_){
00152           double d = histo_->GetRandom();
00153           PileupSelection.push_back(int(d));
00154           TrueNumInteractions.push_back( d );
00155         }
00156       }
00157     
00158     }
00159   }
00160 
00161 
00162 } //namespace edm