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.4 2010-12-15 07:38:50 gunter Exp $ >> 28 // GEANT4 tag $Name: not supported by cvs2svn $ 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 HepMC::FourVector mp0 = motherendvtx->position(); 108 G4LorentzVector motherendpos(mp0.x(), mp 99 G4LorentzVector motherendpos(mp0.x(), mp0.y(), mp0.z(), mp0.t()); 109 << 100 110 if (motherendpos.x() == prodpos.x() && m << 101 if( motherendpos.x() == prodpos.x() && 111 && motherendpos.z() == prodpos.z()) << 102 motherendpos.y() == prodpos.y() && >> 103 motherendpos.z() == prodpos.z() ) // if yes, we attach the particle 112 { 104 { 113 motherendvtx->add_particle_out(particl 105 motherendvtx->add_particle_out(particle); 114 } 106 } 115 else // if not, we check whether the mo << 107 else // if not, we check whether the mother is biological or adopted 116 { << 108 { 117 if (!directParent) // adopted << 109 if(!directParent) // adopted 118 { << 110 { 119 G4bool found = false; 111 G4bool found = false; 120 112 121 // first check if any of the dummy p 113 // first check if any of the dummy particles 122 // has the end vertex at the right p 114 // has the end vertex at the right place 123 // 115 // 124 for (HepMC::GenVertex::particles_out << 116 for(HepMC::GenVertex::particles_out_const_iterator 125 motherendvtx->particles_out_c << 117 it=motherendvtx->particles_out_const_begin(); 126 it != motherendvtx->particles_o << 118 it!=motherendvtx->particles_out_const_end();it++) 127 { 119 { 128 if ((*it)->pdg_id() == -999999) { << 120 if((*it)->pdg_id()==-999999) >> 121 { 129 HepMC::FourVector dp0 = (*it)->e 122 HepMC::FourVector dp0 = (*it)->end_vertex()->position(); 130 G4LorentzVector dummypos(dp0.x() << 123 G4LorentzVector dummypos(dp0.x(), dp0.y(), dp0.z(), dp0.t());; 131 ; << 124 132 << 125 if( dummypos.x() == prodpos.x() && 133 if (dummypos.x() == prodpos.x() << 126 dummypos.y() == prodpos.y() && 134 && dummypos.z() == prodpos.z << 127 dummypos.z() == prodpos.z() ) 135 { 128 { 136 (*it)->end_vertex()->add_parti 129 (*it)->end_vertex()->add_particle_out(particle); 137 found = true; 130 found = true; 138 break; 131 break; 139 } 132 } 140 } 133 } 141 } 134 } 142 135 143 // and if not, create a dummy partic 136 // and if not, create a dummy particle connecting 144 // to the end vertex of the mother 137 // to the end vertex of the mother 145 // 138 // 146 if (!found) { << 139 if(!found) >> 140 { 147 HepMC::GenVertex* childvtx = new H 141 HepMC::GenVertex* childvtx = new HepMC::GenVertex(prodpos); 148 childvtx->add_particle_out(particl 142 childvtx->add_particle_out(particle); 149 143 150 // the dummy vertex gets the barco 144 // the dummy vertex gets the barcode -500000 151 // minus the daughter particle bar 145 // minus the daughter particle barcode 152 // 146 // 153 childvtx->suggest_barcode(-500000 << 147 childvtx->suggest_barcode(-500000-partID); 154 fEvent->add_vertex(childvtx); << 148 event->add_vertex(childvtx); 155 << 149 156 HepMC::GenParticle* dummypart = ne << 150 HepMC::GenParticle* dummypart = >> 151 new HepMC::GenParticle(G4LorentzVector(),-999999); 157 152 158 // the dummy particle gets the bar 153 // the dummy particle gets the barcode 500000 159 // plus the daughter particle barc 154 // plus the daughter particle barcode 160 // 155 // 161 dummypart->suggest_barcode(500000 << 156 dummypart->suggest_barcode(500000+partID); 162 childvtx->add_particle_in(dummypar 157 childvtx->add_particle_in(dummypart); 163 motherendvtx->add_particle_out(dum 158 motherendvtx->add_particle_out(dummypart); 164 } 159 } 165 } 160 } 166 else // biological << 161 else // biological 167 { 162 { 168 // in case mother was already 'split 163 // in case mother was already 'split' we need to look for 169 // the right 'segment' to add the ne 164 // the right 'segment' to add the new daugther. 170 // We use Time coordinate to locate 165 // We use Time coordinate to locate the place for the new vertex 171 166 172 G4int number_of_segments = fSegmenta << 167 G4int number_of_segments = segmentations[motherID]; 173 G4int segment = 0; 168 G4int segment = 0; 174 169 175 // we loop through the segments 170 // we loop through the segments 176 // << 171 // 177 while (!((mother->end_vertex()->posi << 172 while ( !((mother->end_vertex()->position().t()>prodpos.t()) && 178 && (mother->production_vert << 173 (mother->production_vertex()->position().t()<prodpos.t())) ) 179 { 174 { 180 segment++; 175 segment++; 181 if (segment == number_of_segments) << 176 if (segment == number_of_segments) 182 G4cerr << "Problem!!!! Time coor 177 G4cerr << "Problem!!!! Time coordinates incompatible!" << G4endl; 183 << 178 184 mother = fEvent->barcode_to_partic << 179 mother = event->barcode_to_particle(segment*10000000 + motherID); 185 } 180 } 186 << 181 187 // now, we 'split' the appropriate ' 182 // now, we 'split' the appropriate 'segment' of the mother particle 188 // into two particles and create a n 183 // into two particles and create a new vertex 189 // 184 // 190 HepMC::GenVertex* childvtx = new Hep 185 HepMC::GenVertex* childvtx = new HepMC::GenVertex(prodpos); 191 childvtx->add_particle_out(particle) 186 childvtx->add_particle_out(particle); 192 fEvent->add_vertex(childvtx); << 187 event->add_vertex(childvtx); 193 188 194 // we first detach the mother from i 189 // we first detach the mother from its original vertex 195 // 190 // 196 HepMC::GenVertex* orig_mother_end_vt 191 HepMC::GenVertex* orig_mother_end_vtx = mother->end_vertex(); 197 orig_mother_end_vtx->remove_particle 192 orig_mother_end_vtx->remove_particle(mother); 198 193 199 // and attach it to the new vertex 194 // and attach it to the new vertex 200 // 195 // 201 childvtx->add_particle_in(mother); 196 childvtx->add_particle_in(mother); 202 197 203 // now we create a new particle repr 198 // now we create a new particle representing the mother after 204 // interaction the barcode of the ne 199 // interaction the barcode of the new particle is 10000000 + the 205 // original barcode 200 // original barcode 206 // 201 // 207 HepMC::GenParticle* mothertwo = new 202 HepMC::GenParticle* mothertwo = new HepMC::GenParticle(*mother); 208 mothertwo->suggest_barcode(fSegmenta << 203 mothertwo->suggest_barcode(segmentations[motherID]*10000000 >> 204 + mother->barcode()); 209 205 210 // we also reset the barcodes of the 206 // we also reset the barcodes of the vertices 211 // 207 // 212 orig_mother_end_vtx->suggest_barcode << 208 orig_mother_end_vtx->suggest_barcode(-segmentations[motherID] 213 << 209 *10000000 - mother->barcode()); 214 childvtx->suggest_barcode(-mother->b 210 childvtx->suggest_barcode(-mother->barcode()); 215 211 216 // we attach it to the new vertex wh 212 // we attach it to the new vertex where interaction took place 217 // 213 // 218 childvtx->add_particle_out(mothertwo 214 childvtx->add_particle_out(mothertwo); 219 215 220 // and we attach it to the original 216 // and we attach it to the original endvertex 221 // 217 // 222 orig_mother_end_vtx->add_particle_in 218 orig_mother_end_vtx->add_particle_in(mothertwo); 223 219 224 // and finally ... the increase the 220 // and finally ... the increase the 'segmentation counter' 225 // 221 // 226 fSegmentations[motherID] = fSegmenta << 222 segmentations[motherID] = segmentations[motherID]+1; 227 } 223 } 228 } 224 } 229 } 225 } 230 else << 226 else 231 // mother GenParticle is not there for som << 227 // mother GenParticle is not there for some reason... 232 // if this happens, we need to revise the << 228 // if this happens, we need to revise the philosophy... 233 // a solution would be to create HepMC par << 229 // a solution would be to create HepMC particles 234 // at the begining of each track << 230 // at the begining of each track 235 { 231 { 236 G4cerr << "barcode " << motherID << " mo << 232 G4cerr << "barcode " << motherID << " mother not there! "<< G4endl; 237 } 233 } 238 } 234 } 239 else // primary << 235 else // primary 240 { 236 { 241 HepMC::GenVertex* primaryvtx = new HepMC:: 237 HepMC::GenVertex* primaryvtx = new HepMC::GenVertex(prodpos); 242 primaryvtx->add_particle_out(particle); 238 primaryvtx->add_particle_out(particle); 243 fEvent->add_vertex(primaryvtx); << 239 event->add_vertex(primaryvtx); 244 240 245 // add id to the list of primaries 241 // add id to the list of primaries 246 // 242 // 247 fPrimarybarcodes.push_back(partID); << 243 primarybarcodes.push_back(partID); 248 } << 244 } 249 } 245 } 250 246 251 //....oooOO0OOooo........oooOO0OOooo........oo << 252 << 253 void MCTruthManager::PrintEvent() 247 void MCTruthManager::PrintEvent() 254 { 248 { 255 fEvent->print(); << 249 event->print(); 256 250 257 // looping over primaries and print the deca 251 // looping over primaries and print the decay tree for each of them 258 // 252 // 259 for (std::vector<int>::const_iterator primar << 253 for(std::vector<int>::const_iterator primarybar=primarybarcodes.begin(); 260 primarybar != fPrimarybarcodes.end(); p << 254 primarybar!=primarybarcodes.end();primarybar++) 261 { 255 { 262 PrintTree(fEvent->barcode_to_particle(*pri << 256 printTree(event->barcode_to_particle(*primarybar), " | "); 263 } 257 } 264 } 258 } 265 259 266 //....oooOO0OOooo........oooOO0OOooo........oo << 260 void MCTruthManager::printTree(HepMC::GenParticle* particle, G4String offset) 267 << 268 void MCTruthManager::PrintTree(HepMC::GenParti << 269 { 261 { 270 G4cout << offset << "--- barcode: " << part << 262 G4cout << offset << "--- barcode: " << particle->barcode() << " pdg: " 271 << " energy: " << particle->momentum( << 263 << particle->pdg_id() << " energy: " << particle->momentum().e() 272 << " production vertex: " << particle << 264 << " production vertex: " 273 << particle->production_vertex()->pos << 265 << particle->production_vertex()->position().x() << ", " 274 << particle->production_vertex()->pos << 266 << particle->production_vertex()->position().y() << ", " 275 << particle->production_vertex()->pos << 267 << particle->production_vertex()->position().z() << ", " 276 << 268 << particle->production_vertex()->position().t() 277 for (HepMC::GenVertex::particles_out_const_i << 269 << G4endl; 278 particle->end_vertex()->particles_out << 270 279 it != particle->end_vertex()->particles << 271 for(HepMC::GenVertex::particles_out_const_iterator >> 272 it=particle->end_vertex()->particles_out_const_begin(); >> 273 it!=particle->end_vertex()->particles_out_const_end(); >> 274 it++) 280 { 275 { 281 G4String deltaoffset = ""; 276 G4String deltaoffset = ""; 282 277 283 G4int curr = std::fmod(double((*it)->barco << 278 G4int curr = std::fmod(double((*it)->barcode()),10000000.); 284 G4int part = std::fmod(double(particle->ba << 279 G4int part = std::fmod(double(particle->barcode()),10000000.); 285 if (curr != part) { << 280 if( curr != part ) 286 deltaoffset = " | "; << 281 { 287 } << 282 deltaoffset = " | "; >> 283 } 288 284 289 PrintTree((*it), offset + deltaoffset); << 285 printTree((*it), offset + deltaoffset); 290 } << 286 } 291 } 287 } 292 << 293 //....oooOO0OOooo........oooOO0OOooo........oo << 294 288