CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Ntuple2HepMCFiller.cc
Go to the documentation of this file.
1 
6 
7 #include<iostream>
9 using namespace std;
10 using namespace HepMC;
11 
13 //-------------------------------------------------------------------------
15 
16  if (instance_== 0) {
17  instance_ = new Ntuple2HepMCFiller();
18  }
19  return instance_;
20 }
21 
22 
24  initialized_(false), input_(0),
25  index_to_particle(3996),evtid(1),ntpl_id(1) {
26  cout << "Constructing a new Ntuple2HepMCFiller" << endl;
27  if(instance_ == 0) instance_ = this;
28 }
29 
30 //-------------------------------------------------------------------------
32  cout << "Destructing Ntuple2HepMCFiller" << endl;
33  if(input_) {
34  delete input_;
35  }
36  instance_=0;
37 }
38 //-------------------------------------------------------------------------
39 
41 
42 //-------------------------------------------------------------------------
43 void Ntuple2HepMCFiller::initialize(const string & filename, int id){
44 
45  if (initialized_) {
46  cout << "Ntuple2HepMCFiller was already initialized... reinitializing it " << endl;
47  if(input_) {
48  delete input_;
49  }
50  }
51 
52  cout<<"Ntuple2HepMCFiller::initialize : Opening file "<<filename<<endl;
53  ntpl_id=id;
54  input_ = new NtupleROOTFile( filename, id);
55  initialized_ = true;
56 }
57 
58 //-------------------------------------------------------------------------
59 
61 
62  return initialized_;
63 }
64 
65 //-------------------------------------------------------------------------
67  bool filter=false;
68  // 1. create an empty event container
69  HepMC::GenEvent* event = new HepMC::GenEvent();
71  // 2. fill the evt container - if the read is successful, set the pointer
72  if ( this->toGenEvent( evtid, event ) ) evt=event;
73  if (evt){
74  cout <<"| --- Ntuple2HepMCFiller: Event Nr. " <<evt->event_number() <<" with " <<evt->particles_size()<<" particles --- !" <<endl;
75  // printHepMcEvent();
76  filter=true;
77  }
78  if (!evt) {
79  cout << "Ntuple2HepMCFiller: Got no event :-(" <<endl;
80  filter=false;
81  delete evt; // @@@
82  }
83  if (evtid > input_->getNevhep()) filter = false;
84  evtid++;
85  return filter;
86 }
87 
88 //-------------------------------------------------------------------------
90  evtid= event;
91  return true;
92 }
93 
94 //-------------------------------------------------------------------------
95 
97  if (evt!=0) evt->print();
98  return true;
99 }
100 
101 //-------------------------------------------------------------------------
103  if (readCurrentEvent())return evt;
104  else return NULL;
105 
106 }
107 
108 
109 //-------------------------------------------------------------------
110 bool Ntuple2HepMCFiller::toGenEvent( int evtnum, HepMC::GenEvent* evt ){
111  // Written according to HepMC /fio/ IO_HEPEVT.cc( hepmc-01-26 tag at Savannah)
112  // 1. set event number
113  // evt->set_event_number( input_->getNevhep());
114  evt->set_event_number( evtnum ) ;
115  // these do not have the correct defaults if hepev4 is not filled
116  //evt->set_event_scale( hepev4()->scalelh[1] );
117  //evt->set_alphaQCD( hepev4()->alphaqcdlh );
118  //evt->set_alphaQED( hepev4()->alphaqedlh );
119  std::vector<HepMC::GenParticle*> hepevt_particle(input_->getNhep()+1 );
120  //2. create a particle instance for each HEPEVT entry and fill a map
121  // create a vector which maps from the HEPEVT particle index to the
122  // GenParticle address
123  // (+1 in size accounts for hepevt_particle[0] which is unfilled)
124  hepevt_particle[0] = 0;
125  for ( int i1 = 1; i1 <= input_->getNhep(); ++i1 ) {
126  hepevt_particle[i1] = createParticle(i1);
127  }
128  std::set<HepMC::GenVertex*> new_vertices;
129 
130  // 3.+4. loop over HEPEVT particles AGAIN, this time creating vertices
131  for ( int i = 1; i <= input_->getNhep(); ++i ) {
132  // We go through and build EITHER the production or decay
133  // vertex for each entry in hepevt
134  // Note: since the HEPEVT pointers are bi-directional, it is
136  buildProductionVertex( i, hepevt_particle, evt, 1 );
137  }
138  // hepevt_particle.clear();
139  return true;
140 }
141 
142 //-------------------------------------------------------
144 
145  // Builds a particle object corresponding to index in HEPEVT
147  = new HepMC::GenParticle(FourVector( input_->getPhep(index,0),
148  input_->getPhep(index,1),
149  input_->getPhep(index,2),
150  input_->getPhep(index,3)),
151  input_->getIdhep(index),
152  input_->getIsthep(index));
153  p->setGeneratedMass( input_->getPhep(index, 4) );
154  p->suggest_barcode( index );
155  return p;
156 }
157 
158 //-------------------------------------------------------
160 std::vector<HepMC::GenParticle*>& hepevt_particle,
161 HepMC::GenEvent* evt, bool printInconsistencyErrors )
162 {
163  //
164  // for particle in HEPEVT with index i, build a production vertex
165  // if appropriate, and add that vertex to the event
166  HepMC::GenParticle* p = hepevt_particle[i];
167  // a. search to see if a production vertex already exists
168  int mother = input_->getJmohep(i,0);
169  // this is dangerous. Copying null pointer and attempting to fill
170  // information in prod_vtx......
171  HepMC::GenVertex* prod_vtx = p->production_vertex();
172  // no production vertex and mother exist -> produce vertex
173  while ( !prod_vtx && mother > 0 ) {
174  prod_vtx = hepevt_particle[mother]->end_vertex();
175  if ( prod_vtx ) prod_vtx->add_particle_out( p );
176  // increment mother for next iteration
177  if ( ++mother > input_->getJmohep(i,1) ) mother = 0;
178  }
179  // b. if no suitable production vertex exists - and the particle
180  // has atleast one mother or position information to store -
181  // make one@@@
182  FourVector prod_pos( input_->getVhep(i,0), input_->getVhep(i,1),
183  input_->getVhep(i,2), input_->getVhep(i,3));
184  // orginal:
185  if ( !prod_vtx && (number_parents(i)>0 || prod_pos!=FourVector(0,0,0,0) )){
186  prod_vtx = new HepMC::GenVertex();
187  prod_vtx->add_particle_out( p );
188  evt->add_vertex( prod_vtx );
189  }
190  // c. if prod_vtx doesn't already have position specified, fill it
191  if ( prod_vtx && prod_vtx->position()==FourVector(0,0,0,0) ) {
192  prod_vtx->set_position( prod_pos );
193  }
194  // d. loop over mothers to make sure their end_vertices are
195  // consistent
196  mother = input_->getJmohep(i,0);
197  while ( prod_vtx && mother > 0 ) {
198  if ( !hepevt_particle[mother]->end_vertex() ) {
199  // if end vertex of the mother isn't specified, do it now
200  prod_vtx->add_particle_in( hepevt_particle[mother] );
201  }
202  else if (hepevt_particle[mother]->end_vertex() != prod_vtx ) {
203  // problem scenario --- the mother already has a decay
204  // vertex which differs from the daughter's produciton
205  // vertex. This means there is internal
206  // inconsistency in the HEPEVT event record. Print an
207  // error
208  // Note: we could provide a fix by joining the two
209  // vertices with a dummy particle if the problem
210  // arrises often with any particular generator.
211  if ( printInconsistencyErrors ) std::cerr
212  << "Ntuple2HepMCFiller:: inconsistent mother/daughter "
213  << "information in HEPEVT event "
214  << input_->getNevhep()
215  << std::endl;
216  }
217  if ( ++mother > input_->getJmohep(i,1) ) mother = 0;
218  }
219 }
220 
221 
222 //-------------------------------------------------------
224 {
225  int firstchild = input_->getJdahep(index,0);
226  return ( firstchild>0 ) ?
227  ( 1+input_->getJdahep(index,1)-firstchild ) : 0;
228 }
229 
230 //-------------------------------------------------------
232 
233  // Fix for cmkin single particle ntpls
234  if (input_->getNhep()==1) return 1;
235  // Fix for cmkindouble particle ntpls
236  if (input_->getNhep()==2) return 1;
237  int firstparent = input_->getJmohep(index,0);
238  if( firstparent <= 0 ) return 0;
239  int secondparent = input_->getJmohep(index,1);
240  if( secondparent <= 0 ) return 1;
241  return secondparent - firstparent + 1;
242 
243 }
244 
245 /*************************************************************************************
246 NEVHEP = event number
247 NHEP = number of entries (particles, partons)
248 ISTHEP = status code
249 IDHEP = PDG identifier
250 JMOHEP = position of 1st and 2nd mother
251 JDAHEP = position of 1st and last daughter
252 PHEP = 4-momentum and mass (single precision in ntuple file)
253 VHEP = vertex xyz and production time (single precision in ntuple file)
254 
255 
256 IRNMCP = run number
257 IEVMCP = event number
258 WGTMCP = event weight
259 XSECN = cross section equivalent
260 IFILTER = filter pattern
261 NVRMCP = number of additional variables
262 VARMCP(NMXMCP) = list of additional variables
263 Note: VARMCP(1) = PARI(17) (=s_hat) is reserved.
264 
265 There are the following default values:
266 
267 IRNMCP = 1
268 IEVMCP = NEVHEP
269 WGTMCP = 1.0
270 XSECN = IFILTER = 0
271 NVRMCP = 1
272 *****************************************************************************************/
HepMC::GenEvent * fillCurrentEventData()
NtupleROOTFile * input_
int i
Definition: DBlmapReader.cc:9
virtual int getNevhep() const
virtual ~Ntuple2HepMCFiller()
Destructor.
HepMC::GenEvent * evt
Ntuple2HepMCFiller()
Constructor.
virtual bool readCurrentEvent()
#define NULL
Definition: scimark2.h:8
static Ntuple2HepMCFiller * instance_
static Ntuple2HepMCFiller * instance()
virtual int getJdahep(int j, int idx) const
void buildProductionVertex(int i, std::vector< HepMC::GenParticle * > &hepevt_particle, HepMC::GenEvent *evt, bool printInconsistencyErrors)
virtual double getVhep(int j, int idx) const
int number_parents(int index)
HepMC::GenParticle * createParticle(int index)
virtual int getIsthep(int j) const
virtual bool toGenEvent(int evtnum, HepMC::GenEvent *evt)
virtual bool setEvent(unsigned int event)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
virtual void setEvent(int event) const
virtual int getNhep() const
virtual bool printHepMcEvent() const
virtual int getIdhep(int j) const
virtual double getPhep(int j, int idx) const
tuple filename
Definition: lut2db_cfg.py:20
int number_children(int index)
virtual int getJmohep(int j, int idx) const
tuple cout
Definition: gather_cfg.py:121
virtual void initialize(const std::string &filename, int id)
void setInitialized(bool value)