CMS 3D CMS Logo

MCSingleParticleYPt.cc
Go to the documentation of this file.
1 // Filter based on MCSingleParticleFilter.cc, but using rapidity instead of eta
2 
5 #include <iostream>
6 
7 using namespace edm;
8 using namespace std;
9 
11  : token_(consumes<edm::HepMCProduct>(
12  edm::InputTag(iConfig.getUntrackedParameter("moduleLabel", std::string("generator")), "unsmeared"))) {
13  fVerbose = iConfig.getUntrackedParameter("verbose", 0);
14  fchekantiparticle = iConfig.getUntrackedParameter("CheckAntiparticle", true);
15  //here do whatever other initialization is needed
16  vector<int> defpid;
17  defpid.push_back(0);
18  particleID = iConfig.getUntrackedParameter<vector<int> >("ParticleID", defpid);
19  vector<double> defptmin;
20  defptmin.push_back(0.);
21  ptMin = iConfig.getUntrackedParameter<vector<double> >("MinPt", defptmin);
22  vector<double> defrapmin;
23  defrapmin.push_back(-10.);
24  rapMin = iConfig.getUntrackedParameter<vector<double> >("MinY", defrapmin);
25  vector<double> defrapmax;
26  defrapmax.push_back(10.);
27  rapMax = iConfig.getUntrackedParameter<vector<double> >("MaxY", defrapmax);
28  vector<int> defstat;
29  defstat.push_back(0);
30  status = iConfig.getUntrackedParameter<vector<int> >("Status", defstat);
31 
32  // check for same size
33  if ((ptMin.size() > 1 && particleID.size() != ptMin.size()) ||
34  (rapMin.size() > 1 && particleID.size() != rapMin.size()) ||
35  (rapMax.size() > 1 && particleID.size() != rapMax.size()) ||
36  (status.size() > 1 && particleID.size() != status.size())) {
37  edm::LogWarning("MCSingleParticleYPt")
38  << "WARNING: MCSingleParticleYPt : size of vector cuts do not match!!" << endl;
39  }
40 
41  // if ptMin size smaller than particleID , fill up further with defaults
42  if (particleID.size() > ptMin.size()) {
43  vector<double> defptmin2;
44  for (unsigned int i = 0; i < particleID.size(); i++) {
45  defptmin2.push_back(0.);
46  }
47  ptMin = defptmin2;
48  }
49  // if etaMin size smaller than particleID , fill up further with defaults
50  if (particleID.size() > rapMin.size()) {
51  vector<double> defrapmin2;
52  for (unsigned int i = 0; i < particleID.size(); i++) {
53  defrapmin2.push_back(-10.);
54  }
55  rapMin = defrapmin2;
56  }
57  // if etaMax size smaller than particleID , fill up further with defaults
58  if (particleID.size() > rapMax.size()) {
59  vector<double> defrapmax2;
60  for (unsigned int i = 0; i < particleID.size(); i++) {
61  defrapmax2.push_back(10.);
62  }
63  rapMax = defrapmax2;
64  }
65  // if status size smaller than particleID , fill up further with defaults
66  if (particleID.size() > status.size()) {
67  vector<int> defstat2;
68  for (unsigned int i = 0; i < particleID.size(); i++) {
69  defstat2.push_back(0);
70  }
71  status = defstat2;
72  }
73 
74  if (fVerbose > 0) {
75  edm::LogInfo("MCSingleParticleYPt") << "----------------------------------------------------------------------"
76  << std::endl;
77  edm::LogInfo("MCSingleParticleYPt") << "----- MCSingleParticleYPt" << std::endl;
78  for (unsigned int i = 0; i < particleID.size(); ++i) {
79  edm::LogInfo("MCSingleParticleYPt") << " ID: " << particleID[i] << " pT > " << ptMin[i] << ", " << rapMin[i]
80  << " < y < " << rapMax[i] << ", status = " << status[i] << std::endl;
81  }
83  edm::LogInfo("MCSingleParticleYPt") << " anti-particles will be tested as well." << std::endl;
84  edm::LogInfo("MCSingleParticleYPt") << "----------------------------------------------------------------------"
85  << std::endl;
86  }
87 }
88 
90  // do anything here that needs to be done at desctruction time
91  // (e.g. close files, deallocate resources etc.)
92 }
93 
94 // ------------ method called to skim the data ------------
96  using namespace edm;
97  bool accepted = false;
99  iEvent.getByToken(token_, evt);
100 
101  const HepMC::GenEvent* myGenEvent = evt->GetEvent();
102  for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end();
103  ++p) {
104  if (fVerbose > 3)
105  edm::LogInfo("MCSingleParticleYPt")
106  << "Looking at particle : " << (*p)->pdg_id() << " status : " << (*p)->status() << std::endl;
107 
108  for (unsigned int i = 0; i < particleID.size(); i++) {
109  if (particleID[i] == (*p)->pdg_id() || (fchekantiparticle && (-particleID[i] == (*p)->pdg_id())) ||
110  particleID[i] == 0) {
111  // calculate rapidity just for the desired particle and make sure, this particles has enough energy
112  rapidity = ((*p)->momentum().e() - (*p)->momentum().pz()) > 0.
113  ? 0.5 * log(((*p)->momentum().e() + (*p)->momentum().pz()) /
114  ((*p)->momentum().e() - (*p)->momentum().pz()))
115  : rapMax[i] + .1;
116  if (fVerbose > 2)
117  edm::LogInfo("MCSingleParticleYPt")
118  << "Testing particle : " << (*p)->pdg_id() << " pT: " << (*p)->momentum().perp() << " y: " << rapidity
119  << " status : " << (*p)->status() << endl;
120  if ((*p)->momentum().perp() > ptMin[i] && rapidity > rapMin[i] && rapidity < rapMax[i] &&
121  ((*p)->status() == status[i] || status[i] == 0)) {
122  accepted = true;
123  if (fVerbose > 1)
124  edm::LogInfo("MCSingleParticleYPt")
125  << "Accepted particle : " << (*p)->pdg_id() << " pT: " << (*p)->momentum().perp() << " y: " << rapidity
126  << " status : " << (*p)->status() << endl;
127  break;
128  }
129  }
130  }
131  if (accepted)
132  break;
133  }
134 
135  if (accepted) {
136  return true;
137  } else {
138  return false;
139  }
140 }
T getUntrackedParameter(std::string const &, T const &) const
std::vector< int > particleID
std::vector< int > status
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
std::vector< double > rapMax
bool filter(edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
std::vector< double > ptMin
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:34
bool accepted(std::vector< std::string_view > const &, std::string_view)
HLT enums.
MCSingleParticleYPt(const edm::ParameterSet &)
edm::EDGetTokenT< edm::HepMCProduct > token_
std::vector< double > rapMin