From 96d00fa190f7ff238c80d263c75280a612e13c49 Mon Sep 17 00:00:00 2001 From: perrozzi Date: Tue, 8 Sep 2015 20:34:30 +0200 Subject: [PATCH 1/3] port PR 11149 second attempt --- GeneratorInterface/Core/BuildFile.xml | 1 + .../interface/PartonShowerBsHepMCFilter.h | 5 +-- .../Core/src/HepMCFilterDriver.cc | 2 +- .../Core/src/PartonShowerBsHepMCFilter.cc | 38 +++++++------------ .../GenFilters/interface/LHEGenericFilter.h | 6 ++- .../GenFilters/python/LHEGenericFilter_cfi.py | 8 ++++ .../GenFilters/src/LHEGenericFilter.cc | 37 ++++++++++-------- 7 files changed, 50 insertions(+), 47 deletions(-) create mode 100644 GeneratorInterface/GenFilters/python/LHEGenericFilter_cfi.py diff --git a/GeneratorInterface/Core/BuildFile.xml b/GeneratorInterface/Core/BuildFile.xml index a03408acc000e..9300e65937915 100644 --- a/GeneratorInterface/Core/BuildFile.xml +++ b/GeneratorInterface/Core/BuildFile.xml @@ -3,6 +3,7 @@ + diff --git a/GeneratorInterface/Core/interface/PartonShowerBsHepMCFilter.h b/GeneratorInterface/Core/interface/PartonShowerBsHepMCFilter.h index 1f21d90f8c7e2..8f1e3da786ae8 100644 --- a/GeneratorInterface/Core/interface/PartonShowerBsHepMCFilter.h +++ b/GeneratorInterface/Core/interface/PartonShowerBsHepMCFilter.h @@ -24,10 +24,7 @@ class PartonShowerBsHepMCFilter : public BaseHepMCFilter{ private: - int particle_id; - int exclude_status_id; - int status_id; }; -#endif +#endif \ No newline at end of file diff --git a/GeneratorInterface/Core/src/HepMCFilterDriver.cc b/GeneratorInterface/Core/src/HepMCFilterDriver.cc index caef408b0ea67..1cf49279bce87 100644 --- a/GeneratorInterface/Core/src/HepMCFilterDriver.cc +++ b/GeneratorInterface/Core/src/HepMCFilterDriver.cc @@ -87,4 +87,4 @@ void HepMCFilterDriver::resetStatistics() { sumtotal_w_ = 0; sumtotal_w2_ = 0; -} +} \ No newline at end of file diff --git a/GeneratorInterface/Core/src/PartonShowerBsHepMCFilter.cc b/GeneratorInterface/Core/src/PartonShowerBsHepMCFilter.cc index d1a3b53fbee40..2717b6dcef469 100644 --- a/GeneratorInterface/Core/src/PartonShowerBsHepMCFilter.cc +++ b/GeneratorInterface/Core/src/PartonShowerBsHepMCFilter.cc @@ -1,6 +1,7 @@ #include "GeneratorInterface/Core/interface/PartonShowerBsHepMCFilter.h" #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h" #include +#include "HepPDT/ParticleID.hh" using namespace edm; @@ -8,15 +9,7 @@ using namespace std; //constructor -PartonShowerBsHepMCFilter::PartonShowerBsHepMCFilter(const edm::ParameterSet& iConfig) : - - // particle id of the gen particles that you want to filter - particle_id(iConfig.getParameter("Particle_id")), - // status id of the particles that you want to exclude from the filter - exclude_status_id(iConfig.getUntrackedParameter("Exclude_status_id",-1)), - // status id of the particles that you want to filetr on - status_id(iConfig.getUntrackedParameter("Status_id",-1)) - +PartonShowerBsHepMCFilter::PartonShowerBsHepMCFilter(const edm::ParameterSet& iConfig) { } @@ -35,26 +28,21 @@ PartonShowerBsHepMCFilter::~PartonShowerBsHepMCFilter() // ------------ method called to produce the data ------------ bool PartonShowerBsHepMCFilter::filter(const HepMC::GenEvent* evt) { - - if( exclude_status_id > 0. && status_id > 0.){ - std::cout << "ERROR: Skipping event: Configuration has both exclude and status id set to a value > 0. They can not be used simultaneously." << std::endl; - return false; // skip event - } - for ( HepMC::GenEvent::particle_const_iterator p = evt->particles_begin(); - p != evt->particles_end(); ++p ) { + // loop over gen particles + for ( HepMC::GenEvent::particle_const_iterator p = evt->particles_begin(); p != evt->particles_end(); ++p ){ - if( abs((*p)->pdg_id()) == particle_id ){ - if( exclude_status_id > 0. && (*p)->status() != exclude_status_id ) - return true; // keep event - else if( status_id > 0. && (*p)->status() == status_id ) - return true; // keep event - else - return true; // keep event + // check only status 2 particles + if( (*p)->status()==2 ){ + // if one of the status 2 particles is a B-hadron, accept the event + HepPDT::ParticleID pid((*p)->pdg_id()); + if( pid.hasBottom() ){ + return true; // accept event + } } - + } return false; // skip event -} +} \ No newline at end of file diff --git a/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h b/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h index 5218a1c0fceb6..81bc8a36189c4 100644 --- a/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h +++ b/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h @@ -49,8 +49,10 @@ class LHEGenericFilter : public edm::EDFilter { edm::EDGetTokenT src_; int numRequired_; // number of particles required to pass filter - bool acceptMore_; // if true (default), accept numRequired or more. - // if false, accept events with exactly equal to numRequired. + std::string acceptLogic_; // LT meaning < + // GT > + // EQ = + // NE != std::vector particleID_; // vector of particle IDs to look for int totalEvents_; // counters int passedEvents_; diff --git a/GeneratorInterface/GenFilters/python/LHEGenericFilter_cfi.py b/GeneratorInterface/GenFilters/python/LHEGenericFilter_cfi.py new file mode 100644 index 0000000000000..e17c987c05932 --- /dev/null +++ b/GeneratorInterface/GenFilters/python/LHEGenericFilter_cfi.py @@ -0,0 +1,8 @@ +import FWCore.ParameterSet.Config as cms + +lheGenericFilter = cms.EDFilter("LHEGenericFilter", + src = cms.InputTag("source"), + NumRequired = cms.int32(2), + ParticleID = cms.vint32(5), + AcceptLogic = cms.string("LT") # LT meaning < NumRequired, GT >, EQ =, NE != +) diff --git a/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc b/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc index 8e027b7989fd2..be7586c11f1fe 100644 --- a/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc +++ b/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc @@ -1,10 +1,13 @@ #include "GeneratorInterface/GenFilters/interface/LHEGenericFilter.h" +using namespace edm; +using namespace std; + LHEGenericFilter::LHEGenericFilter(const edm::ParameterSet& iConfig) : - numRequired_(iConfig.getParameter("NumRequired")), - acceptMore_(iConfig.getParameter("AcceptMore")), - particleID_(iConfig.getParameter< std::vector >("ParticleID")), - totalEvents_(0), passedEvents_(0) +numRequired_(iConfig.getParameter("NumRequired")), +acceptLogic_(iConfig.getParameter("AcceptLogic")), +particleID_(iConfig.getParameter< std::vector >("ParticleID")), +totalEvents_(0), passedEvents_(0) { //here do whatever other initialization is needed src_ = consumes(iConfig.getParameter("src")); @@ -12,9 +15,8 @@ LHEGenericFilter::LHEGenericFilter(const edm::ParameterSet& iConfig) : LHEGenericFilter::~LHEGenericFilter() { - - // do anything here that needs to be done at destruction time - // (e.g. close files, deallocate resources etc.) + // do anything here that needs to be done at destruction time + // (e.g. close files, deallocate resources etc.) } @@ -29,20 +31,25 @@ bool LHEGenericFilter::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) int nFound = 0; for (int i = 0; i < EvtHandle->hepeup().NUP; ++i) { - if (EvtHandle->hepeup().ISTUP[i] != 1) { - continue; - } + if (EvtHandle->hepeup().ISTUP[i] != 1) { // keep only outgoing particles + continue; + } for (unsigned int j = 0; j < particleID_.size(); ++j) { if (particleID_[j] == 0 || abs(particleID_[j]) == abs(EvtHandle->hepeup().IDUP[i]) ) { - nFound++; - break; // only match a given particle once! + nFound++; + break; // only match a given particle once! } } // loop over targets - if (acceptMore_ && nFound == numRequired_) break; // stop looking if we don't mind having more } // loop over particles - if (nFound == numRequired_) { + // event accept/reject logic + if ( + (acceptLogic_.compare("LT")==0 && nFound < numRequired_) + || (acceptLogic_.compare("GT")==0 && nFound > numRequired_) + || (acceptLogic_.compare("EQ")==0 && nFound == numRequired_) + || (acceptLogic_.compare("NE")==0 && nFound != numRequired_) + ) { passedEvents_++; return true; } else { @@ -54,7 +61,7 @@ bool LHEGenericFilter::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) // ------------ method called once each job just after ending the event loop ------------ void LHEGenericFilter::endJob() { edm::LogInfo("LHEGenericFilter") << "=== Results of LHEGenericFilter: passed " - << passedEvents_ << "/" << totalEvents_ << " events" << std::endl; + << passedEvents_ << "/" << totalEvents_ << " events" << std::endl; } //define this as a plug-in From a4863f07bf8000836de45b683673ac998d6b8a4d Mon Sep 17 00:00:00 2001 From: perrozzi Date: Sun, 13 Sep 2015 10:30:51 +0200 Subject: [PATCH 2/3] add enum --- .../GenFilters/interface/LHEGenericFilter.h | 2 ++ .../GenFilters/src/LHEGenericFilter.cc | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h b/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h index 81bc8a36189c4..919cfee693bf5 100644 --- a/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h +++ b/GeneratorInterface/GenFilters/interface/LHEGenericFilter.h @@ -56,5 +56,7 @@ class LHEGenericFilter : public edm::EDFilter { std::vector particleID_; // vector of particle IDs to look for int totalEvents_; // counters int passedEvents_; + enum logic_ { LT, GT, EQ, NE}; + logic_ whichlogic; }; #endif diff --git a/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc b/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc index be7586c11f1fe..dd1f97fbcb162 100644 --- a/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc +++ b/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc @@ -11,10 +11,17 @@ totalEvents_(0), passedEvents_(0) { //here do whatever other initialization is needed src_ = consumes(iConfig.getParameter("src")); + + if(acceptLogic_.compare("LT")==0) whichlogic = LT; + else if(acceptLogic_.compare("GT")==0) whichlogic = GT; + else if(acceptLogic_.compare("EQ")==0) whichlogic = EQ; + else if(acceptLogic_.compare("NE")==0) whichlogic = NE; + } LHEGenericFilter::~LHEGenericFilter() { + // do anything here that needs to be done at destruction time // (e.g. close files, deallocate resources etc.) @@ -45,10 +52,10 @@ bool LHEGenericFilter::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) // event accept/reject logic if ( - (acceptLogic_.compare("LT")==0 && nFound < numRequired_) - || (acceptLogic_.compare("GT")==0 && nFound > numRequired_) - || (acceptLogic_.compare("EQ")==0 && nFound == numRequired_) - || (acceptLogic_.compare("NE")==0 && nFound != numRequired_) + (whichlogic==LT && nFound < numRequired_) + || (whichlogic==GT && nFound > numRequired_) + || (whichlogic==EQ && nFound == numRequired_) + || (whichlogic==NE && nFound != numRequired_) ) { passedEvents_++; return true; From 32c66f9f690b125122551fa83901392488fa9955 Mon Sep 17 00:00:00 2001 From: perrozzi Date: Tue, 15 Sep 2015 15:12:55 +0200 Subject: [PATCH 3/3] add edm::LogError --- GeneratorInterface/GenFilters/src/LHEGenericFilter.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc b/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc index dd1f97fbcb162..232938dfca027 100644 --- a/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc +++ b/GeneratorInterface/GenFilters/src/LHEGenericFilter.cc @@ -16,6 +16,8 @@ totalEvents_(0), passedEvents_(0) else if(acceptLogic_.compare("GT")==0) whichlogic = GT; else if(acceptLogic_.compare("EQ")==0) whichlogic = EQ; else if(acceptLogic_.compare("NE")==0) whichlogic = NE; + else edm::LogError ("cat_A") << "wrong input for AcceptLogic string"; + }