CMS 3D CMS Logo

HLTTauMCProducer.cc
Go to the documentation of this file.
2 
3 using namespace edm;
4 using namespace std;
5 using namespace reco;
6 
8  // One Parameter Set per Collection
9 
10  MC_ = consumes<GenParticleCollection>(mc.getUntrackedParameter<edm::InputTag>("GenParticles"));
11  MCMET_ = consumes<GenMETCollection>(mc.getUntrackedParameter<edm::InputTag>("GenMET"));
12  ptMinMCTau_ = mc.getUntrackedParameter<double>("ptMinTau", 5.);
13  ptMinMCMuon_ = mc.getUntrackedParameter<double>("ptMinMuon", 2.);
14  ptMinMCElectron_ = mc.getUntrackedParameter<double>("ptMinElectron", 5.);
15  m_PDG_ = mc.getUntrackedParameter<std::vector<int>>("BosonID");
16  etaMin = mc.getUntrackedParameter<double>("EtaMin", -2.5);
17  etaMax = mc.getUntrackedParameter<double>("EtaMax", 2.5);
18  phiMin = mc.getUntrackedParameter<double>("PhiMin", -3.15);
19  phiMax = mc.getUntrackedParameter<double>("PhiMax", 3.15);
20 
21  produces<LorentzVectorCollection>("LeptonicTauLeptons");
22  produces<LorentzVectorCollection>("LeptonicTauElectrons");
23  produces<LorentzVectorCollection>("LeptonicTauMuons");
24  produces<LorentzVectorCollection>("HadronicTauOneProng");
25  produces<LorentzVectorCollection>("HadronicTauThreeProng");
26  produces<LorentzVectorCollection>("HadronicTauOneAndThreeProng");
27  produces<LorentzVectorCollection>("TauOther");
28  produces<LorentzVectorCollection>("Neutrina");
29  produces<LorentzVectorCollection>("MET");
30  produces<std::vector<int>>("Mothers");
31 }
32 
34 
36  // All the code from HLTTauMCInfo is here :-)
37 
38  unique_ptr<LorentzVectorCollection> product_Electrons(new LorentzVectorCollection);
39  unique_ptr<LorentzVectorCollection> product_Muons(new LorentzVectorCollection);
40  unique_ptr<LorentzVectorCollection> product_Leptons(new LorentzVectorCollection);
41  unique_ptr<LorentzVectorCollection> product_OneProng(new LorentzVectorCollection);
42  unique_ptr<LorentzVectorCollection> product_ThreeProng(new LorentzVectorCollection);
43  unique_ptr<LorentzVectorCollection> product_OneAndThreeProng(new LorentzVectorCollection);
44  unique_ptr<LorentzVectorCollection> product_Other(new LorentzVectorCollection);
45  unique_ptr<LorentzVectorCollection> product_Neutrina(new LorentzVectorCollection);
46  unique_ptr<LorentzVectorCollection> product_MET(new LorentzVectorCollection);
47  unique_ptr<std::vector<int>> product_Mothers(new std::vector<int>);
48 
50  iEvent.getByToken(MC_, genParticles);
51 
52  if (!genParticles.isValid())
53  return;
54 
55  // Look for MET
57  iEvent.getByToken(MCMET_, genMet);
58  LorentzVector MET(0., 0., 0., 0.);
59  if (genMet.isValid()) {
60  MET = LorentzVector(genMet->front().px(), genMet->front().py(), 0, genMet->front().pt());
61  }
62  product_MET->push_back(MET);
63 
64  // Look for primary bosons
65  // It is not guaranteed that primary bosons are stored in event history.
66  // Is it really needed when check if taus from the boson is removed?
67  // Kept for backward compatibility
68  for (GenParticleCollection::const_iterator p = genParticles->begin(); p != genParticles->end(); ++p) {
69  // Check the PDG ID
70  bool pdg_ok = false;
71  for (size_t pi = 0; pi < m_PDG_.size(); ++pi) {
72  if (abs((*p).pdgId()) == m_PDG_[pi] && ((*p).isHardProcess() || (*p).status() == 3)) {
73  pdg_ok = true;
74  // cout<<" Bsoson particles: "<< (*p).pdgId()<< " " <<(*p).status() << "
75  // "<< pdg_ok<<endl;
76  break;
77  }
78  }
79 
80  // Check if the boson is one of interest and if there is a valid vertex
81  if (pdg_ok) {
82  product_Mothers->push_back((*p).pdgId());
83 
84  TLorentzVector Boson((*p).px(), (*p).py(), (*p).pz(), (*p).energy());
85  }
86  } // End of search for the bosons
87 
88  // Look for taus
89  GenParticleRefVector allTaus;
90  unsigned index = 0;
91  for (GenParticleCollection::const_iterator p = genParticles->begin(); p != genParticles->end(); ++p, ++index) {
92  const GenParticle &genP = *p;
93  // accept only isPromptDecayed() particles
94  if (!genP.isPromptDecayed())
95  continue;
96  // check if it is tau, i.e. if |pdgId|=15
97  if (std::abs(genP.pdgId()) == 15) {
98  GenParticleRef genRef(genParticles, index);
99  // check if it is the last tau in decay/radiation chain
100  GenParticleRefVector daugTaus;
101  getGenDecayProducts(genRef, daugTaus, 0, 15);
102  if (daugTaus.empty())
103  allTaus.push_back(genRef);
104  }
105  }
106 
107  // Find stable tau decay products and build visible taus
108  for (GenParticleRefVector::const_iterator t = allTaus.begin(); t != allTaus.end(); ++t) {
109  // look for all stable (status=1) decay products
110  GenParticleRefVector decayProducts;
111  getGenDecayProducts(*t, decayProducts, 1);
112 
113  // build visible taus and recognize decay mode
114  if (!decayProducts.empty()) {
115  LorentzVector Visible_Taus(0., 0., 0., 0.);
116  LorentzVector TauDecayProduct(0., 0., 0., 0.);
117  LorentzVector Neutrino(0., 0., 0., 0.);
118 
119  int numElectrons = 0;
120  int numMuons = 0;
121  int numChargedPions = 0;
122  int numNeutralPions = 0;
123  int numPhotons = 0;
124  int numNeutrinos = 0;
125  int numOtherParticles = 0;
126 
127  for (GenParticleRefVector::const_iterator pit = decayProducts.begin(); pit != decayProducts.end(); ++pit) {
128  int pdg_id = abs((*pit)->pdgId());
129  if (pdg_id == 11)
130  numElectrons++;
131  else if (pdg_id == 13)
132  numMuons++;
133  else if (pdg_id == 211 || pdg_id == 321)
134  numChargedPions++; // Count both pi+ and K+
135  else if (pdg_id == 111 || pdg_id == 130 || pdg_id == 310)
136  numNeutralPions++; // Count both pi0 and K0_L/S
137  else if (pdg_id == 12 || pdg_id == 14 || pdg_id == 16) {
138  numNeutrinos++;
139  if (pdg_id == 16) {
140  Neutrino.SetPxPyPzE((*pit)->px(), (*pit)->py(), (*pit)->pz(), (*pit)->energy());
141  }
142  } else if (pdg_id == 22)
143  numPhotons++;
144  else {
145  numOtherParticles++;
146  }
147 
148  if (pdg_id != 12 && pdg_id != 14 && pdg_id != 16) {
149  TauDecayProduct.SetPxPyPzE((*pit)->px(), (*pit)->py(), (*pit)->pz(), (*pit)->energy());
150  Visible_Taus += TauDecayProduct;
151  }
152  // cout<< "This has to be the same: " << (*pit)->pdgId()
153  //<< " "<< (*pit)->status()<< " mother: "<< (*pit)->mother()->pdgId() <<
154  // endl;
155  }
156 
157  int tauDecayMode = kOther;
158 
159  if (numOtherParticles == 0) {
160  if (numElectrons == 1) {
161  //--- tau decays into electrons
162  tauDecayMode = kElectron;
163  } else if (numMuons == 1) {
164  //--- tau decays into muons
165  tauDecayMode = kMuon;
166  } else {
167  //--- hadronic tau decays
168  switch (numChargedPions) {
169  case 1:
170  if (numNeutralPions != 0) {
171  tauDecayMode = kOther;
172  break;
173  }
174  switch (numPhotons) {
175  case 0:
176  tauDecayMode = kOneProng0pi0;
177  break;
178  case 2:
179  tauDecayMode = kOneProng1pi0;
180  break;
181  case 4:
182  tauDecayMode = kOneProng2pi0;
183  break;
184  default:
185  tauDecayMode = kOther;
186  break;
187  }
188  break;
189  case 3:
190  if (numNeutralPions != 0) {
191  tauDecayMode = kOther;
192  break;
193  }
194  switch (numPhotons) {
195  case 0:
196  tauDecayMode = kThreeProng0pi0;
197  break;
198  case 2:
199  tauDecayMode = kThreeProng1pi0;
200  break;
201  default:
202  tauDecayMode = kOther;
203  break;
204  }
205  break;
206  }
207  }
208  }
209 
210  // cout<< "So we have a: " << tauDecayMode <<endl;
211  if (tauDecayMode == kElectron) {
212  if ((Visible_Taus.eta() > etaMin && Visible_Taus.eta() < etaMax && Visible_Taus.phi() > phiMin &&
213  Visible_Taus.phi() < phiMax) &&
214  (Visible_Taus.pt() > ptMinMCElectron_)) {
215  product_Electrons->push_back(Visible_Taus);
216  product_Leptons->push_back(Visible_Taus);
217  }
218  } else if (tauDecayMode == kMuon) {
219  if ((Visible_Taus.eta() > etaMin && Visible_Taus.eta() < etaMax && Visible_Taus.phi() > phiMin &&
220  Visible_Taus.phi() < phiMax) &&
221  (Visible_Taus.pt() > ptMinMCMuon_)) {
222  product_Muons->push_back(Visible_Taus);
223  product_Leptons->push_back(Visible_Taus);
224  }
225  } else if (tauDecayMode == kOneProng0pi0 || tauDecayMode == kOneProng1pi0 || tauDecayMode == kOneProng2pi0) {
226  if ((Visible_Taus.eta() > etaMin && Visible_Taus.eta() < etaMax && Visible_Taus.phi() > phiMin &&
227  Visible_Taus.phi() < phiMax) &&
228  (Visible_Taus.pt() > ptMinMCTau_)) {
229  product_OneProng->push_back(Visible_Taus);
230  product_OneAndThreeProng->push_back(Visible_Taus);
231  product_Neutrina->push_back(Neutrino);
232  }
233  } else if (tauDecayMode == kThreeProng0pi0 || tauDecayMode == kThreeProng1pi0) {
234  if ((Visible_Taus.eta() > etaMin && Visible_Taus.eta() < etaMax && Visible_Taus.phi() > phiMin &&
235  Visible_Taus.phi() < phiMax) &&
236  (Visible_Taus.pt() > ptMinMCTau_)) {
237  product_ThreeProng->push_back(Visible_Taus);
238  product_OneAndThreeProng->push_back(Visible_Taus);
239  product_Neutrina->push_back(Neutrino);
240  }
241  } else if (tauDecayMode == kOther) {
242  if ((Visible_Taus.eta() > etaMin && Visible_Taus.eta() < etaMax && Visible_Taus.phi() > phiMin &&
243  Visible_Taus.phi() < phiMax) &&
244  (Visible_Taus.pt() > ptMinMCTau_)) {
245  product_Other->push_back(Visible_Taus);
246  }
247  }
248  }
249  }
250 
251  iEvent.put(std::move(product_Leptons), "LeptonicTauLeptons");
252  iEvent.put(std::move(product_Electrons), "LeptonicTauElectrons");
253  iEvent.put(std::move(product_Muons), "LeptonicTauMuons");
254  iEvent.put(std::move(product_OneProng), "HadronicTauOneProng");
255  iEvent.put(std::move(product_ThreeProng), "HadronicTauThreeProng");
256  iEvent.put(std::move(product_OneAndThreeProng), "HadronicTauOneAndThreeProng");
257  iEvent.put(std::move(product_Other), "TauOther");
258  iEvent.put(std::move(product_Neutrina), "Neutrina");
259  iEvent.put(std::move(product_MET), "MET");
260  iEvent.put(std::move(product_Mothers), "Mothers");
261 }
262 
263 // Helper Function
264 
267  int status,
268  int pdgId) {
269  const GenParticleRefVector &daughterRefs = mother->daughterRefVector();
270 
271  for (GenParticleRefVector::const_iterator d = daughterRefs.begin(); d != daughterRefs.end(); ++d) {
272  if ((status == 0 || (*d)->status() == status) && (pdgId == 0 || std::abs((*d)->pdgId()) == pdgId)) {
273  products.push_back(*d);
274  } else
275  getGenDecayProducts(*d, products, status, pdgId);
276  }
277 }
int pdgId() const final
PDG identifier.
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:104
~HLTTauMCProducer() override
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
ESProducts< std::remove_reference_t< TArgs >... > products(TArgs &&...args)
Definition: ESProducts.h:128
const Double_t pi
HLTTauMCProducer(const edm::ParameterSet &)
void produce(edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
void getGenDecayProducts(const reco::GenParticleRef &, reco::GenParticleRefVector &, int status=1, int pdgId=0)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
math::XYZTLorentzVector LorentzVector
bool isValid() const
Definition: HandleBase.h:74
std::vector< LorentzVector > LorentzVectorCollection
bool isPromptDecayed() const
Definition: GenParticle.h:58
TLorentzVector genMet(const HepMC::GenEvent *all, double etamin=-9999., double etamax=9999.)
fixed size matrix
HLT enums.
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:69
def move(src, dest)
Definition: eostools.py:511
math::PtEtaPhiELorentzVectorF LorentzVector