File indexing completed on 2025-02-23 09:21:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 #include "MCTruthManager.hh"
0043
0044
0045
0046 static MCTruthManager* instance = 0;
0047
0048
0049
0050 MCTruthManager::MCTruthManager() : fEvent(0), fConfig(0) {}
0051
0052
0053
0054 MCTruthManager::~MCTruthManager() {}
0055
0056
0057
0058 MCTruthManager* MCTruthManager::GetInstance()
0059 {
0060 if (instance == 0) {
0061 instance = new MCTruthManager();
0062 }
0063 return instance;
0064 }
0065
0066
0067
0068 void MCTruthManager::NewEvent()
0069 {
0070
0071 delete fEvent;
0072
0073 fEvent = new HepMC::GenEvent();
0074 }
0075
0076
0077
0078 void MCTruthManager::AddParticle(G4LorentzVector& momentum, G4LorentzVector& prodpos,
0079 G4LorentzVector& endpos, G4int pdg_id, G4int partID,
0080 G4int motherID, G4bool directParent)
0081 {
0082
0083 HepMC::GenParticle* particle = new HepMC::GenParticle(momentum, pdg_id);
0084 particle->suggest_barcode(partID);
0085
0086
0087 fSegmentations[partID] = 1;
0088
0089
0090 HepMC::GenVertex* endvertex = new HepMC::GenVertex(endpos);
0091
0092
0093 endvertex->suggest_barcode(-partID);
0094 endvertex->add_particle_in(particle);
0095 fEvent->add_vertex(endvertex);
0096
0097 if (motherID)
0098 {
0099
0100
0101 HepMC::GenParticle* mother = fEvent->barcode_to_particle(motherID);
0102
0103 if (mother) {
0104
0105
0106 HepMC::GenVertex* motherendvtx = mother->end_vertex();
0107 HepMC::FourVector mp0 = motherendvtx->position();
0108 G4LorentzVector motherendpos(mp0.x(), mp0.y(), mp0.z(), mp0.t());
0109
0110 if (motherendpos.x() == prodpos.x() && motherendpos.y() == prodpos.y()
0111 && motherendpos.z() == prodpos.z())
0112 {
0113 motherendvtx->add_particle_out(particle);
0114 }
0115 else
0116 {
0117 if (!directParent)
0118 {
0119 G4bool found = false;
0120
0121
0122
0123
0124 for (HepMC::GenVertex::particles_out_const_iterator it =
0125 motherendvtx->particles_out_const_begin();
0126 it != motherendvtx->particles_out_const_end(); it++)
0127 {
0128 if ((*it)->pdg_id() == -999999) {
0129 HepMC::FourVector dp0 = (*it)->end_vertex()->position();
0130 G4LorentzVector dummypos(dp0.x(), dp0.y(), dp0.z(), dp0.t());
0131 ;
0132
0133 if (dummypos.x() == prodpos.x() && dummypos.y() == prodpos.y()
0134 && dummypos.z() == prodpos.z())
0135 {
0136 (*it)->end_vertex()->add_particle_out(particle);
0137 found = true;
0138 break;
0139 }
0140 }
0141 }
0142
0143
0144
0145
0146 if (!found) {
0147 HepMC::GenVertex* childvtx = new HepMC::GenVertex(prodpos);
0148 childvtx->add_particle_out(particle);
0149
0150
0151
0152
0153 childvtx->suggest_barcode(-500000 - partID);
0154 fEvent->add_vertex(childvtx);
0155
0156 HepMC::GenParticle* dummypart = new HepMC::GenParticle(G4LorentzVector(), -999999);
0157
0158
0159
0160
0161 dummypart->suggest_barcode(500000 + partID);
0162 childvtx->add_particle_in(dummypart);
0163 motherendvtx->add_particle_out(dummypart);
0164 }
0165 }
0166 else
0167 {
0168
0169
0170
0171
0172 G4int number_of_segments = fSegmentations[motherID];
0173 G4int segment = 0;
0174
0175
0176
0177 while (!((mother->end_vertex()->position().t() > prodpos.t())
0178 && (mother->production_vertex()->position().t() < prodpos.t())))
0179 {
0180 segment++;
0181 if (segment == number_of_segments)
0182 G4cerr << "Problem!!!! Time coordinates incompatible!" << G4endl;
0183
0184 mother = fEvent->barcode_to_particle(segment * 10000000 + motherID);
0185 }
0186
0187
0188
0189
0190 HepMC::GenVertex* childvtx = new HepMC::GenVertex(prodpos);
0191 childvtx->add_particle_out(particle);
0192 fEvent->add_vertex(childvtx);
0193
0194
0195
0196 HepMC::GenVertex* orig_mother_end_vtx = mother->end_vertex();
0197 orig_mother_end_vtx->remove_particle(mother);
0198
0199
0200
0201 childvtx->add_particle_in(mother);
0202
0203
0204
0205
0206
0207 HepMC::GenParticle* mothertwo = new HepMC::GenParticle(*mother);
0208 mothertwo->suggest_barcode(fSegmentations[motherID] * 10000000 + mother->barcode());
0209
0210
0211
0212 orig_mother_end_vtx->suggest_barcode(-fSegmentations[motherID] * 10000000
0213 - mother->barcode());
0214 childvtx->suggest_barcode(-mother->barcode());
0215
0216
0217
0218 childvtx->add_particle_out(mothertwo);
0219
0220
0221
0222 orig_mother_end_vtx->add_particle_in(mothertwo);
0223
0224
0225
0226 fSegmentations[motherID] = fSegmentations[motherID] + 1;
0227 }
0228 }
0229 }
0230 else
0231
0232
0233
0234
0235 {
0236 G4cerr << "barcode " << motherID << " mother not there! " << G4endl;
0237 }
0238 }
0239 else
0240 {
0241 HepMC::GenVertex* primaryvtx = new HepMC::GenVertex(prodpos);
0242 primaryvtx->add_particle_out(particle);
0243 fEvent->add_vertex(primaryvtx);
0244
0245
0246
0247 fPrimarybarcodes.push_back(partID);
0248 }
0249 }
0250
0251
0252
0253 void MCTruthManager::PrintEvent()
0254 {
0255 fEvent->print();
0256
0257
0258
0259 for (std::vector<int>::const_iterator primarybar = fPrimarybarcodes.begin();
0260 primarybar != fPrimarybarcodes.end(); primarybar++)
0261 {
0262 PrintTree(fEvent->barcode_to_particle(*primarybar), " | ");
0263 }
0264 }
0265
0266
0267
0268 void MCTruthManager::PrintTree(HepMC::GenParticle* particle, G4String offset)
0269 {
0270 G4cout << offset << "--- barcode: " << particle->barcode() << " pdg: " << particle->pdg_id()
0271 << " energy: " << particle->momentum().e()
0272 << " production vertex: " << particle->production_vertex()->position().x() << ", "
0273 << particle->production_vertex()->position().y() << ", "
0274 << particle->production_vertex()->position().z() << ", "
0275 << particle->production_vertex()->position().t() << G4endl;
0276
0277 for (HepMC::GenVertex::particles_out_const_iterator it =
0278 particle->end_vertex()->particles_out_const_begin();
0279 it != particle->end_vertex()->particles_out_const_end(); it++)
0280 {
0281 G4String deltaoffset = "";
0282
0283 G4int curr = std::fmod(double((*it)->barcode()), 10000000.);
0284 G4int part = std::fmod(double(particle->barcode()), 10000000.);
0285 if (curr != part) {
0286 deltaoffset = " | ";
0287 }
0288
0289 PrintTree((*it), offset + deltaoffset);
0290 }
0291 }
0292
0293