00001
00005 #include "IOMC/NtupleConverter/interface/Ntuple2HepMCFiller.h"
00006
00007 #include<iostream>
00008 #include "IOMC/NtupleConverter/interface/NtupleROOTFile.h"
00009 using namespace std;
00010 using namespace HepMC;
00011
00012 Ntuple2HepMCFiller * Ntuple2HepMCFiller::instance_=0;
00013
00014 Ntuple2HepMCFiller * Ntuple2HepMCFiller::instance(){
00015
00016 if (instance_== 0) {
00017 instance_ = new Ntuple2HepMCFiller();
00018 }
00019 return instance_;
00020 }
00021
00022
00023 Ntuple2HepMCFiller::Ntuple2HepMCFiller():
00024 initialized_(false), input_(0),
00025 index_to_particle(3996),evtid(1),ntpl_id(1) {
00026 cout << "Constructing a new Ntuple2HepMCFiller" << endl;
00027 if(instance_ == 0) instance_ = this;
00028 }
00029
00030
00031 Ntuple2HepMCFiller::~Ntuple2HepMCFiller(){
00032 cout << "Destructing Ntuple2HepMCFiller" << endl;
00033 if(input_) {
00034 delete input_;
00035 }
00036 instance_=0;
00037 }
00038
00039
00040 void Ntuple2HepMCFiller::setInitialized(bool value){initialized_=value;}
00041
00042
00043 void Ntuple2HepMCFiller::initialize(const string & filename, int id){
00044
00045 if (initialized_) {
00046 cout << "Ntuple2HepMCFiller was already initialized... reinitializing it " << endl;
00047 if(input_) {
00048 delete input_;
00049 }
00050 }
00051
00052 cout<<"Ntuple2HepMCFiller::initialize : Opening file "<<filename<<endl;
00053 ntpl_id=id;
00054 input_ = new NtupleROOTFile( filename, id);
00055 initialized_ = true;
00056 }
00057
00058
00059
00060 bool Ntuple2HepMCFiller::isInitialized(){
00061
00062 return initialized_;
00063 }
00064
00065
00066 bool Ntuple2HepMCFiller::readCurrentEvent() {
00067 bool filter=false;
00068
00069 HepMC::GenEvent* event = new HepMC::GenEvent();
00070 input_ ->setEvent(evtid);
00071
00072 if ( this->toGenEvent( evtid, event ) ) evt=event;
00073 if (evt){
00074 cout <<"| --- Ntuple2HepMCFiller: Event Nr. " <<evt->event_number() <<" with " <<evt->particles_size()<<" particles --- !" <<endl;
00075
00076 filter=true;
00077 }
00078 if (!evt) {
00079 cout << "Ntuple2HepMCFiller: Got no event :-(" <<endl;
00080 filter=false;
00081 delete evt;
00082 }
00083 if (evtid > input_->getNevhep()) filter = false;
00084 evtid++;
00085 return filter;
00086 }
00087
00088
00089 bool Ntuple2HepMCFiller::setEvent(unsigned int event){
00090 evtid= event;
00091 return true;
00092 }
00093
00094
00095
00096 bool Ntuple2HepMCFiller::printHepMcEvent() const{
00097 if (evt!=0) evt->print();
00098 return true;
00099 }
00100
00101
00102 HepMC::GenEvent * Ntuple2HepMCFiller::fillCurrentEventData(){
00103 if (readCurrentEvent())return evt;
00104 else return NULL;
00105
00106 }
00107
00108
00109
00110 bool Ntuple2HepMCFiller::toGenEvent( int evtnum, HepMC::GenEvent* evt ){
00111
00112
00113
00114 evt->set_event_number( evtnum ) ;
00115
00116
00117
00118
00119 std::vector<HepMC::GenParticle*> hepevt_particle(input_->getNhep()+1 );
00120
00121
00122
00123
00124 hepevt_particle[0] = 0;
00125 for ( int i1 = 1; i1 <= input_->getNhep(); ++i1 ) {
00126 hepevt_particle[i1] = createParticle(i1);
00127 }
00128 std::set<HepMC::GenVertex*> new_vertices;
00129
00130
00131 for ( int i = 1; i <= input_->getNhep(); ++i ) {
00132
00133
00134
00136 buildProductionVertex( i, hepevt_particle, evt, 1 );
00137 }
00138
00139 return true;
00140 }
00141
00142
00143 HepMC::GenParticle* Ntuple2HepMCFiller::createParticle( int index ) {
00144
00145
00146 HepMC::GenParticle* p
00147 = new HepMC::GenParticle(FourVector( input_->getPhep(index,0),
00148 input_->getPhep(index,1),
00149 input_->getPhep(index,2),
00150 input_->getPhep(index,3)),
00151 input_->getIdhep(index),
00152 input_->getIsthep(index));
00153 p->setGeneratedMass( input_->getPhep(index, 4) );
00154 p->suggest_barcode( index );
00155 return p;
00156 }
00157
00158
00159 void Ntuple2HepMCFiller::buildProductionVertex( int i,
00160 std::vector<HepMC::GenParticle*>& hepevt_particle,
00161 HepMC::GenEvent* evt, bool printInconsistencyErrors )
00162 {
00163
00164
00165
00166 HepMC::GenParticle* p = hepevt_particle[i];
00167
00168 int mother = input_->getJmohep(i,0);
00169
00170
00171 HepMC::GenVertex* prod_vtx = p->production_vertex();
00172
00173 while ( !prod_vtx && mother > 0 ) {
00174 prod_vtx = hepevt_particle[mother]->end_vertex();
00175 if ( prod_vtx ) prod_vtx->add_particle_out( p );
00176
00177 if ( ++mother > input_->getJmohep(i,1) ) mother = 0;
00178 }
00179
00180
00181
00182 FourVector prod_pos( input_->getVhep(i,0), input_->getVhep(i,1),
00183 input_->getVhep(i,2), input_->getVhep(i,3));
00184
00185 if ( !prod_vtx && (number_parents(i)>0 || prod_pos!=FourVector(0,0,0,0) )){
00186 prod_vtx = new HepMC::GenVertex();
00187 prod_vtx->add_particle_out( p );
00188 evt->add_vertex( prod_vtx );
00189 }
00190
00191 if ( prod_vtx && prod_vtx->position()==FourVector(0,0,0,0) ) {
00192 prod_vtx->set_position( prod_pos );
00193 }
00194
00195
00196 mother = input_->getJmohep(i,0);
00197 while ( prod_vtx && mother > 0 ) {
00198 if ( !hepevt_particle[mother]->end_vertex() ) {
00199
00200 prod_vtx->add_particle_in( hepevt_particle[mother] );
00201 }
00202 else if (hepevt_particle[mother]->end_vertex() != prod_vtx ) {
00203
00204
00205
00206
00207
00208
00209
00210
00211 if ( printInconsistencyErrors ) std::cerr
00212 << "Ntuple2HepMCFiller:: inconsistent mother/daughter "
00213 << "information in HEPEVT event "
00214 << input_->getNevhep()
00215 << std::endl;
00216 }
00217 if ( ++mother > input_->getJmohep(i,1) ) mother = 0;
00218 }
00219 }
00220
00221
00222
00223 int Ntuple2HepMCFiller::number_children( int index )
00224 {
00225 int firstchild = input_->getJdahep(index,0);
00226 return ( firstchild>0 ) ?
00227 ( 1+input_->getJdahep(index,1)-firstchild ) : 0;
00228 }
00229
00230
00231 int Ntuple2HepMCFiller::number_parents( int index ) {
00232
00233
00234 if (input_->getNhep()==1) return 1;
00235
00236 if (input_->getNhep()==2) return 1;
00237 int firstparent = input_->getJmohep(index,0);
00238 if( firstparent <= 0 ) return 0;
00239 int secondparent = input_->getJmohep(index,1);
00240 if( secondparent <= 0 ) return 1;
00241 return secondparent - firstparent + 1;
00242
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272