CMS 3D CMS Logo

Public Member Functions | Private Attributes

edm::PileUp Class Reference

#include <PileUp.h>

List of all members.

Public Member Functions

double averageNumber () const
void CalculatePileup (int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions)
bool doPileUp ()
void dropUnwantedBranches (std::vector< std::string > const &wantedBranches)
void endJob ()
void input (unsigned int s)
const unsigned int & input () const
 PileUp (ParameterSet const &pset, double averageNumber, TH1F *const histo, const bool playback)
template<typename T >
void playPileUp (const std::vector< edm::EventID > &ids, T eventOperator)
bool poisson () const
template<typename T >
void readPileUp (edm::EventID const &signal, std::vector< edm::EventID > &ids, T eventOperator, const int NumPU)
void reload (const edm::EventSetup &setup)
 ~PileUp ()

Private Attributes

double averageNumber_
std::unique_ptr< EventPrincipaleventPrincipal_
bool fixed_
bool fixed_OOT_
TH1F * h1f
TH1F * histo_
bool histoDistribution_
TH1F * hprobFunction
std::unique_ptr
< VectorInputSource > const 
input_
unsigned int inputType_
int const intAverage_
int intFixed_ITPU_
int intFixed_OOT_
bool manage_OOT_
bool none_
bool playback_
bool poisson_
bool poisson_OOT_
std::unique_ptr
< CLHEP::RandPoisson > 
poissonDistr_OOT_
std::unique_ptr
< CLHEP::RandPoissonQ > 
poissonDistribution_
TFile * probFileHisto
bool probFunctionDistribution_
std::unique_ptr
< ProcessConfiguration
processConfiguration_
std::unique_ptr< ProductRegistryproductRegistry_
bool PU_Study_
bool samelumi_
int seed_
bool sequential_
std::string Study_type_
std::string type_

Detailed Description

Definition at line 32 of file PileUp.h.


Constructor & Destructor Documentation

edm::PileUp::PileUp ( ParameterSet const &  pset,
double  averageNumber,
TH1F *const  histo,
const bool  playback 
) [explicit]

Definition at line 28 of file PileUp.cc.

References averageNumber_, eventPrincipal_, Exception, edm::ParameterSet::exists(), fixed_, fixed_OOT_, edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), histoDistribution_, edm::ParameterSet::id(), input_, intFixed_ITPU_, intFixed_OOT_, edm::Service< T >::isAvailable(), manage_OOT_, none_, poisson_, poisson_OOT_, poissonDistr_OOT_, poissonDistribution_, probFunctionDistribution_, processConfiguration_, PU_Study_, edm::ParameterSet::registerIt(), seed_, AlCaHLTBitMon_QueryRunRegistry::string, Study_type_, and type_.

                                                                                                        :
    type_(pset.getParameter<std::string>("type")),
    averageNumber_(averageNumber),
    intAverage_(static_cast<int>(averageNumber)),
    histo_(histo),
    histoDistribution_(type_ == "histo"),
    probFunctionDistribution_(type_ == "probFunction"),
    poisson_(type_ == "poisson"),
    fixed_(type_ == "fixed"),
    none_(type_ == "none"),
    productRegistry_(new SignallingProductRegistry),
    input_(VectorInputSourceFactory::get()->makeVectorInputSource(pset, InputSourceDescription(
                                                                   ModuleDescription(),
                                                                   *productRegistry_,
                                                                   boost::shared_ptr<BranchIDListHelper>(new BranchIDListHelper),
                                                                   boost::shared_ptr<ActivityRegistry>(new ActivityRegistry),
                                                                   -1, -1
                                                                   )).release()),
    processConfiguration_(new ProcessConfiguration(std::string("@MIXING"), getReleaseVersion(), getPassID())),
    eventPrincipal_(),
    poissonDistribution_(),
    poissonDistr_OOT_(),
    playback_(playback),
    sequential_(pset.getUntrackedParameter<bool>("sequential", false)),
    samelumi_(pset.getUntrackedParameter<bool>("sameLumiBlock", false)),
    seed_(0) {

    // Use the empty parameter set for the parameter set ID of our "@MIXING" process.
    ParameterSet emptyPSet;
    emptyPSet.registerIt();
    processConfiguration_->setParameterSetID(emptyPSet.id());

    input_->productRegistry()->setFrozen();

    // A modified HistoryAppender must be used for unscheduled processing.
    eventPrincipal_.reset(new EventPrincipal(input_->productRegistry(),
                                       input_->branchIDListHelper(),
                                       *processConfiguration_,
                                       nullptr));

    if (pset.exists("nbPileupEvents")) {
       seed_=pset.getParameter<edm::ParameterSet>("nbPileupEvents").getUntrackedParameter<int>("seed",0);
    }

    bool DB=type_=="readDB";

    edm::Service<edm::RandomNumberGenerator> rng;
    if (!rng.isAvailable()) {
      throw cms::Exception("Configuration")
        << "PileUp requires the RandomNumberGeneratorService\n"
        "which is not present in the configuration file.  You must add the service\n"
        "in the configuration file or remove the modules that require it.";
    }
    
    CLHEP::HepRandomEngine& engine = rng->getEngine();
    poissonDistribution_.reset(new CLHEP::RandPoissonQ(engine, averageNumber_));
    
    // Get seed for the case when using user histogram or probability function
    if (histoDistribution_ || probFunctionDistribution_ || DB){ 
      if(seed_ !=0) {
        gRandom->SetSeed(seed_);
        LogInfo("MixingModule") << " Change seed for " << type_ << " mode. The seed is set to " << seed_;
      }
      else {
        gRandom->SetSeed(engine.getSeed());
      }
    } 
     
        
    if (!(histoDistribution_ || probFunctionDistribution_ || poisson_ || fixed_ || none_) && !DB) {
      throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
        << "'type' parameter (a string) has a value of '" << type_ << "'.\n"
        << "Legal values are 'poisson', 'fixed', or 'none'\n";
    }

    if (!DB){
    manage_OOT_ = pset.getUntrackedParameter<bool>("manage_OOT", false);

    // Check for string describing special processing.  Add these here for individual cases

    PU_Study_ = false;
    Study_type_ = "";

    Study_type_ = pset.getUntrackedParameter<std::string>("Special_Pileup_Studies", "");

    if(Study_type_ == "Fixed_ITPU_Vary_OOTPU") {

      PU_Study_ = true;
      intFixed_ITPU_ = pset.getUntrackedParameter<int>("intFixed_ITPU", 0);

    }

    if(manage_OOT_) { // figure out what the parameters are

      //      if (playback_) throw cms::Exception("Illegal parameter clash","PileUp::PileUp(ParameterSet const& pset)")
      // << " manage_OOT option not allowed with playback ";

      std::string OOT_type = pset.getUntrackedParameter<std::string>("OOT_type");

      if(OOT_type == "Poisson" || OOT_type == "poisson") {
        poisson_OOT_ = true;
        poissonDistr_OOT_.reset(new CLHEP::RandPoisson(engine));
      }
      else if(OOT_type == "Fixed" || OOT_type == "fixed") {
        fixed_OOT_ = true;
        // read back the fixed number requested out-of-time
        intFixed_OOT_ = pset.getUntrackedParameter<int>("intFixed_OOT", -1);
        if(intFixed_OOT_ < 0) {
          throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)") 
            << " Fixed out-of-time pileup requested, but no fixed value given ";
        }
      }
      else {
        throw cms::Exception("Illegal parameter value","PileUp::PileUp(ParameterSet const& pset)")
          << "'OOT_type' parameter (a string) has a value of '" << OOT_type << "'.\n"
          << "Legal values are 'poisson' or 'fixed'\n";
      }
      edm::LogInfo("MixingModule") <<" Out-of-time pileup will be generated with a " << OOT_type << " distribution. " ;
    }
    }
    
  }
edm::PileUp::~PileUp ( )

Definition at line 251 of file PileUp.cc.

                  {
  }

Member Function Documentation

double edm::PileUp::averageNumber ( ) const [inline]

Definition at line 43 of file PileUp.h.

References averageNumber_.

Referenced by reload().

{return averageNumber_;}
void edm::PileUp::CalculatePileup ( int  MinBunch,
int  MaxBunch,
std::vector< int > &  PileupSelection,
std::vector< float > &  TrueNumInteractions 
)

Definition at line 254 of file PileUp.cc.

References averageNumber_, fixed_, histo_, histoDistribution_, intAverage_, intFixed_ITPU_, intFixed_OOT_, manage_OOT_, none_, poisson_, poisson_OOT_, poissonDistr_OOT_, poissonDistribution_, probFunctionDistribution_, PU_Study_, and Study_type_.

                                                                                                                                 {

    // if we are managing the distribution of out-of-time pileup separately, select the distribution for bunch
    // crossing zero first, save it for later.

    int nzero_crossing = -1;
    double Fnzero_crossing = -1;

    if(manage_OOT_) {
      if (none_){
        nzero_crossing = 0;
      }else if (poisson_){
        nzero_crossing =  poissonDistribution_->fire() ;
      }else if (fixed_){
        nzero_crossing =  intAverage_ ;
      }else if (histoDistribution_ || probFunctionDistribution_){
        double d = histo_->GetRandom();
        //n = (int) floor(d + 0.5);  // incorrect for bins with integer edges
        Fnzero_crossing =  d;
        nzero_crossing = int(d);
      }

    }

    for(int bx = MinBunch; bx < MaxBunch+1; ++bx) {

      if(manage_OOT_) {
        if(bx==0 && !poisson_OOT_) { 
          PileupSelection.push_back(nzero_crossing) ;
          TrueNumInteractions.push_back( nzero_crossing );
        }
        else{
          if(poisson_OOT_) {
            if(PU_Study_ && (Study_type_ == "Fixed_ITPU_Vary_OOTPU" ) && bx==0 ) {
              PileupSelection.push_back(intFixed_ITPU_) ;
            }
            else{
              PileupSelection.push_back(poissonDistr_OOT_->fire(Fnzero_crossing)) ;
            }
            TrueNumInteractions.push_back( Fnzero_crossing );
          }
          else {
            PileupSelection.push_back(intFixed_OOT_) ;
            TrueNumInteractions.push_back( intFixed_OOT_ );
          }  
        }
      }
      else {
        if (none_){
          PileupSelection.push_back(0);
          TrueNumInteractions.push_back( 0. );
        }else if (poisson_){
          PileupSelection.push_back(poissonDistribution_->fire());
          TrueNumInteractions.push_back( averageNumber_ );
        }else if (fixed_){
          PileupSelection.push_back(intAverage_);
          TrueNumInteractions.push_back( intAverage_ );
        }else if (histoDistribution_ || probFunctionDistribution_){
          double d = histo_->GetRandom();
          PileupSelection.push_back(int(d));
          TrueNumInteractions.push_back( d );
        }
      }
    
    }
  }
bool edm::PileUp::doPileUp ( ) [inline]

Definition at line 45 of file PileUp.h.

References averageNumber_, and none_.

{return none_ ? false :  averageNumber_>0.;}
void edm::PileUp::dropUnwantedBranches ( std::vector< std::string > const &  wantedBranches) [inline]

Definition at line 46 of file PileUp.h.

References input_.

                                                                          {
      input_->dropUnwantedBranches(wantedBranches);
    }
void edm::PileUp::endJob ( void  ) [inline]

Definition at line 49 of file PileUp.h.

References input_.

                   {
      input_->doEndJob();
    }
void edm::PileUp::input ( unsigned int  s) [inline]

Definition at line 62 of file PileUp.h.

References inputType_, and alignCSCRings::s.

const unsigned int& edm::PileUp::input ( ) const [inline]

Definition at line 61 of file PileUp.h.

References inputType_.

{return inputType_;}
template<typename T >
void edm::PileUp::playPileUp ( const std::vector< edm::EventID > &  ids,
T  eventOperator 
)

Definition at line 182 of file PileUp.h.

References eventPrincipal_, and input_.

                                                                        {
    //TrueNumInteractions.push_back( ids.size() ) ;
    input_->loopSpecified(*eventPrincipal_,ids,eventOperator);
  }
bool edm::PileUp::poisson ( ) const [inline]

Definition at line 44 of file PileUp.h.

References poisson_.

{return poisson_;}
template<typename T >
void edm::PileUp::readPileUp ( edm::EventID const &  signal,
std::vector< edm::EventID > &  ids,
T  eventOperator,
const int  pileEventCnt 
)

Generates events from a VectorInputSource. This function decides which method of VectorInputSource to call: sequential, random, or pre-specified. The ids are either ids to read or ids to store while reading. eventOperator has a type that matches the eventOperator in VectorInputSource::loopRandom.

The "signal" event is optionally used to restrict the secondary events used for pileup and mixing.

Definition at line 141 of file PileUp.h.

References eventPrincipal_, input_, fjr2json::lumi, edm::EventID::luminosityBlock(), SiPixelLorentzAngle_cfi::read, edm::EventID::run(), samelumi_, and sequential_.

                                                                                                                     {

    // One reason PileUp is responsible for recording event IDs is
    // that it is the one that knows how many events will be read.
    ids.reserve(pileEventCnt);
    RecordEventID<T> recorder(ids,eventOperator);
    int read;
    if (samelumi_) {
      const edm::LuminosityBlockID lumi(signal.run(), signal.luminosityBlock());
      if (sequential_)
        read = input_->loopSequentialWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
      else
        read = input_->loopRandomWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
    } else {
      if (sequential_) {
        // boost::bind creates a functor from recordEventForPlayback
        // so that recordEventForPlayback can insert itself before
        // the original eventOperator.

        read = input_->loopSequential(*eventPrincipal_, pileEventCnt, recorder);
        //boost::bind(&PileUp::recordEventForPlayback<T>,
        //                    boost::ref(*this), _1, boost::ref(ids),
        //                             boost::ref(eventOperator))
        //  );
          
      } else  {
        read = input_->loopRandom(*eventPrincipal_, pileEventCnt, recorder);
        //               boost::bind(&PileUp::recordEventForPlayback<T>,
        //                             boost::ref(*this), _1, boost::ref(ids),
        //                             boost::ref(eventOperator))
        //                 );
      }
    }
    if (read != pileEventCnt)
      edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
  }
void edm::PileUp::reload ( const edm::EventSetup setup)

Definition at line 151 of file PileUp.cc.

References MixingInputConfig::averageNumber(), averageNumber(), averageNumber_, HDQMDatabaseProducer::config, alignCSCRings::e, Exception, fixed_, fixed_OOT_, MixingInputConfig::fixedOutOfTime(), edm::EventSetup::get(), histo_, histoDistribution_, i, inputType_, intFixed_OOT_, j, LogDebug, manage_OOT_, none_, MixingInputConfig::outOfTime(), poisson_, poisson_OOT_, poissonDistr_OOT_, poissonDistribution_, probFunctionDistribution_, MixingInputConfig::probFunctionVariable(), MixingInputConfig::probValue(), MixingInputConfig::type(), type_, SiStripMonitorClusterAlca_cfi::xmax, and SiStripMonitorClusterAlca_cfi::xmin.

                                                {
    //get the required parameters from DB.
    edm::ESHandle<MixingModuleConfig> configM;
    setup.get<MixingRcd>().get(configM);

    const MixingInputConfig & config=configM->config(inputType_);

    //get the type
    type_=config.type();
    //set booleans
    histoDistribution_=type_ == "histo";
    probFunctionDistribution_=type_ == "probFunction";
    poisson_=type_ == "poisson";
    fixed_=type_ == "fixed";
    none_=type_ == "none";
    
    if (histoDistribution_) edm::LogError("MisConfiguration")<<"type histo cannot be reloaded from DB, yet";
    
    if (fixed_){
      averageNumber_=averageNumber();
    }
    else if (poisson_)
      {
        averageNumber_=config.averageNumber();
        edm::Service<edm::RandomNumberGenerator> rng; 
        CLHEP::HepRandomEngine& engine = rng->getEngine();            
        poissonDistribution_.reset(new CLHEP::RandPoissonQ(engine, averageNumber_));
      }
    else if (probFunctionDistribution_)
      {
        //need to reload the histogram from DB
        const std::vector<int> & dataProbFunctionVar = config.probFunctionVariable();
        std::vector<double> dataProb = config.probValue();
        
        int varSize = (int) dataProbFunctionVar.size();
        int probSize = (int) dataProb.size();
                
        if ((dataProbFunctionVar[0] != 0) || (dataProbFunctionVar[varSize - 1] != (varSize - 1))) 
          throw cms::Exception("BadProbFunction") << "Please, check the variables of the probability function! The first variable should be 0 and the difference between two variables should be 1." << std::endl;
                
        // Complete the vector containing the probability  function data
        // with the values "0"
        if (probSize < varSize){
          edm::LogWarning("MixingModule") << " The probability function data will be completed with " <<(varSize - probSize)  <<" values 0.";
          
          for (int i=0; i<(varSize - probSize); i++) dataProb.push_back(0);
          
          probSize = dataProb.size();
          edm::LogInfo("MixingModule") << " The number of the P(x) data set after adding the values 0 is " << probSize;
        }
        
        // Create an histogram with the data from the probability function provided by the user           
        int xmin = (int) dataProbFunctionVar[0];
        int xmax = (int) dataProbFunctionVar[varSize-1]+1;  // need upper edge to be one beyond last value
        int numBins = varSize;
        
        edm::LogInfo("MixingModule") << "An histogram will be created with " << numBins << " bins in the range ("<< xmin << "," << xmax << ")." << std::endl;

        if (histo_) delete histo_;
        histo_ = new TH1F("h","Histo from the user's probability function",numBins,xmin,xmax); 
        
        LogDebug("MixingModule") << "Filling histogram with the following data:" << std::endl;
        
        for (int j=0; j < numBins ; j++){
          LogDebug("MixingModule") << " x = " << dataProbFunctionVar[j ]<< " P(x) = " << dataProb[j];
          histo_->Fill(dataProbFunctionVar[j]+0.5,dataProb[j]); // assuming integer values for the bins, fill bin centers, not edges 
        }
        
        // Check if the histogram is normalized
        if ( ((histo_->Integral() - 1) > 1.0e-02) && ((histo_->Integral() - 1) < -1.0e-02)){ 
          throw cms::Exception("BadProbFunction") << "The probability function should be normalized!!! " << std::endl;
        }
        averageNumber_=histo_->GetMean();
      }

    int oot=config.outOfTime();
    manage_OOT_=false;
    if (oot==1)
      {
        manage_OOT_=true;
        poisson_OOT_ = false;
        if (poissonDistr_OOT_){poissonDistr_OOT_.reset(); }
        fixed_OOT_ = true;
        intFixed_OOT_=config.fixedOutOfTime();
      }
    else if (oot==2)
      {
        manage_OOT_=true;
        poisson_OOT_ = true;
        fixed_OOT_ = false;
        if (!poissonDistr_OOT_) {
          //no need to trash the previous one if already there
          edm::Service<edm::RandomNumberGenerator> rng; 
          CLHEP::HepRandomEngine& engine = rng->getEngine();                      
          poissonDistr_OOT_.reset(new CLHEP::RandPoisson(engine));
        }
      }

    
  }

Member Data Documentation

double edm::PileUp::averageNumber_ [private]

Definition at line 67 of file PileUp.h.

Referenced by averageNumber(), CalculatePileup(), doPileUp(), PileUp(), and reload().

std::unique_ptr<EventPrincipal> edm::PileUp::eventPrincipal_ [private]

Definition at line 88 of file PileUp.h.

Referenced by PileUp(), playPileUp(), and readPileUp().

bool edm::PileUp::fixed_ [private]

Definition at line 73 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

bool edm::PileUp::fixed_OOT_ [private]

Definition at line 77 of file PileUp.h.

Referenced by PileUp(), and reload().

TH1F* edm::PileUp::h1f [private]

Definition at line 93 of file PileUp.h.

TH1F* edm::PileUp::histo_ [private]

Definition at line 69 of file PileUp.h.

Referenced by CalculatePileup(), and reload().

Definition at line 70 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

TH1F* edm::PileUp::hprobFunction [private]

Definition at line 94 of file PileUp.h.

std::unique_ptr<VectorInputSource> const edm::PileUp::input_ [private]

Definition at line 86 of file PileUp.h.

Referenced by dropUnwantedBranches(), endJob(), PileUp(), playPileUp(), and readPileUp().

unsigned int edm::PileUp::inputType_ [private]

Definition at line 65 of file PileUp.h.

Referenced by input(), and reload().

int const edm::PileUp::intAverage_ [private]

Definition at line 68 of file PileUp.h.

Referenced by CalculatePileup().

Definition at line 83 of file PileUp.h.

Referenced by CalculatePileup(), and PileUp().

Definition at line 82 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

bool edm::PileUp::manage_OOT_ [private]

Definition at line 75 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

bool edm::PileUp::none_ [private]

Definition at line 74 of file PileUp.h.

Referenced by CalculatePileup(), doPileUp(), PileUp(), and reload().

bool edm::PileUp::playback_ [private]

Definition at line 98 of file PileUp.h.

bool edm::PileUp::poisson_ [private]

Definition at line 72 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), poisson(), and reload().

bool edm::PileUp::poisson_OOT_ [private]

Definition at line 76 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

std::unique_ptr<CLHEP::RandPoisson> edm::PileUp::poissonDistr_OOT_ [private]

Definition at line 90 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

std::unique_ptr<CLHEP::RandPoissonQ> edm::PileUp::poissonDistribution_ [private]

Definition at line 89 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

TFile* edm::PileUp::probFileHisto [private]

Definition at line 95 of file PileUp.h.

Definition at line 71 of file PileUp.h.

Referenced by CalculatePileup(), PileUp(), and reload().

Definition at line 87 of file PileUp.h.

Referenced by PileUp().

std::unique_ptr<ProductRegistry> edm::PileUp::productRegistry_ [private]

Definition at line 85 of file PileUp.h.

bool edm::PileUp::PU_Study_ [private]

Definition at line 79 of file PileUp.h.

Referenced by CalculatePileup(), and PileUp().

bool edm::PileUp::samelumi_ [private]

Definition at line 104 of file PileUp.h.

Referenced by readPileUp().

int edm::PileUp::seed_ [private]

Definition at line 107 of file PileUp.h.

Referenced by PileUp().

bool edm::PileUp::sequential_ [private]

Definition at line 101 of file PileUp.h.

Referenced by readPileUp().

std::string edm::PileUp::Study_type_ [private]

Definition at line 80 of file PileUp.h.

Referenced by CalculatePileup(), and PileUp().

std::string edm::PileUp::type_ [private]

Definition at line 66 of file PileUp.h.

Referenced by PileUp(), and reload().