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
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
00023 srand(time(NULL));
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
00056
00057
00058 }
00059 }
00060
00061 void ParticleReplacerParticleGun::endJob() {
00062 if(abs(gunParticle_) == 15) {
00063
00064
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
00086
00087 if(printout_) {
00088 std::cout << " ///////////////////// After py1ent, before pyhepc /////////////////////" << std::endl;
00089 call_pylist(3);
00090 }
00091
00092
00093 call_pyhepc(1);
00094
00095 if(printout_) {
00096 std::cout << " ///////////////////// After pyhepc, before vertex shift /////////////////////" << std::endl;
00097 HepMC::HEPEVT_Wrapper::print_hepevt();
00098 }
00099
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);
00107 shift.setY(pvtx->y()*10);
00108 shift.setZ(pvtx->z()*10);
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);
00120 shift.setY(muon.vy()*10);
00121 shift.setZ(muon.vz()*10);
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
00138
00139
00140
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);
00154 if(printout_) {
00155 std::cout << " ///////////////////// After pyhepc, before vertex fix /////////////////////" << std::endl;
00156 call_pylist(3);
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 if(printout_) {
00166
00167
00168
00169
00170 std::cout << " ///////////////////// After vertex fix, before pyexec /////////////////////" << std::endl;
00171 call_pylist(3);
00172 }
00173 }
00174 else {
00175 call_pyhepc(2);
00176 if(printout_) {
00177 std::cout << " ///////////////////// After pyhepc, before pyexec /////////////////////" << std::endl;
00178 call_pylist(3);
00179 }
00180 }
00181
00182 call_pyexec();
00183
00184 if(printout_) {
00185 std::cout << " ///////////////////// After pyexec, before pyhepc /////////////////////" << std::endl;
00186 call_pylist(3);
00187 }
00188
00189 call_pyhepc(1);
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
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
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
00243
00244
00245 if(forceTauDecay_ == "hadrons"){
00246
00247
00248
00249
00250 }
00251 else if(forceTauDecay_ == "1prong"){
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 }
00264 else if(forceTauDecay_ == "3prong"){
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
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
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 if(pdg_id == -15){
00311
00312 pol1_[2] = tauHelicity(pdg_id);
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 }
00334 else if (pdg_id == 15){
00335
00336 pol2_[2] = tauHelicity(pdg_id);
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 }
00358 }
00359
00360 float ParticleReplacerParticleGun::tauHelicity(int pdg_id) {
00361
00362
00363
00364
00365
00366
00367
00368
00369 if(pdg_id < 0) {
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();
00383 }
00384 }
00385 else {
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();
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 }