CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TauSpinnerCMS.cc
Go to the documentation of this file.
2 
3 //MC-TESTER header files
4 #include "Tauola/Tauola.h"
5 #include "TauSpinner/tau_reweight_lib.h"
6 #include "TauSpinner/Tauola_wrapper.h"
8 #include "TLorentzVector.h"
9 
10 #include "CLHEP/Random/RandomEngine.h"
14 
15 using namespace edm;
16 using namespace TauSpinner;
17 
18 CLHEP::HepRandomEngine* TauSpinnerCMS::fRandomEngine= nullptr;
19 
21 
23  isReco_(pset.getParameter<bool>("isReco"))
24  ,isTauolaConfigured_(pset.getParameter<bool>("isTauolaConfigured" ))
25  ,isLHPDFConfigured_(pset.getParameter<bool>("isLHPDFConfigured" ))
26  ,LHAPDFname_(pset.getUntrackedParameter("LHAPDFname",(string)("MSTW2008nnlo90cl.LHgrid")))
27  ,CMSEnergy_(pset.getParameter<double>("CMSEnergy"))//GeV
28  ,gensrc_(pset.getParameter<edm::InputTag>("gensrc"))
29  ,MotherPDGID_(pset.getUntrackedParameter("MotherPDGID",(int)(-1)))
30  ,Ipol_(pset.getUntrackedParameter("Ipol",(int)(0)))
31  ,nonSM2_(pset.getUntrackedParameter("nonSM2",(int)(0)))
32  ,nonSMN_(pset.getUntrackedParameter("nonSMN",(int)(0)))
33  ,roundOff_(pset.getUntrackedParameter("roundOff",(double)(0.01)))
34 {
35  produces<bool>("TauSpinnerWTisValid").setBranchAlias("TauSpinnerWTisValid");
36  produces<double>("TauSpinnerWT").setBranchAlias("TauSpinnerWT");
37  produces<double>("TauSpinnerWTFlip").setBranchAlias("TauSpinnerWTFlip");
38  produces<double>("TauSpinnerWThplus").setBranchAlias("TauSpinnerWThplus");
39  produces<double>("TauSpinnerWThminus").setBranchAlias("TauSpinnerWThminus");
40 
41 }
42 
44 {
46  if(!rng.isAvailable()) {
47  throw cms::Exception("Configuration")
48  << "The RandomNumberProducer module requires the RandomNumberGeneratorService\n"
49  "which appears to be absent. Please add that service to your configuration\n"
50  "or remove the modules that require it." << std::endl;
51  }
52  fRandomEngine = &rng->getEngine();
53 
55  Tauolapp::Tauola::setRandomGenerator(TauSpinnerCMS::flat);
56  Tauolapp::Tauola::initialize();
57  }
58  if(!isLHPDFConfigured_){
59  LHAPDF::initPDFSetByName(LHAPDFname_);
60  }
63  bool Ipp = true; // for pp collisions
64  // Initialize TauSpinner
65  //Ipol - polarization of input sample
66  //nonSM2 - nonstandard model calculations
67  //nonSMN
68  TauSpinner::initialize_spinner(Ipp,Ipol_,nonSM2_,nonSMN_,CMSEnergy_);
69  }
70 }
71 
73  double WT=1.0;
74  double WTFlip=1.0;
75  double polSM=-999; //range [-1,1]
76  SimpleParticle X, tau, tau2;
77  std::vector<SimpleParticle> tau_daughters, tau_daughters2;
78  int stat(0);
79  if(isReco_){
80  stat=readParticlesfromReco(e,X,tau,tau2,tau_daughters,tau_daughters2);
81  }
82  else{
83  Handle< HepMCProduct > EvtHandle ;
84  e.getByLabel( "generator", EvtHandle ) ;
85  const HepMC::GenEvent* Evt = EvtHandle->GetEvent() ;
86  stat=readParticlesFromHepMC(Evt,X,tau,tau2,tau_daughters,tau_daughters2);
87  }
88  if(MotherPDGID_<0 || abs(X.pdgid())==MotherPDGID_){
89  if(stat!=1){
90  // Determine the weight
91  if( abs(X.pdgid())==24 || abs(X.pdgid())==37 ){
92  TLorentzVector tau_1r(0,0,0,0);
93  TLorentzVector tau_1(tau.px(),tau.py(),tau.pz(),tau.e());
94  for(unsigned int i=0; i<tau_daughters.size();i++){
95  tau_1r+=TLorentzVector(tau_daughters.at(i).px(),tau_daughters.at(i).py(),tau_daughters.at(i).pz(),tau_daughters.at(i).e());
96  }
97  if(fabs(tau_1r.M()-tau_1.M())<roundOff_){
98  WT = TauSpinner::calculateWeightFromParticlesWorHpn(X, tau, tau2, tau_daughters); // note that tau2 is tau neutrino
99  polSM=getTauSpin();
100  WTFlip=(2.0-WT)/WT;
101  }
102  }
103  else if( X.pdgid()==25 || X.pdgid()==36 || X.pdgid()==22 || X.pdgid()==23 ){
104  TLorentzVector tau_1r(0,0,0,0), tau_2r(0,0,0,0);
105  TLorentzVector tau_1(tau.px(),tau.py(),tau.pz(),tau.e()), tau_2(tau2.px(),tau2.py(),tau2.pz(),tau2.e());
106  for(unsigned int i=0; i<tau_daughters.size();i++){
107  tau_1r+=TLorentzVector(tau_daughters.at(i).px(),tau_daughters.at(i).py(),tau_daughters.at(i).pz(),tau_daughters.at(i).e());
108  }
109  for(unsigned int i=0; i<tau_daughters2.size();i++){
110  tau_2r+=TLorentzVector(tau_daughters2.at(i).px(),tau_daughters2.at(i).py(),tau_daughters2.at(i).pz(),tau_daughters2.at(i).e());
111  }
112 
113  if(fabs(tau_1r.M()-tau_1.M())<roundOff_ && fabs(tau_2r.M()-tau_2.M())<roundOff_){
114  WT = TauSpinner::calculateWeightFromParticlesH(X, tau, tau2, tau_daughters,tau_daughters2);
115  //std::cout << "WT " << WT << std::endl;
116  polSM=getTauSpin();
117  if(X.pdgid()==25 || X.pdgid()==22 || X.pdgid()==23 ){
118  if(X.pdgid()==25) X.setPdgid(23);
119  if( X.pdgid()==22 || X.pdgid()==23 ) X.setPdgid(25);
120 
121  double WTother=TauSpinner::calculateWeightFromParticlesH(X, tau, tau2, tau_daughters,tau_daughters2);
122  WTFlip=WTother/WT;
123  }
124  }
125  }
126  else{
127  cout<<"TauSpinner: WARNING: Unexpected PDG for tau mother: "<<X.pdgid()<<endl;
128  }
129  }
130  }
131  bool isValid=true;
132  if(!(0<=WT && WT<10)){isValid=false; WT=1.0; WTFlip=1.0;}
133  std::auto_ptr<bool> TauSpinnerWeightisValid(new bool);
134  *TauSpinnerWeightisValid =isValid;
135  e.put(TauSpinnerWeightisValid,"TauSpinnerWTisValid");
136 
137  // regular weight
138  std::auto_ptr<double> TauSpinnerWeight(new double);
139  *TauSpinnerWeight =WT;
140  e.put(TauSpinnerWeight,"TauSpinnerWT");
141 
142  // flipped weight (ie Z->H or H->Z)
143  std::auto_ptr<double> TauSpinnerWeightFlip(new double);
144  *TauSpinnerWeightFlip =WTFlip;
145  e.put(TauSpinnerWeightFlip,"TauSpinnerWTFlip");
146 
147  // h+ polarization
148  double WThplus=WT;
149  if(polSM<0.0 && polSM!=-999 && isValid) WThplus=0;
150  std::auto_ptr<double> TauSpinnerWeighthplus(new double);
151  *TauSpinnerWeighthplus = WThplus;
152  e.put(TauSpinnerWeighthplus,"TauSpinnerWThplus");
153 
154  // h- polarization
155  double WThminus=WT;
156  if(polSM>0.0&& polSM!=-999 && isValid) WThminus=0;
157  std::auto_ptr<double> TauSpinnerWeighthminus(new double);
158  *TauSpinnerWeighthminus = WThminus;
159  e.put(TauSpinnerWeighthminus,"TauSpinnerWThminus");
160 
161  return ;
162 }
163 
165 
167 
168 int TauSpinnerCMS::readParticlesfromReco(edm::Event& e,SimpleParticle &X,SimpleParticle &tau,SimpleParticle &tau2,
169  std::vector<SimpleParticle> &tau_daughters,std::vector<SimpleParticle> &tau2_daughters){
171  e.getByLabel(gensrc_, genParticles);
172  for(reco::GenParticleCollection::const_iterator itr = genParticles->begin(); itr!= genParticles->end(); ++itr){
173  int pdgid=abs(itr->pdgId());
174  if(pdgid==24 || pdgid==37 || pdgid ==25 || pdgid==36 || pdgid==22 || pdgid==23 ){
175  const reco::GenParticle *hx=&(*itr);
176  if(!isFirst(hx)) continue;
177  GetLastSelf(hx);
178  const reco::GenParticle *recotau1=NULL;
179  const reco::GenParticle *recotau2=NULL;
180  unsigned int ntau(0),ntauornu(0);
181  for(unsigned int i=0; i<itr->numberOfDaughters(); i++){
182  const reco::Candidate *dau=itr->daughter(i);
183  if(abs(dau->pdgId())!=pdgid){
184  if(abs(dau->pdgId())==15 || abs(dau->pdgId())==16){
185  if(ntau==0 && abs(dau->pdgId())==15){
186  recotau1=static_cast<const reco::GenParticle*>(dau);
187  GetLastSelf(recotau1);
188  ntau++;
189  }
190  else if((ntau==1 && abs(dau->pdgId())==15) || abs(dau->pdgId())==16){
191  recotau2=static_cast<const reco::GenParticle*>(dau);
192  if(abs(dau->pdgId())==15){ntau++;GetLastSelf(recotau2);}
193  }
194  ntauornu++;
195  }
196  }
197  }
198  if((ntau==2 && ntauornu==2) || (ntau==1 && ntauornu==2)){
199  X.setPx(itr->p4().Px());
200  X.setPy(itr->p4().Py());
201  X.setPz(itr->p4().Pz());
202  X.setE (itr->p4().E());
203  X.setPdgid(itr->pdgId());
204  tau.setPx(recotau1->p4().Px());
205  tau.setPy(recotau1->p4().Py());
206  tau.setPz(recotau1->p4().Pz());
207  tau.setE (recotau1->p4().E());
208  tau.setPdgid(recotau1->pdgId());
209  GetRecoDaughters(recotau1,tau_daughters,recotau1->pdgId());
210  tau2.setPx(recotau2->p4().Px());
211  tau2.setPy(recotau2->p4().Py());
212  tau2.setPz(recotau2->p4().Pz());
213  tau2.setE (recotau2->p4().E());
214  tau2.setPdgid(recotau2->pdgId());
215  if(ntau==2)GetRecoDaughters(recotau2,tau2_daughters,recotau2->pdgId());
216  return 0;
217  }
218  }
219  }
220  return 1;
221 }
222 
224  for (unsigned int i=0; i< Particle->numberOfDaughters(); i++){
225  const reco::GenParticle *dau=static_cast<const reco::GenParticle*>(Particle->daughter(i));
226  if(Particle->pdgId()==dau->pdgId()){
227  Particle=dau;
228  GetLastSelf(Particle);
229  }
230  }
231 }
232 
234  for (unsigned int i=0; i< Particle->numberOfMothers(); i++){
235  const reco::GenParticle *moth=static_cast<const reco::GenParticle*>(Particle->mother(i));
236  if(Particle->pdgId()==moth->pdgId()){
237  return false;
238  }
239  }
240  return true;
241 }
242 
243 void TauSpinnerCMS::GetRecoDaughters(const reco::GenParticle *Particle,std::vector<SimpleParticle> &daughters, int parentpdgid){
244  if( Particle->numberOfDaughters()==0 || abs(Particle->pdgId())==111){
245  SimpleParticle tp(Particle->p4().Px(), Particle->p4().Py(), Particle->p4().Pz(), Particle->p4().E(), Particle->pdgId());
246  daughters.push_back(tp);
247  return;
248  }
249  for (unsigned int i=0; i< Particle->numberOfDaughters(); i++){
250  const reco::Candidate *dau=Particle->daughter(i);
251  GetRecoDaughters(static_cast<const reco::GenParticle*>(dau),daughters,Particle->pdgId());
252  }
253 }
254 
256  if ( !fRandomEngine ) {
257  throw cms::Exception("LogicError")
258  << "TauSpinnerCMS::flat: Attempt to generate random number when engine pointer is null\n"
259  << "This might mean that the code was modified to generate a random number outside the\n"
260  << "event and beginLuminosityBlock methods, which is not allowed.\n";
261  }
262  return fRandomEngine->flat();
263 }
264 
int i
Definition: DBlmapReader.cc:9
virtual int pdgId() const
PDG identifier.
void GetLastSelf(const reco::GenParticle *Particle)
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
static double flat()
bool isFirst(const reco::GenParticle *Particle)
int readParticlesFromHepMC(const HepMC::GenEvent *event, SimpleParticle &X, SimpleParticle &tau, SimpleParticle &tau2, std::vector< SimpleParticle > &tau_daughters, std::vector< SimpleParticle > &tau2_daughters)
#define X(str)
Definition: MuonsGrabber.cc:49
#define abs(x)
Definition: mlp_lapack.h:159
#define NULL
Definition: scimark2.h:8
double roundOff_
Definition: TauSpinnerCMS.h:74
virtual void endJob()
static CLHEP::HepRandomEngine * fRandomEngine
Definition: TauSpinnerCMS.h:75
void GetRecoDaughters(const reco::GenParticle *Particle, std::vector< TauSpinner::SimpleParticle > &daughters, int parentpdgid)
static bool isTauSpinnerConfigure
Definition: TauSpinnerCMS.h:60
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
virtual size_t numberOfMothers() const
number of mothers
virtual size_t numberOfDaughters() const
number of daughters
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
bool isAvailable() const
Definition: Service.h:47
int readParticlesfromReco(edm::Event &e, TauSpinner::SimpleParticle &X, TauSpinner::SimpleParticle &tau, TauSpinner::SimpleParticle &tau2, std::vector< TauSpinner::SimpleParticle > &tau_daughters, std::vector< TauSpinner::SimpleParticle > &tau2_daughters)
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
virtual int pdgId() const =0
PDG identifier.
bool isTauolaConfigured_
Definition: TauSpinnerCMS.h:54
std::string LHAPDFname_
Definition: TauSpinnerCMS.h:56
virtual void produce(edm::Event &, const edm::EventSetup &)
edm::InputTag gensrc_
Definition: TauSpinnerCMS.h:58
virtual void endRun(const edm::Run &, const edm::EventSetup &)
TauSpinnerCMS(const edm::ParameterSet &)
double CMSEnergy_
Definition: TauSpinnerCMS.h:57
tuple cout
Definition: gather_cfg.py:121
virtual void beginJob()
bool isLHPDFConfigured_
Definition: TauSpinnerCMS.h:55
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
virtual const Candidate * mother(size_type=0) const
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...
Definition: Run.h:33