Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 /// \file eventgenerator/HepMC/MCTruth/src/MCT 26 /// \file eventgenerator/HepMC/MCTruth/src/MCTruthManager.cc 27 /// \brief Implementation of the MCTruthManage 27 /// \brief Implementation of the MCTruthManager class 28 // 28 // 29 // 29 // 30 // 30 // 31 // 31 // 32 // ------------------------------------------- 32 // -------------------------------------------------------------- 33 // GEANT 4 - MCTruthManager class 33 // GEANT 4 - MCTruthManager class 34 // ------------------------------------------- 34 // -------------------------------------------------------------- 35 // 35 // 36 // Author: Witold POKORSKI (Witold.Pokorski@ce 36 // Author: Witold POKORSKI (Witold.Pokorski@cern.ch) 37 // 37 // 38 // ------------------------------------------- 38 // -------------------------------------------------------------- 39 // 39 // 40 //....oooOO0OOooo........oooOO0OOooo........oo 40 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 41 41 42 #include "MCTruthManager.hh" 42 #include "MCTruthManager.hh" 43 43 44 //....oooOO0OOooo........oooOO0OOooo........oo 44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 45 45 46 static MCTruthManager* instance = 0; 46 static MCTruthManager* instance = 0; 47 47 48 //....oooOO0OOooo........oooOO0OOooo........oo 48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 49 49 50 MCTruthManager::MCTruthManager() : fEvent(0), << 50 MCTruthManager::MCTruthManager() : fEvent(0), fConfig(0) >> 51 {} 51 52 52 //....oooOO0OOooo........oooOO0OOooo........oo 53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 53 54 54 MCTruthManager::~MCTruthManager() {} << 55 MCTruthManager::~MCTruthManager() >> 56 {} 55 57 56 //....oooOO0OOooo........oooOO0OOooo........oo 58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 57 59 58 MCTruthManager* MCTruthManager::GetInstance() 60 MCTruthManager* MCTruthManager::GetInstance() 59 { 61 { 60 if (instance == 0) { << 62 if( instance == 0 ) >> 63 { 61 instance = new MCTruthManager(); 64 instance = new MCTruthManager(); 62 } 65 } 63 return instance; 66 return instance; 64 } 67 } 65 68 66 //....oooOO0OOooo........oooOO0OOooo........oo 69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 67 70 68 void MCTruthManager::NewEvent() 71 void MCTruthManager::NewEvent() 69 { 72 { 70 // first delete the old event 73 // first delete the old event 71 delete fEvent; 74 delete fEvent; 72 // and now instaciate a new one 75 // and now instaciate a new one 73 fEvent = new HepMC::GenEvent(); 76 fEvent = new HepMC::GenEvent(); 74 } 77 } 75 78 76 //....oooOO0OOooo........oooOO0OOooo........oo 79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 77 80 78 void MCTruthManager::AddParticle(G4LorentzVect << 81 void MCTruthManager::AddParticle(G4LorentzVector& momentum, 79 G4LorentzVect << 82 G4LorentzVector& prodpos, 80 G4int motherI << 83 G4LorentzVector& endpos, >> 84 G4int pdg_id, G4int partID, G4int motherID, >> 85 G4bool directParent) 81 { 86 { 82 // we create a new particle with barcode = p 87 // we create a new particle with barcode = partID 83 HepMC::GenParticle* particle = new HepMC::Ge 88 HepMC::GenParticle* particle = new HepMC::GenParticle(momentum, pdg_id); 84 particle->suggest_barcode(partID); 89 particle->suggest_barcode(partID); 85 // we initialize the 'segmentations' map 90 // we initialize the 'segmentations' map 86 // for the moment particle is not 'segmented << 91 // for the moment particle is not 'segmented' 87 fSegmentations[partID] = 1; 92 fSegmentations[partID] = 1; 88 93 89 // we create the GenVertex corresponding to 94 // we create the GenVertex corresponding to the end point of the track 90 HepMC::GenVertex* endvertex = new HepMC::Gen 95 HepMC::GenVertex* endvertex = new HepMC::GenVertex(endpos); 91 << 96 92 // barcode of the endvertex = - barcode of t 97 // barcode of the endvertex = - barcode of the track 93 endvertex->suggest_barcode(-partID); 98 endvertex->suggest_barcode(-partID); 94 endvertex->add_particle_in(particle); 99 endvertex->add_particle_in(particle); 95 fEvent->add_vertex(endvertex); 100 fEvent->add_vertex(endvertex); 96 << 101 97 if (motherID) // not a primary << 102 if(motherID) // not a primary 98 { 103 { 99 // here we could try to improve speed by s << 104 // here we could try to improve speed by searching only through particles which 100 // belong to the given primary tree 105 // belong to the given primary tree 101 HepMC::GenParticle* mother = fEvent->barco 106 HepMC::GenParticle* mother = fEvent->barcode_to_particle(motherID); 102 // 107 // 103 if (mother) { << 108 if(mother) >> 109 { 104 // we first check whether the mother's e 110 // we first check whether the mother's end vertex corresponds to the particle's 105 // production vertex 111 // production vertex 106 HepMC::GenVertex* motherendvtx = mother- 112 HepMC::GenVertex* motherendvtx = mother->end_vertex(); 107 HepMC::FourVector mp0 = motherendvtx->po 113 HepMC::FourVector mp0 = motherendvtx->position(); 108 G4LorentzVector motherendpos(mp0.x(), mp 114 G4LorentzVector motherendpos(mp0.x(), mp0.y(), mp0.z(), mp0.t()); 109 << 115 110 if (motherendpos.x() == prodpos.x() && m << 116 if( motherendpos.x() == prodpos.x() && 111 && motherendpos.z() == prodpos.z()) << 117 motherendpos.y() == prodpos.y() && >> 118 motherendpos.z() == prodpos.z() ) // if yes, we attach the particle 112 { 119 { 113 motherendvtx->add_particle_out(particl 120 motherendvtx->add_particle_out(particle); 114 } 121 } 115 else // if not, we check whether the mo << 122 else // if not, we check whether the mother is biological or adopted 116 { << 123 { 117 if (!directParent) // adopted << 124 if(!directParent) // adopted 118 { << 125 { 119 G4bool found = false; 126 G4bool found = false; 120 127 121 // first check if any of the dummy p 128 // first check if any of the dummy particles 122 // has the end vertex at the right p 129 // has the end vertex at the right place 123 // 130 // 124 for (HepMC::GenVertex::particles_out << 131 for(HepMC::GenVertex::particles_out_const_iterator 125 motherendvtx->particles_out_c << 132 it=motherendvtx->particles_out_const_begin(); 126 it != motherendvtx->particles_o << 133 it!=motherendvtx->particles_out_const_end();it++) 127 { 134 { 128 if ((*it)->pdg_id() == -999999) { << 135 if((*it)->pdg_id()==-999999) >> 136 { 129 HepMC::FourVector dp0 = (*it)->e 137 HepMC::FourVector dp0 = (*it)->end_vertex()->position(); 130 G4LorentzVector dummypos(dp0.x() << 138 G4LorentzVector dummypos(dp0.x(), dp0.y(), dp0.z(), dp0.t());; 131 ; << 139 132 << 140 if( dummypos.x() == prodpos.x() && 133 if (dummypos.x() == prodpos.x() << 141 dummypos.y() == prodpos.y() && 134 && dummypos.z() == prodpos.z << 142 dummypos.z() == prodpos.z() ) 135 { 143 { 136 (*it)->end_vertex()->add_parti 144 (*it)->end_vertex()->add_particle_out(particle); 137 found = true; 145 found = true; 138 break; 146 break; 139 } 147 } 140 } 148 } 141 } 149 } 142 150 143 // and if not, create a dummy partic 151 // and if not, create a dummy particle connecting 144 // to the end vertex of the mother 152 // to the end vertex of the mother 145 // 153 // 146 if (!found) { << 154 if(!found) >> 155 { 147 HepMC::GenVertex* childvtx = new H 156 HepMC::GenVertex* childvtx = new HepMC::GenVertex(prodpos); 148 childvtx->add_particle_out(particl 157 childvtx->add_particle_out(particle); 149 158 150 // the dummy vertex gets the barco 159 // the dummy vertex gets the barcode -500000 151 // minus the daughter particle bar 160 // minus the daughter particle barcode 152 // 161 // 153 childvtx->suggest_barcode(-500000 << 162 childvtx->suggest_barcode(-500000-partID); 154 fEvent->add_vertex(childvtx); 163 fEvent->add_vertex(childvtx); 155 << 164 156 HepMC::GenParticle* dummypart = ne << 165 HepMC::GenParticle* dummypart = >> 166 new HepMC::GenParticle(G4LorentzVector(),-999999); 157 167 158 // the dummy particle gets the bar 168 // the dummy particle gets the barcode 500000 159 // plus the daughter particle barc 169 // plus the daughter particle barcode 160 // 170 // 161 dummypart->suggest_barcode(500000 << 171 dummypart->suggest_barcode(500000+partID); 162 childvtx->add_particle_in(dummypar 172 childvtx->add_particle_in(dummypart); 163 motherendvtx->add_particle_out(dum 173 motherendvtx->add_particle_out(dummypart); 164 } 174 } 165 } 175 } 166 else // biological << 176 else // biological 167 { 177 { 168 // in case mother was already 'split 178 // in case mother was already 'split' we need to look for 169 // the right 'segment' to add the ne 179 // the right 'segment' to add the new daugther. 170 // We use Time coordinate to locate 180 // We use Time coordinate to locate the place for the new vertex 171 181 172 G4int number_of_segments = fSegmenta 182 G4int number_of_segments = fSegmentations[motherID]; 173 G4int segment = 0; 183 G4int segment = 0; 174 184 175 // we loop through the segments 185 // we loop through the segments 176 // << 186 // 177 while (!((mother->end_vertex()->posi << 187 while ( !((mother->end_vertex()->position().t()>prodpos.t()) && 178 && (mother->production_vert << 188 (mother->production_vertex()->position().t()<prodpos.t())) ) 179 { 189 { 180 segment++; 190 segment++; 181 if (segment == number_of_segments) << 191 if (segment == number_of_segments) 182 G4cerr << "Problem!!!! Time coor 192 G4cerr << "Problem!!!! Time coordinates incompatible!" << G4endl; 183 << 193 184 mother = fEvent->barcode_to_partic << 194 mother = fEvent->barcode_to_particle(segment*10000000 + motherID); 185 } 195 } 186 << 196 187 // now, we 'split' the appropriate ' 197 // now, we 'split' the appropriate 'segment' of the mother particle 188 // into two particles and create a n 198 // into two particles and create a new vertex 189 // 199 // 190 HepMC::GenVertex* childvtx = new Hep 200 HepMC::GenVertex* childvtx = new HepMC::GenVertex(prodpos); 191 childvtx->add_particle_out(particle) 201 childvtx->add_particle_out(particle); 192 fEvent->add_vertex(childvtx); 202 fEvent->add_vertex(childvtx); 193 203 194 // we first detach the mother from i 204 // we first detach the mother from its original vertex 195 // 205 // 196 HepMC::GenVertex* orig_mother_end_vt 206 HepMC::GenVertex* orig_mother_end_vtx = mother->end_vertex(); 197 orig_mother_end_vtx->remove_particle 207 orig_mother_end_vtx->remove_particle(mother); 198 208 199 // and attach it to the new vertex 209 // and attach it to the new vertex 200 // 210 // 201 childvtx->add_particle_in(mother); 211 childvtx->add_particle_in(mother); 202 212 203 // now we create a new particle repr 213 // now we create a new particle representing the mother after 204 // interaction the barcode of the ne 214 // interaction the barcode of the new particle is 10000000 + the 205 // original barcode 215 // original barcode 206 // 216 // 207 HepMC::GenParticle* mothertwo = new 217 HepMC::GenParticle* mothertwo = new HepMC::GenParticle(*mother); 208 mothertwo->suggest_barcode(fSegmenta << 218 mothertwo->suggest_barcode(fSegmentations[motherID]*10000000 >> 219 + mother->barcode()); 209 220 210 // we also reset the barcodes of the 221 // we also reset the barcodes of the vertices 211 // 222 // 212 orig_mother_end_vtx->suggest_barcode << 223 orig_mother_end_vtx->suggest_barcode(-fSegmentations[motherID] 213 << 224 *10000000 - mother->barcode()); 214 childvtx->suggest_barcode(-mother->b 225 childvtx->suggest_barcode(-mother->barcode()); 215 226 216 // we attach it to the new vertex wh 227 // we attach it to the new vertex where interaction took place 217 // 228 // 218 childvtx->add_particle_out(mothertwo 229 childvtx->add_particle_out(mothertwo); 219 230 220 // and we attach it to the original 231 // and we attach it to the original endvertex 221 // 232 // 222 orig_mother_end_vtx->add_particle_in 233 orig_mother_end_vtx->add_particle_in(mothertwo); 223 234 224 // and finally ... the increase the 235 // and finally ... the increase the 'segmentation counter' 225 // 236 // 226 fSegmentations[motherID] = fSegmenta << 237 fSegmentations[motherID] = fSegmentations[motherID]+1; 227 } 238 } 228 } 239 } 229 } 240 } 230 else << 241 else 231 // mother GenParticle is not there for som << 242 // mother GenParticle is not there for some reason... 232 // if this happens, we need to revise the << 243 // if this happens, we need to revise the philosophy... 233 // a solution would be to create HepMC par << 244 // a solution would be to create HepMC particles 234 // at the begining of each track << 245 // at the begining of each track 235 { 246 { 236 G4cerr << "barcode " << motherID << " mo << 247 G4cerr << "barcode " << motherID << " mother not there! "<< G4endl; 237 } 248 } 238 } 249 } 239 else // primary << 250 else // primary 240 { 251 { 241 HepMC::GenVertex* primaryvtx = new HepMC:: 252 HepMC::GenVertex* primaryvtx = new HepMC::GenVertex(prodpos); 242 primaryvtx->add_particle_out(particle); 253 primaryvtx->add_particle_out(particle); 243 fEvent->add_vertex(primaryvtx); 254 fEvent->add_vertex(primaryvtx); 244 255 245 // add id to the list of primaries 256 // add id to the list of primaries 246 // 257 // 247 fPrimarybarcodes.push_back(partID); 258 fPrimarybarcodes.push_back(partID); 248 } << 259 } 249 } 260 } 250 261 251 //....oooOO0OOooo........oooOO0OOooo........oo 262 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 252 263 253 void MCTruthManager::PrintEvent() 264 void MCTruthManager::PrintEvent() 254 { 265 { 255 fEvent->print(); 266 fEvent->print(); 256 267 257 // looping over primaries and print the deca 268 // looping over primaries and print the decay tree for each of them 258 // 269 // 259 for (std::vector<int>::const_iterator primar << 270 for(std::vector<int>::const_iterator primarybar=fPrimarybarcodes.begin(); 260 primarybar != fPrimarybarcodes.end(); p << 271 primarybar!=fPrimarybarcodes.end();primarybar++) 261 { 272 { 262 PrintTree(fEvent->barcode_to_particle(*pri 273 PrintTree(fEvent->barcode_to_particle(*primarybar), " | "); 263 } 274 } 264 } 275 } 265 276 266 //....oooOO0OOooo........oooOO0OOooo........oo 277 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 267 278 268 void MCTruthManager::PrintTree(HepMC::GenParti 279 void MCTruthManager::PrintTree(HepMC::GenParticle* particle, G4String offset) 269 { 280 { 270 G4cout << offset << "--- barcode: " << part << 281 G4cout << offset << "--- barcode: " << particle->barcode() << " pdg: " 271 << " energy: " << particle->momentum( << 282 << particle->pdg_id() << " energy: " << particle->momentum().e() 272 << " production vertex: " << particle << 283 << " production vertex: " 273 << particle->production_vertex()->pos << 284 << particle->production_vertex()->position().x() << ", " 274 << particle->production_vertex()->pos << 285 << particle->production_vertex()->position().y() << ", " 275 << particle->production_vertex()->pos << 286 << particle->production_vertex()->position().z() << ", " 276 << 287 << particle->production_vertex()->position().t() 277 for (HepMC::GenVertex::particles_out_const_i << 288 << G4endl; 278 particle->end_vertex()->particles_out << 289 279 it != particle->end_vertex()->particles << 290 for(HepMC::GenVertex::particles_out_const_iterator >> 291 it=particle->end_vertex()->particles_out_const_begin(); >> 292 it!=particle->end_vertex()->particles_out_const_end(); >> 293 it++) 280 { 294 { 281 G4String deltaoffset = ""; 295 G4String deltaoffset = ""; 282 296 283 G4int curr = std::fmod(double((*it)->barco << 297 G4int curr = std::fmod(double((*it)->barcode()),10000000.); 284 G4int part = std::fmod(double(particle->ba << 298 G4int part = std::fmod(double(particle->barcode()),10000000.); 285 if (curr != part) { << 299 if( curr != part ) 286 deltaoffset = " | "; << 300 { 287 } << 301 deltaoffset = " | "; >> 302 } 288 303 289 PrintTree((*it), offset + deltaoffset); 304 PrintTree((*it), offset + deltaoffset); 290 } << 305 } 291 } 306 } 292 307 293 //....oooOO0OOooo........oooOO0OOooo........oo 308 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 294 309