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