CMS 3D CMS Logo

GenPUProtonProducer.cc
Go to the documentation of this file.
1 /* \class GenPUProtonProducer
2  *
3  * Modification of GenParticleProducer.
4  * Saves final state protons from HepMC events in Crossing Frame, in the generator-particle format.
5  *
6  * Note: Use the option USER_CXXFLAGS=-DEDM_ML_DEBUG with SCRAM in order to enable debug messages.
7  *
8  * March 9, 2017 : Initial version.
9  * March 14, 2017 : Updated debug messages.
10  * July 27, 2017 : Removed extra loop initially inherited from GenParticleProducer.
11  * August 17, 2017 : Replaced std::auto_ptr with std::unique_ptr.
12  * September 6, 2017 : Updated module to edm::global::EDProducer with ConvertParticle as RunCache following GenParticleProducer.
13  *
14  */
15 
20 
21 #include <vector>
22 #include <string>
23 #include <unordered_map>
24 
25 namespace {
26 
27  class ConvertParticle {
28  public:
29  static constexpr int PDGCacheMax = 32768;
30  static constexpr double mmToCm = 0.1;
31 
32  ConvertParticle()
33  : abortOnUnknownPDGCode_(true), initialized_(false), chargeP_(PDGCacheMax, 0), chargeM_(PDGCacheMax, 0){};
34 
35  ConvertParticle(bool abortOnUnknownPDGCode)
36  : abortOnUnknownPDGCode_(abortOnUnknownPDGCode),
37  initialized_(false),
38  chargeP_(PDGCacheMax, 0),
39  chargeM_(PDGCacheMax, 0){};
40 
41  ~ConvertParticle(){};
42 
43  bool initialized() const { return initialized_; }
44 
45  void init(HepPDT::ParticleDataTable const& pdt) {
46  if (!initialized_) {
47  for (HepPDT::ParticleDataTable::const_iterator p = pdt.begin(); p != pdt.end(); ++p) {
48  HepPDT::ParticleID const& id = p->first;
49  int pdgId = id.pid(), apdgId = std::abs(pdgId);
50  int q3 = id.threeCharge();
51  if (apdgId < PDGCacheMax && pdgId > 0) {
52  chargeP_[apdgId] = q3;
53  chargeM_[apdgId] = -q3;
54  } else if (apdgId < PDGCacheMax) {
55  chargeP_[apdgId] = -q3;
56  chargeM_[apdgId] = q3;
57  } else {
58  chargeMap_.emplace(pdgId, q3);
59  chargeMap_.emplace(-pdgId, -q3);
60  }
61  }
62  initialized_ = true;
63  }
64  }
65 
66  bool operator()(reco::GenParticle& cand, HepMC::GenParticle const* part) const {
68  int pdgId = part->pdg_id();
69  cand.setThreeCharge(chargeTimesThree(pdgId));
70  cand.setPdgId(pdgId);
71  cand.setStatus(part->status());
72  cand.setP4(p4);
73  cand.setCollisionId(0);
74  HepMC::GenVertex const* v = part->production_vertex();
75  if (v != nullptr) {
76  HepMC::ThreeVector vtx = v->point3d();
78  cand.setVertex(vertex);
79  } else {
80  cand.setVertex(reco::Candidate::Point(0, 0, 0));
81  }
82  return true;
83  }
84 
85  private:
86  bool abortOnUnknownPDGCode_;
87  bool initialized_;
88  std::vector<int> chargeP_, chargeM_;
89  std::unordered_map<int, int> chargeMap_;
90 
91  int chargeTimesThree(int id) const {
92  if (std::abs(id) < PDGCacheMax)
93  return id > 0 ? chargeP_[id] : chargeM_[-id];
94 
95  auto f = chargeMap_.find(id);
96  if (f == chargeMap_.end()) {
97  if (abortOnUnknownPDGCode_)
98  throw edm::Exception(edm::errors::LogicError) << "invalid PDG id: " << id << std::endl;
99  else
100  return HepPDT::ParticleID(id).threeCharge();
101  }
102  return f->second;
103  }
104  };
105 
106  class SelectProton {
107  public:
108  bool operator()(HepMC::GenParticle const* part, double minPz) const {
109  bool selection = ((!part->end_vertex() && part->status() == 1) && (part->pdg_id() == 2212) &&
110  (TMath::Abs(part->momentum().pz()) >= minPz));
111  return selection;
112  }
113  };
114 
115 } // Anonymous namespace
116 
121 
126 
127 namespace edm {
128  class ParameterSet;
129 }
130 
131 class GenPUProtonProducer : public edm::global::EDProducer<edm::RunCache<ConvertParticle> > {
132 public:
134  ~GenPUProtonProducer() override;
135 
136  void produce(edm::StreamID, edm::Event& e, const edm::EventSetup&) const override;
137  std::shared_ptr<ConvertParticle> globalBeginRun(const edm::Run&, const edm::EventSetup&) const override;
138  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override{};
139 
140 private:
142 
144  std::vector<int> bunchList_;
145  double minPz_;
146  SelectProton select_;
147 };
148 
156 
160 
161 #include <algorithm>
162 
163 using namespace edm;
164 using namespace reco;
165 using namespace std;
166 using namespace HepMC;
167 
169  : abortOnUnknownPDGCode_(cfg.getUntrackedParameter<bool>("abortOnUnknownPDGCode", true)),
170  bunchList_(cfg.getParameter<vector<int> >("bunchCrossingList")),
171  minPz_(cfg.getParameter<double>("minPz")) {
172  produces<GenParticleCollection>();
173  mixToken_ =
174  consumes<CrossingFrame<HepMCProduct> >(InputTag(cfg.getParameter<std::string>("mix"), "generatorSmeared"));
175 }
176 
178 
179 std::shared_ptr<ConvertParticle> GenPUProtonProducer::globalBeginRun(const Run&, const EventSetup& es) const {
181  es.getData(pdt);
182  auto convert_ptr = std::make_shared<ConvertParticle>(abortOnUnknownPDGCode_);
183  if (!convert_ptr->initialized())
184  convert_ptr->init(*pdt);
185 
186  return convert_ptr;
187 }
188 
190  size_t totalSize = 0;
191  size_t npiles = 1;
192 
194  evt.getByToken(mixToken_, cf);
195  std::unique_ptr<MixCollection<HepMCProduct> > cfhepmcprod(new MixCollection<HepMCProduct>(cf.product()));
196  npiles = cfhepmcprod->size();
197 
198  LogDebug("GenPUProtonProducer") << " Number of pile-up events : " << npiles << endl;
199 
200  for (size_t icf = 0; icf < npiles; ++icf) {
201  LogDebug("GenPUProtonProducer") << "CF " << icf
202  << " size : " << cfhepmcprod->getObject(icf).GetEvent()->particles_size() << endl;
203  totalSize += cfhepmcprod->getObject(icf).GetEvent()->particles_size();
204  }
205  LogDebug("GenPUProtonProducer") << "Total size : " << totalSize << endl;
206 
207  // Initialise containers
208  auto candsPtr = std::make_unique<GenParticleCollection>();
209  GenParticleCollection& cands = *candsPtr;
210 
211  // Loop over pile-up events
212  ConvertParticle const& convertParticle_ = *runCache(evt.getRun().index());
213 
215  unsigned int total_number_of_protons = 0;
216  size_t idx_mix = 0;
217  // Fill collection
218  for (mixHepMC_itr = cfhepmcprod->begin(); mixHepMC_itr != cfhepmcprod->end(); ++mixHepMC_itr, ++idx_mix) {
219  int bunch = mixHepMC_itr.bunch();
220 
221  if (find(bunchList_.begin(), bunchList_.end(), bunch) != bunchList_.end()) {
222  auto event = (*mixHepMC_itr).GetEvent();
223 
224  size_t num_particles = event->particles_size();
225 
226  // Fill output collection
227  unsigned int number_of_protons = 0;
228  for (auto p = event->particles_begin(); p != event->particles_end(); ++p) {
229  HepMC::GenParticle const* part = *p;
230  if (select_(part, minPz_)) {
232  convertParticle_(cand, part);
233  ++number_of_protons;
234  cands.push_back(cand);
235  }
236  }
237  LogDebug("GenPUProtonProducer") << "Idx : " << idx_mix << " Bunch : " << bunch
238  << " Number of particles : " << num_particles
239  << " Number of protons : " << number_of_protons << endl;
240 
241  total_number_of_protons += number_of_protons;
242  }
243  }
244  LogDebug("GenPUProtonProducer") << "Total number of protons : " << total_number_of_protons << endl;
245  LogDebug("GenPUProtonProducer") << "Output collection size : " << cands.size() << endl;
246 
247  evt.put(std::move(candsPtr));
248 }
249 
251 
edm::StreamID
Definition: StreamID.h:30
Handle.h
init
int init
Definition: HydjetWrapper.h:64
electrons_cff.bool
bool
Definition: electrons_cff.py:372
funct::false
false
Definition: Factorize.h:34
edm::Handle::product
T const * product() const
Definition: Handle.h:70
ESHandle.h
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
GenPUProtonProducer::select_
SelectProton select_
Definition: GenPUProtonProducer.cc:146
edm::errors::LogicError
Definition: EDMException.h:37
reco::GenParticle
Definition: GenParticle.h:21
edm::Run
Definition: Run.h:45
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm::Run::index
RunIndex index() const
Definition: Run.cc:21
edm
HLT enums.
Definition: AlignableModifier.h:19
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CrossingFrame.h
reco::GenParticleCollection
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
Definition: GenParticleFwd.h:13
GenPUProtonProducer::globalEndRun
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
Definition: GenPUProtonProducer.cc:138
GenPUProtonProducer::abortOnUnknownPDGCode_
bool abortOnUnknownPDGCode_
Definition: GenPUProtonProducer.cc:143
GenPUProtonProducer::mixToken_
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > mixToken_
Definition: GenPUProtonProducer.cc:138
GenPUProtonProducer::produce
void produce(edm::StreamID, edm::Event &e, const edm::EventSetup &) const override
Definition: GenPUProtonProducer.cc:189
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
GenPUProtonProducer
Definition: GenPUProtonProducer.cc:131
findQualityFiles.v
v
Definition: findQualityFiles.py:179
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::Handle
Definition: AssociativeIterator.h:50
genPUProtons_cfi.minPz
minPz
Definition: genPUProtons_cfi.py:6
MixCollection::MixItr
Definition: MixCollection.h:62
GenParticle.h
CandidateFwd.h
EDMException.h
MakerMacros.h
MixCollection::MixItr::bunch
int bunch() const
Definition: MixCollection.h:91
part
part
Definition: HCALResponse.h:20
GeneratorMix_cff.abortOnUnknownPDGCode
abortOnUnknownPDGCode
Definition: GeneratorMix_cff.py:10
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Abs
T Abs(T a)
Definition: MathUtil.h:49
MixCollection.h
MixCollection
Definition: MixCollection.h:11
GenPUProtonProducer::~GenPUProtonProducer
~GenPUProtonProducer() override
Definition: GenPUProtonProducer.cc:177
GenParticleFwd.h
corrVsCorr.selection
selection
main part
Definition: corrVsCorr.py:100
Run.h
edm::ESHandle< HepPDT::ParticleDataTable >
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
edm::global::EDProducer
Definition: EDProducer.h:32
badGlobalMuonTaggersAOD_cff.vtx
vtx
Definition: badGlobalMuonTaggersAOD_cff.py:5
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
funct::true
true
Definition: Factorize.h:173
bphysicsOniaDQM_cfi.vertex
vertex
Definition: bphysicsOniaDQM_cfi.py:7
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:670
edm::ParameterSet
Definition: ParameterSet.h:36
Event.h
ParticleDataTable.h
cand
Definition: decayParser.h:34
createfilelist.int
int
Definition: createfilelist.py:10
p4
double p4[4]
Definition: TauolaWrapper.h:92
edm::Event::put
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:132
GenPUProtonProducer::GenPUProtonProducer
GenPUProtonProducer(const edm::ParameterSet &)
Definition: GenPUProtonProducer.cc:168
EgammaValidation_cff.pdgId
pdgId
Definition: EgammaValidation_cff.py:118
edm::EventSetup
Definition: EventSetup.h:57
InputTag.h
PDGCacheMax
static constexpr int PDGCacheMax
Definition: GenParticleProducer.cc:31
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:113
looper.cfg
cfg
Definition: looper.py:297
GenPUProtonProducer::bunchList_
std::vector< int > bunchList_
Definition: GenPUProtonProducer.cc:144
GenParticle.GenParticle
GenParticle
Definition: GenParticle.py:18
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
mmToCm
static const double mmToCm
Definition: GenParticleProducer.cc:142
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:31
HepMC
Definition: GenParticle.h:15
Exception
Definition: hltDiff.cc:246
HLT_2018_cff.cands
cands
Definition: HLT_2018_cff.py:13762
EventSetup.h
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
HepMCProduct.h
EDProducer.h
reco::Candidate::LorentzVector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
reco::Candidate::Point
math::XYZPoint Point
point in the space
Definition: Candidate.h:40
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
LHEGenericFilter_cfi.ParticleID
ParticleID
Definition: LHEGenericFilter_cfi.py:6
ParticleDataTable
HepPDT::ParticleDataTable ParticleDataTable
Definition: ParticleDataTable.h:8
GenPUProtonProducer::globalBeginRun
std::shared_ptr< ConvertParticle > globalBeginRun(const edm::Run &, const edm::EventSetup &) const override
Definition: GenPUProtonProducer.cc:179
edm::Event::getRun
Run const & getRun() const
Definition: Event.cc:107
GenPUProtonProducer::minPz_
double minPz_
Definition: GenPUProtonProducer.cc:145
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37