CGEM BOSS 6.6.5.i
BESIII Offline Software System
Loading...
Searching...
No Matches
ManualGenerator.cxx
Go to the documentation of this file.
1// *****************************************************************************
2//
3// ManualGenerator.cxx
4//
5// A package to read four-vectors from an input file and send them through
6// the BES detector simulation.
7//
8// Notes:
9//
10// * To use, replace EvtGen in the job options file with:
11//
12// #include "$MANUALGENERATORROOT/share/ManualGenerator.txt"
13// ManualGenerator.InputFile = "inputfile.txt";
14//
15// where "inputfile.txt" is a text file containing four-vectors in
16// the following format for each event:
17//
18// [number of final state particles in this event]
19// [PDG ID of particle 1] [P_x] [P_y] [P_z] [E]
20// [PDG ID of particle 2] [P_x] [P_y] [P_z] [E]
21// and so on.
22//
23// * This package deals only with final state particles. The production
24// of the initial state particle (for example, the J/psi) must be handled
25// by a prior package (for example, KKMC).
26//
27// * Events should be generated with zero net 3-momentum. Particles are
28// then internally boosted to account for the crossing angle, beam energy
29// spread, etc., as reflected by the four-momentum of the initial state
30// particle.
31//
32// * Events should be generated at the origin. Particles are then offset
33// according to the production vertex of the initial state particle.
34//
35// * pi0 and Ks decays are handled by GEANT.
36// (Note that B(pi0 --> gamma gamma) is not 100%.)
37//
38//
39// September 2011 R. Mitchell
40//
41// --2012-8-10, zhao qingzhang and ping ronggang
42// Boost from CM system to Lab system has been implement out of generator, so
43// remove the boost
44// *****************************************************************************
45
47
48#include "GaudiKernel/MsgStream.h"
49#include "GaudiKernel/ISvcLocator.h"
50#include "GaudiKernel/AlgFactory.h"
51#include "GaudiKernel/DataSvc.h"
52#include "GaudiKernel/SmartDataPtr.h"
53
55
56#include <stdlib.h>
57
58
59// *****************************************************************************
60//
61// CONSTRUCTOR
62//
63// *****************************************************************************
64
65ManualGenerator::ManualGenerator(const string& name, ISvcLocator* pSvcLocator):Algorithm( name, pSvcLocator ){
66
67
68 // get the name of the input file from the job options file
69
70 declareProperty("InputFile", m_inputFileName = "");
71
72}
73
74
75
76// *****************************************************************************
77//
78// INITIALIZE
79//
80// *****************************************************************************
81
82
84 //MsgStream log(messageService(), name());
85 //log << MSG::INFO << "ManualGenerator initialize" << endreq;
86
87
88 // open the input file
89
90 m_inputFile.open(m_inputFileName.c_str());
91 if (!m_inputFile){
92 cout << "ManualGenerator: PROBLEMS OPENING FILE "
93 << m_inputFileName << endl;
94 exit(0);
95 }
96
97
98 // zero the event counter
99
100 m_eventCounter = 0;
101
102
103 return StatusCode::SUCCESS;
104
105}
106
107
108
109// *****************************************************************************
110//
111// EXECUTE
112//
113// *****************************************************************************
114
115
117
118
119 // read an event from the input file
120
121 int nParticles;
122 int idParticles[100];
123 double pxParticle;
124 double pyParticle;
125 double pzParticle;
126 double eParticle;
127 CLHEP::HepLorentzVector pParticles[100];
128
129 if (m_inputFile >> nParticles){
130 for (int i = 0; i < nParticles; i++){
131 m_inputFile >> idParticles[i];
132 m_inputFile >> pxParticle; pParticles[i].setPx(pxParticle);
133 m_inputFile >> pyParticle; pParticles[i].setPy(pyParticle);
134 m_inputFile >> pzParticle; pParticles[i].setPz(pzParticle);
135 m_inputFile >> eParticle; pParticles[i].setE(eParticle);
136 }
137 }
138
139 else{
140 cout << "ManualGenerator: RAN OUT OF INPUT EVENTS FROM FILE "
141 << m_inputFileName << endl;
142 return StatusCode::SUCCESS;
143 }
144
145
146
147 // print out a message for this event to make sure all is well
148
149 cout << "ManualGenerator: PROCESSING EVENT " << ++m_eventCounter << endl;
150 cout << nParticles << endl;
151 for (int i = 0; i < nParticles; i++){
152 cout << idParticles[i] << "\t";
153 cout << pParticles[i].px() << "\t";
154 cout << pParticles[i].py() << "\t";
155 cout << pParticles[i].pz() << "\t";
156 cout << pParticles[i].e() << endl;
157 }
158
159
160 // retrieve the original MC event (HepMC::GenEvent) from the framework
161
162 HepMC::GenEvent* genEvent = 0;
163 SmartDataPtr<McGenEventCol> initialMcCol(eventSvc(), "/Event/Gen");
164 if (initialMcCol == 0){
165// HepMC::GenEvent* genEvent = new GenEvent();
166// genEvent->set_event_number(0);
167
168 //create a Transient store
169// McGenEventCol *mcColl = new McGenEventCol;
170// McGenEvent* mcEvent = new McGenEvent(genEvent);
171// mcColl->push_back(mcEvent);
172// StatusCode sc = eventSvc()->registerObject("/Event/Gen", mcColl);
173// if (sc != StatusCode::SUCCESS) return StatusCode::FAILURE;
174
175 cout << "ManualGenerator: COULD NOT FIND THE ORIGINAL MC EVENT COLLECTION" << endl;
176 exit(0);
177 }
178// else
179// {
180 genEvent = (*(initialMcCol->begin()))->getGenEvt();
181// }
182 //genEvent->print();
183
184
185
186 // loop over MC particles and look for the particle we want to decay
187
188 HepMC::GenParticle* decayParticle = NULL;
189 for(HepMC::GenEvent::particle_const_iterator igenParticle = genEvent->particles_begin();
190 igenParticle != genEvent->particles_end(); ++igenParticle ){
191 decayParticle = *igenParticle;
192 if (decayParticle->status() == 1 && abs(decayParticle->pdg_id()) != 22) break;
193 }
194 if (!decayParticle){
195 cout << "ManualGenerator: COULD NOT FIND A PARTICLE TO DECAY" << endl;
196 exit(0);
197 }
198
199
200 // create a new vertex from the decaying particle's production vertex
201 // and add it to the event
202
203 HepMC::GenVertex* initialVertex = decayParticle->production_vertex();
204 HepMC::GenVertex* newVertex = new HepMC::GenVertex(initialVertex->position());
205 genEvent->add_vertex(newVertex);
206 newVertex->add_particle_in(decayParticle);
207
208
209
210 // boost the input four-vectors into the decaying particle's rest frame
211
212 CLHEP::HepLorentzVector initialFourMomentum(decayParticle->momentum().px(),
213 decayParticle->momentum().py(),
214 decayParticle->momentum().pz(),
215 decayParticle->momentum().e());
216 CLHEP::HepLorentzVector finalFourMomentum(0,0,0,0);
217 for (int i = 0; i < nParticles; i++){
218 // pParticles[i].boost(initialFourMomentum.boostVector());
219 finalFourMomentum += pParticles[i];
220 }
221 if ((fabs(initialFourMomentum.px() - finalFourMomentum.px()) > 0.0001) ||
222 (fabs(initialFourMomentum.py() - finalFourMomentum.py()) > 0.0001) ||
223 (fabs(initialFourMomentum.pz() - finalFourMomentum.pz()) > 0.0001) ||
224 (fabs(initialFourMomentum.e() - finalFourMomentum.e()) > 0.01)){
225 cout << "ManualGenerator: INITIAL AND FINAL STATES HAVE INCONSISTENT 4-MOMENTA" << endl;
226 cout << "delta px = " << initialFourMomentum.px() - finalFourMomentum.px() << endl;
227 cout << "delta py = " << initialFourMomentum.py() - finalFourMomentum.py() << endl;
228 cout << "delta pz = " << initialFourMomentum.pz() - finalFourMomentum.pz() << endl;
229 cout << "delta e = " << initialFourMomentum.e() - finalFourMomentum.e() << endl;
230 // exit(0);
231 }
232
233
234 // add the input four-vectors to the new vertex
235
236 for (int i = 0; i < nParticles; i++){
237 newVertex->add_particle_out(new GenParticle(pParticles[i], idParticles[i], 1));
238 }
239 //genEvent->print();
240
241
242
243 return StatusCode::SUCCESS;
244}
245
246
247
248
249// *****************************************************************************
250//
251// FINALIZE
252//
253// *****************************************************************************
254
255
257{
258 m_inputFile.close();
259 return StatusCode::SUCCESS;
260}
StatusCode initialize()
ManualGenerator(const string &name, ISvcLocator *pSvcLocator)
StatusCode finalize()