CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/TauAnalysis/MCEmbeddingTools/src/ParticleReplacerParticleGun.cc

Go to the documentation of this file.
00001 #include "TauAnalysis/MCEmbeddingTools/interface/ParticleReplacerParticleGun.h"
00002 #include "TauAnalysis/MCEmbeddingTools/interface/extraPythia.h"
00003 
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 
00006 #include "HepMC/PythiaWrapper.h"
00007 #include "HepMC/IO_HEPEVT.h"
00008 
00009 ParticleReplacerParticleGun::ParticleReplacerParticleGun(const edm::ParameterSet& iConfig, bool verbose):
00010   ParticleReplacerBase(iConfig),
00011   tauola_(iConfig.getParameter<edm::ParameterSet>("ExternalDecays").getParameter<edm::ParameterSet>("Tauola")),
00012   //tauola_(gen::TauolaInterface::getInstance()),
00013   pythia_(iConfig),
00014   particleOrigin_(iConfig.getParameter<std::string>("particleOrigin")),
00015   forceTauPolarization_(iConfig.getParameter<std::string>("forceTauPolarization")),
00016   forceTauDecay_(iConfig.getParameter<std::string>("forceTauDecay")),
00017   generatorMode_(iConfig.getParameter<std::string>("generatorMode")),
00018   gunParticle_(iConfig.getParameter<int>("gunParticle")),
00019   forceTauPlusHelicity_(iConfig.getParameter<int>("forceTauPlusHelicity")),
00020   forceTauMinusHelicity_(iConfig.getParameter<int>("forceTauMinusHelicity")),
00021   printout_(verbose) {
00022   //tauola_->setPSet(iConfig.getParameter<edm::ParameterSet>("ExternalDecays").getParameter<edm::ParameterSet>("Tauola"));
00023   srand(time(NULL)); // Should we use RandomNumberGenerator service?
00024 
00025   if(forceTauPlusHelicity_ != 0) 
00026     edm::LogInfo("MuonReplacement") << "[ParticleReplacer::ParticleReplacer] "
00027                                     << "Forcing tau+ to helicity " << forceTauPlusHelicity_ << std::endl;
00028   if(forceTauMinusHelicity_ != 0)
00029     edm::LogInfo("MuonReplacement") << "[ParticleReplacer::ParticleReplacer] "
00030                                     << "Forcing tau- to helicity " << forceTauMinusHelicity_ << std::endl;
00031   if(forceTauPlusHelicity_ == 0 && forceTauMinusHelicity_ == 0)
00032     edm::LogInfo("MuonReplacement") << "[ParticleReplacer::ParticleReplacer] "
00033                                     << "     Forcing tau helicity as decayed from a " << forceTauPolarization_ << std::endl; 
00034   if(forceTauDecay_ != "" && forceTauDecay_ != "none")
00035     edm::LogInfo("MuonReplacement") << "[ParticleReplacer::ParticleReplacer] "
00036                                     << "Forcing tau decaying into " << forceTauDecay_ << std::endl;
00037 
00038   std::memset(pol1_, 0, 4*sizeof(float));
00039   std::memset(pol2_, 0, 4*sizeof(float));
00040 
00041   if(generatorMode_ != "Tauola")
00042     throw cms::Exception("Configuration") << "Generator mode other than Tauola is not supported" << std::endl;
00043 
00044   throw cms::Exception("UnimplementedFeature") << "ParticleReplacerParticleGun is not usable yet." << std::endl;
00045 }
00046 
00047 ParticleReplacerParticleGun::~ParticleReplacerParticleGun() {}
00048 
00049 void ParticleReplacerParticleGun::beginJob() {
00050   gen::Pythia6Service::InstanceWrapper guard(&pythia_);
00051 
00052   pythia_.setGeneralParams();
00053 
00054   if(abs(gunParticle_) == 15) {
00055     /* FIXME
00056     call_tauola(-1,1);
00057     */
00058   }
00059 }
00060 
00061 void ParticleReplacerParticleGun::endJob() {
00062   if(abs(gunParticle_) == 15) {
00063     /* FIXME
00064        call_tauola(1,1);
00065     */
00066   }
00067 }
00068 
00069 std::auto_ptr<HepMC::GenEvent> ParticleReplacerParticleGun::produce(const reco::MuonCollection& muons, const reco::Vertex *pvtx, const HepMC::GenEvent *genEvt) {
00070   if(genEvt != 0)
00071     throw cms::Exception("UnimplementedFeature") << "ParticleReplacerParticleGun does NOT support merging at HepMC level" << std::endl;
00072 
00073   std::auto_ptr<HepMC::GenEvent> evt(0);
00074   std::vector<HepMC::FourVector> muons_corrected;
00075   muons_corrected.reserve(muons.size());
00076   correctTauMass(muons, muons_corrected);
00077 
00078   gen::Pythia6Service::InstanceWrapper guard(&pythia_);
00079 
00080   for(unsigned int i=0; i<muons_corrected.size(); ++i) {
00081     HepMC::FourVector& muon = muons_corrected[i];
00082     call_py1ent(i+1, gunParticle_*muons[i].charge(), muon.e(), muon.theta(), muon.phi());
00083   }
00084 
00085   // Let's not do a call_pyexec here because it's unnecessary
00086 
00087   if(printout_) {
00088     std::cout << " /////////////////////  After py1ent, before pyhepc /////////////////////" << std::endl;
00089     call_pylist(3);
00090   }
00091 
00092   // Vertex shift
00093   call_pyhepc(1); // pythia -> hepevt
00094 
00095   if(printout_) {
00096     std::cout << " /////////////////////  After pyhepc, before vertex shift /////////////////////" << std::endl;
00097     HepMC::HEPEVT_Wrapper::print_hepevt();
00098   }
00099   // Search for HepMC/HEPEVT_Wrapper.h for the wrapper interface
00100   int nparticles = HepMC::HEPEVT_Wrapper::number_entries();
00101   HepMC::ThreeVector shift(0,0,0); 
00102   if(particleOrigin_ == "primaryVertex") {
00103     if(!pvtx)
00104       throw cms::Exception("LogicError") << "Particle origin set to primaryVertex, but pvtx is null!" << std::endl;
00105 
00106     shift.setX(pvtx->x()*10); // cm -> mm
00107     shift.setY(pvtx->y()*10); // cm -> mm
00108     shift.setZ(pvtx->z()*10); // cm -> mm
00109   }
00110   for(int i=1; i <= nparticles; ++i) {
00111     if(abs(HepMC::HEPEVT_Wrapper::id(i)) != abs(gunParticle_)) {
00112       throw cms::Exception("LogicError") << "Particle in HEPEVT is " << HepMC::HEPEVT_Wrapper::id(i)
00113                                          << " is not the same as gunParticle " << gunParticle_
00114                                          << " for index " << i << std::endl;
00115     }
00116 
00117     if(particleOrigin_ == "muonReferencePoint") {
00118       const reco::Muon& muon = muons[i-1];
00119       shift.setX(muon.vx()*10); // cm -> mm
00120       shift.setY(muon.vy()*10); // cm -> mm
00121       shift.setZ(muon.vz()*10); // cm -> mm
00122     }
00123 
00124     HepMC::HEPEVT_Wrapper::set_position(i,
00125                                         HepMC::HEPEVT_Wrapper::x(i) + shift.x(),
00126                                         HepMC::HEPEVT_Wrapper::y(i) + shift.y(),
00127                                         HepMC::HEPEVT_Wrapper::z(i) + shift.z(),
00128                                         HepMC::HEPEVT_Wrapper::t(i));
00129   }
00130 
00131   if(printout_) {
00132     std::cout << " /////////////////////  After vertex shift, before pyhepc/tauola /////////////////////" << std::endl;
00133     HepMC::HEPEVT_Wrapper::print_hepevt();
00134   }
00135 
00136   if(abs(gunParticle_) == 15){
00137     // Code example from TauolaInterface::processEvent()
00138     /* FIXME
00139     int dummy = -1;
00140     int numGenParticles_beforeTAUOLA = call_ihepdim(dummy);
00141     */
00142 
00143     forceTauolaTauDecays();
00144 
00145     for(unsigned int i=0; i<muons_corrected.size(); ++i) {
00146       tauola_forParticleGun(i+1, gunParticle_*muons[i].charge(), muons_corrected[i]);
00147     }
00148 
00149     if(printout_) {
00150       std::cout << " /////////////////////  After tauola, before pyhepc  /////////////////////" << std::endl;
00151       HepMC::HEPEVT_Wrapper::print_hepevt();
00152     }
00153     call_pyhepc(2); // hepevt->pythia
00154     if(printout_) {
00155       std::cout << " /////////////////////  After pyhepc, before vertex fix  /////////////////////" << std::endl;
00156       call_pylist(3);
00157     }
00158 
00159     // Fix tau decay vertex position
00160     /* FIXME
00161     int numGenParticles_afterTAUOLA = call_ihepdim(dummy);
00162     tauola_.setDecayVertex(numGenParticles_beforeTAUOLA, numGenParticles_afterTAUOLA);
00163     */
00164 
00165     if(printout_) {
00166       /* FIXME
00167       std::cout << "     numGenParticles_beforeTAUOLA " << numGenParticles_beforeTAUOLA << std::endl
00168                 << "     numGenParticles_afterTAUOLA  " << numGenParticles_afterTAUOLA << std::endl;
00169       */
00170       std::cout << " /////////////////////  After vertex fix, before pyexec  /////////////////////" << std::endl;
00171       call_pylist(3);
00172     }
00173   }
00174   else {
00175     call_pyhepc(2); // hepevt->pythia
00176     if(printout_) {
00177       std::cout << " /////////////////////  After pyhepc, before pyexec  /////////////////////" << std::endl;
00178       call_pylist(3);
00179     }
00180   }
00181 
00182   call_pyexec(); // taus: decay pi0's etc; others: decay whatever
00183 
00184   if(printout_) {
00185     std::cout << " /////////////////////  After pyexec, before pyhepc  /////////////////////" << std::endl;
00186     call_pylist(3);
00187   }
00188 
00189   call_pyhepc(1); // pythia -> hepevt
00190 
00191   HepMC::IO_HEPEVT conv;
00192   evt = std::auto_ptr<HepMC::GenEvent>(new HepMC::GenEvent(*conv.read_next_event()));
00193 
00194   if(printout_) {
00195     evt->print();
00196 
00197     std::cout << std::endl << "Vertices: " << std::endl;
00198     for(HepMC::GenEvent::vertex_const_iterator iter = evt->vertices_begin(); iter != evt->vertices_end(); ++iter) {
00199       std::cout << "Vertex " << (*iter)->id() << ", barcode " << (*iter)->barcode() << std::endl;
00200 
00201       HepMC::ThreeVector point = (*iter)->point3d();
00202       std::cout << "  Point (" << point.x() << ", " << point.y() << ", " << point.z() << ")" << std::endl;
00203 
00204       std::cout << "  Incoming particles: ";
00205       for(HepMC::GenVertex::particles_in_const_iterator pi = (*iter)->particles_in_const_begin(); pi != (*iter)->particles_in_const_end(); ++pi) {
00206         std::cout << (*pi)->pdg_id() << " ";
00207       }
00208       std::cout << std::endl;
00209       std::cout << "  Outgoing particles: ";
00210       for(HepMC::GenVertex::particles_out_const_iterator pi = (*iter)->particles_out_const_begin(); pi != (*iter)->particles_out_const_end(); ++pi) {
00211         std::cout << (*pi)->pdg_id() << " ";
00212       }
00213       std::cout << std::endl << std::endl;
00214     }
00215   }
00216 
00217   return evt;
00218 }
00219 
00220 void ParticleReplacerParticleGun::correctTauMass(const reco::MuonCollection& muons, std::vector<HepMC::FourVector>& corrected) {
00221   if(abs(gunParticle_) == 15) {
00222     // Correct energy for tau
00223     for(reco::MuonCollection::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
00224       const reco::Muon::LorentzVector& vec = iMuon->p4();
00225 
00226       double E = sqrt(tauMass*tauMass + vec.x()*vec.x() + vec.y()*vec.y() + vec.z()*vec.z());
00227 
00228       corrected.push_back(HepMC::FourVector(vec.x(), vec.y(), vec.z(), E));
00229     }
00230   }
00231   else {
00232     // Just copy the LorentzVector
00233     for(reco::MuonCollection::const_iterator iMuon = muons.begin(); iMuon != muons.end(); ++iMuon) {
00234       corrected.push_back(iMuon->p4());
00235     }
00236   }
00237 }
00238 
00239 void ParticleReplacerParticleGun::forceTauolaTauDecays() {
00240   if(forceTauDecay_ == "" || forceTauDecay_ == "none") return;
00241 
00242   // for documentation look at Tauola file tauola_photos_ini.f
00243   // COMMON block COMMON / TAUBRA / GAMPRT(30),JLIST(30),NCHAN
00244 
00245   if(forceTauDecay_ == "hadrons"){
00246     /* FIXME
00247     taubra.gamprt[0] = 0; // disable branchings to electrons in Tauola
00248     taubra.gamprt[1] = 0; // disable branchings to muons in Tauola
00249     */
00250   }
00251   else if(forceTauDecay_ == "1prong"){
00252     /* FIXME
00253     taubra.gamprt[0]  = 0; // disable branchings to electrons in Tauola
00254     taubra.gamprt[1]  = 0; // disable branchings to muons in Tauola
00255     taubra.gamprt[7]  = 0;
00256     taubra.gamprt[9]  = 0;
00257     taubra.gamprt[10] = 0;
00258     taubra.gamprt[11] = 0;
00259     taubra.gamprt[12] = 0;
00260     taubra.gamprt[13] = 0;
00261     taubra.gamprt[17] = 0;
00262     */
00263   }
00264   else if(forceTauDecay_ == "3prong"){
00265     /* FIXME
00266     taubra.gamprt[0]  = 0; // disable branchings to electrons in Tauola
00267     taubra.gamprt[1]  = 0; // disable branchings to muons in Tauola
00268     taubra.gamprt[2]  = 0;
00269     taubra.gamprt[3]  = 0;
00270     taubra.gamprt[4]  = 0;
00271     taubra.gamprt[5]  = 0;
00272     taubra.gamprt[6]  = 0;
00273     taubra.gamprt[8]  = 0;
00274     taubra.gamprt[14] = 0;
00275     taubra.gamprt[15] = 0;
00276     taubra.gamprt[16] = 0;
00277     taubra.gamprt[18] = 0;
00278     taubra.gamprt[19] = 0;
00279     taubra.gamprt[20] = 0;
00280     taubra.gamprt[21] = 0;
00281     */
00282   }
00283   else 
00284     edm::LogError("MuonReplacement") << "[ParticleReplacerAlgoParticleGun::forceTauoladecays] "
00285                                      << "Unknown value for forcing tau decays: " << forceTauDecay_ << std::endl;
00286 }
00287 
00288 void ParticleReplacerParticleGun::tauola_forParticleGun(int tau_idx, int pdg_id, const HepMC::FourVector& particle_momentum) {
00289   if(abs(pdg_id) != 15) {
00290     edm::LogError("MuonReplacement") << "[ParticleReplacerAlgoParticleGun::tauola_forParticleGuns] "
00291                                      << "Trying to decay something else than tau: pdg_id = " << pdg_id << std::endl;
00292     return;
00293   }
00294 
00295   // By default the index of tau+ is 3 and tau- is 4. The TAUOLA
00296   // routine takes internally care of finding the correct
00297   // position of the tau which is decayed. However, since we are
00298   // calling DEXAY routine directly by ourselves, we must set
00299   // the index manually by ourselves. Fortunately, this seems to
00300   // be simple.
00301   /* FIXME
00302   if(printout_)
00303     std::cout << " Tauola taupos common block: np1 " << taupos.np1 << " np2 " << taupos.np2 << std::endl;
00304   taupos.np1 = tau_idx;
00305   taupos.np2 = tau_idx;
00306   if(printout_)
00307     std::cout << " Resetting taupos common block to: np1 " << taupos.np1 << " np2 " << taupos.np2 << std::endl;
00308   */
00309 
00310   if(pdg_id == -15){ // tau+
00311 
00312     pol1_[2] = tauHelicity(pdg_id);
00313 
00314     /* FIXME
00315     momdec.p1[0] = particle_momentum.x();
00316     momdec.p1[1] = particle_momentum.y();
00317     momdec.p1[2] = particle_momentum.z();
00318     momdec.p1[3] = particle_momentum.e();
00319 
00320     momdec.p2[0] = -momdec.p1[0];
00321     momdec.p2[1] = -momdec.p1[1];
00322     momdec.p2[2] = -momdec.p1[2];
00323     momdec.p2[3] =  momdec.p1[3];
00324 
00325     // "mother" p4
00326     momdec.q1[0] = 0;
00327     momdec.q1[1] = 0;
00328     momdec.q1[2] = 0;
00329     momdec.q1[3] = momdec.p1[3] + momdec.p2[3];
00330 
00331     call_dexay(1,pol1);
00332     */
00333   }
00334   else if (pdg_id == 15){ // tau-
00335 
00336     pol2_[2] = tauHelicity(pdg_id);
00337 
00338     /* FIXME
00339     momdec.p2[0] = particle_momentum.x();
00340     momdec.p2[1] = particle_momentum.y();
00341     momdec.p2[2] = particle_momentum.z();
00342     momdec.p2[3] = particle_momentum.e();
00343 
00344     momdec.p1[0] = -momdec.p2[0];
00345     momdec.p1[1] = -momdec.p2[1];
00346     momdec.p1[2] = -momdec.p2[2];
00347     momdec.p1[3] =  momdec.p2[3];
00348 
00349     // "mother" p4
00350     momdec.q1[0] = 0;
00351     momdec.q1[1] = 0;
00352     momdec.q1[2] = 0;
00353     momdec.q1[3] = momdec.p1[3] + momdec.p2[3];
00354 
00355     call_dexay(2,pol2);
00356     */
00357   }
00358 }
00359 
00360 float ParticleReplacerParticleGun::tauHelicity(int pdg_id) {
00361   /* tau polarization summary from Tauola source code:
00362      in all cases      W : pol1 = -1, pol2 = -1 (tau or neutrino)
00363      H+: pol1 = +1, pol2 = +1 (tau or neutrino)
00364 
00365      if neutral higgs    : pol1 = +1, pol2 = -1 OR pol1 = -1, pol2 = +1 (tau tau)
00366      if Z or undetermined: pol1 = +1, pol2 = +1 OR pol1 = -1, pol2 = -1 (tau tau)
00367   */
00368 
00369   if(pdg_id < 0) { // tau+
00370     if(forceTauPlusHelicity_ != 0) {
00371       return forceTauPlusHelicity_;
00372     }
00373     if(forceTauPolarization_ == "W") return -1;
00374     if(forceTauPolarization_ == "H+") return 1;
00375     if(pol2_[2] != 0) {
00376       if(forceTauPolarization_ == "h" ||
00377          forceTauPolarization_ == "H" ||
00378          forceTauPolarization_ == "A") return -pol2_[2];
00379       else return pol2_[2];
00380     }
00381     else {
00382       return randomPolarization(); //all other cases random, when first tau
00383     }
00384   }
00385   else {           // tau-
00386     if(forceTauMinusHelicity_ != 0) {
00387       return forceTauMinusHelicity_;
00388     }
00389     if(forceTauPolarization_ == "W") return -1;
00390     if(forceTauPolarization_ == "H+") return 1;
00391     if(pol1_[2] != 0){
00392       if(forceTauPolarization_ == "h" ||
00393          forceTauPolarization_ == "H" ||
00394          forceTauPolarization_ == "A") return -pol1_[2];
00395       else return pol1_[2];
00396     }
00397     else {
00398       return randomPolarization(); //all other cases random, when first tau
00399     }
00400   }
00401 
00402   edm::LogError("MuonReplacement") << "[ParticleReplacerAlgoParticleGun::tauHelicity] "
00403                                    << "tauHelicity undetermined, returning 0" << std::endl;
00404   return 0;
00405 }
00406 
00407 float ParticleReplacerParticleGun::randomPolarization() {
00408   uint32_t randomNumber = rand();
00409   if(randomNumber%2 > 0.5) return 1; 
00410   return -1;
00411 }