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