Changeset 288 for snovis/trunk/source/G4Lab/cxx/TrajectoryAccessor.cxx
- Timestamp:
- Sep 25, 2007, 12:24:08 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
snovis/trunk/source/G4Lab/cxx/TrajectoryAccessor.cxx
r233 r288 20 20 // Geant4 : 21 21 #include <G4RunManager.hh> 22 #include <G4VTrajectoryPoint.hh> 23 #include <G4AttValue.hh> 24 #include <G4UnitsTable.hh> 22 25 23 26 // Slash : … … 30 33 #include <Lib/Value.h> 31 34 #include <Lib/smanip.h> 35 #include <Lib/sout.h> 32 36 33 37 // G4Lab : 34 #include <G4Lab/ Trajectory.h>38 #include <G4Lab/Interfaces/IGeant4Trajectory.h> 35 39 #include <G4Lab/SoG4Trajectories.h> 40 #include <G4Lab/BestUnit.h> 36 41 37 42 ////////////////////////////////////////////////////////////////////////////// 38 43 G4Lab::TrajectoryAccessor::TrajectoryAccessor( 39 44 Slash::Core::ISession& aSession 45 ,G4RunManager* aRunManager 40 46 ) 41 47 :OnX::InventorAccessor(aSession) 42 48 ,fType("Trajectory") 49 ,fRunManager(aRunManager) 50 ,fAttDefs(0) 43 51 ,fModeling("immediate_all") 44 ,f G4Lab(false)52 ,fIGeant4Trajectory(false) 45 53 ,fSeparator(0) 46 54 ,fCoordinate(0) … … 93 101 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 94 102 { 95 // Set properties here since a user G4 Trajectory may103 // Set properties here since a user G4VTrajectory may 96 104 // had been declared very lately. 97 105 clearProperties(); 98 // G4 Trajectory properties :106 // G4VTrajectory properties : 99 107 addProperty("particle",Lib::Property::STRING); 100 108 addProperty("track",Lib::Property::INTEGER); 101 109 addProperty("parent",Lib::Property::INTEGER); 102 110 addProperty("charge",Lib::Property::DOUBLE); 103 if(isG4LabTrajectory()) { 111 addProperty("pdg",Lib::Property::INTEGER); 112 addProperty("px",Lib::Property::DOUBLE); 113 addProperty("py",Lib::Property::DOUBLE); 114 addProperty("pz",Lib::Property::DOUBLE); 115 116 fIGeant4Trajectory = isIGeant4Trajectory(); 117 if(fIGeant4Trajectory) { 104 118 // G4Lab::Trajectory properties : 105 119 addProperty("energy",Lib::Property::DOUBLE); 106 120 addProperty("totalEnergy",Lib::Property::DOUBLE); 107 121 addProperty("globalTime",Lib::Property::DOUBLE); 122 addProperty("process",Lib::Property::STRING); 123 addProperty("stopX",Lib::Property::DOUBLE); 124 addProperty("stopY",Lib::Property::DOUBLE); 125 addProperty("stopZ",Lib::Property::DOUBLE); 126 addProperty("stopPV",Lib::Property::STRING); 108 127 } 109 128 //addProperty("id",Lib::Property::POINTER); 110 111 G4RunManager* runManager = G4RunManager::GetRunManager(); 112 if(!runManager) return 0; 113 const G4Event* event = runManager->GetCurrentEvent(); 129 addAttDefsProperties(); 130 131 if(!fRunManager) { 132 Lib::Out out(fSession.printer()); 133 out << "No G4RunManager." << Lib::endl; 134 return 0; 135 } 136 const G4Event* event = fRunManager->GetCurrentEvent(); 114 137 if(!event) { 115 138 Lib::Out out(fSession.printer()); … … 125 148 } 126 149 127 fG4Lab = isG4LabTrajectory();128 129 150 return new TrajectoryIterator(*trajectoryContainer); 130 151 } … … 133 154 Slash::Data::IAccessor::Data aData 134 155 ,const std::string& aName 135 ,void* 136 ) 137 ////////////////////////////////////////////////////////////////////////////// 138 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 139 { 140 G4Trajectory* obj = (G4Trajectory*)aData; 156 ,void* aTag 157 ) 158 ////////////////////////////////////////////////////////////////////////////// 159 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 160 { 161 G4VTrajectory* obj = (G4VTrajectory*)aData; 162 IGeant4Trajectory* iobj = dynamic_cast<IGeant4Trajectory*>(obj); 163 141 164 //if(aName=="id") { 142 165 //return new Lib::Value((void*)obj); 143 166 //} else 167 // G4VTrajectory data : 144 168 if(aName=="particle") { 145 169 return new Lib::Value(obj->GetParticleName()); … … 150 174 } else if(aName=="charge") { 151 175 return new Lib::Value(obj->GetCharge()); 152 } else if(!fG4Lab) { 153 return new Lib::Value(); 154 } else { // We have G4Lab::Trajectory objects : 155 G4Lab::Trajectory* obj = (G4Lab::Trajectory*)aData; 176 } else if(aName=="pdg") { 177 return new Lib::Value(obj->GetPDGEncoding()); 178 } else if(aName=="px") { 179 return new Lib::Value(obj->GetInitialMomentum().x()); 180 } else if(aName=="py") { 181 return new Lib::Value(obj->GetInitialMomentum().y()); 182 } else if(aName=="pz") { 183 return new Lib::Value(obj->GetInitialMomentum().z()); 184 } else if(iobj) { 156 185 if(aName=="energy") { 157 return new Lib::Value( obj->GetKineticEnergy());186 return new Lib::Value(iobj->kineticEnergy()); 158 187 } else if(aName=="totalEnergy") { 159 return new Lib::Value( obj->GetTotalEnergy());188 return new Lib::Value(iobj->totalEnergy()); 160 189 } else if(aName=="globalTime") { 161 return new Lib::Value(obj->GetGlobalTime()); 162 } else { 163 return new Lib::Value(); 164 } 165 } 190 return new Lib::Value(iobj->globalTime()); 191 } else if(aName=="process") { 192 return new Lib::Value(iobj->creatorProcessName()); 193 } else if(aName=="stopX") { 194 return new Lib::Value(iobj->stoppingPoint()[0]); 195 } else if(aName=="stopY") { 196 return new Lib::Value(iobj->stoppingPoint()[1]); 197 } else if(aName=="stopZ") { 198 return new Lib::Value(iobj->stoppingPoint()[2]); 199 } else if(aName=="stopPV") { 200 G4VPhysicalVolume* pv = iobj->stoppingPhysicalVolume(); 201 if(!pv) return new Lib::Value(); 202 return new Lib::Value(pv->GetName()); 203 } 204 } 205 return findAttDefsValue(aData,aName,aTag); 166 206 } 167 207 ////////////////////////////////////////////////////////////////////////////// … … 197 237 separator->addChild(fSoGC.getLightModel()); 198 238 199 SoG4Trajectories* soG4Trajectories = new SoG4Trajectories ;239 SoG4Trajectories* soG4Trajectories = new SoG4Trajectories(fRunManager); 200 240 separator->addChild(soG4Trajectories); 201 241 … … 233 273 if(!fSeparator) return; // not in "pickable" mode. 234 274 235 G4 Trajectory* obj = (G4Trajectory*)aData;275 G4VTrajectory* obj = (G4VTrajectory*)aData; 236 276 if(!obj) return; 237 277 … … 242 282 int32_t* coordIndex = new int32_t[pointn+1]; 243 283 244 G4TrajectoryPoint* tp;245 G4ThreeVector pos;246 284 for (int i = 0; i < pointn ; i++) { 247 tp = (G4TrajectoryPoint*)(obj->GetPoint(i)); //?248 pos = tp->GetPosition();285 G4VTrajectoryPoint* tp = obj->GetPoint(i); //? 286 G4ThreeVector pos = tp->GetPosition(); 249 287 points[i].setValue((float)pos.x(),(float)pos.y(),(float)pos.z()); 250 288 coordIndex[i] = fIndex+i; … … 307 345 } 308 346 ////////////////////////////////////////////////////////////////////////////// 309 bool G4Lab::TrajectoryAccessor::is G4LabTrajectory(347 bool G4Lab::TrajectoryAccessor::isIGeant4Trajectory( 310 348 ) 311 349 ////////////////////////////////////////////////////////////////////////////// 312 350 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 313 351 { 314 G4RunManager* runManager = G4RunManager::GetRunManager(); 315 if(!runManager) return false; 316 const G4Event* event = runManager->GetCurrentEvent(); 352 if(!fRunManager) return false; 353 const G4Event* event = fRunManager->GetCurrentEvent(); 317 354 if(!event) return false; 318 355 G4TrajectoryContainer* trajectoryContainer = … … 321 358 int number = trajectoryContainer->entries(); 322 359 if(number<=0) return false; 323 G4 Lab::Trajectory* trajectory =324 dynamic_cast<G4Lab::Trajectory*>((G4Trajectory*)(*trajectoryContainer)[(size_t)0]);360 G4VTrajectory* vt = (*trajectoryContainer)[(size_t)0]; 361 IGeant4Trajectory* trajectory = dynamic_cast<IGeant4Trajectory*>(vt); 325 362 return (trajectory?true:false); 326 363 } 364 ////////////////////////////////////////////////////////////////////////////// 365 void G4Lab::TrajectoryAccessor::addAttDefsProperties( 366 ) 367 ////////////////////////////////////////////////////////////////////////////// 368 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 369 { 370 fAttDefs = 0; 371 if(!fRunManager) return; 372 const G4Event* event = fRunManager->GetCurrentEvent(); 373 if(!event) return; 374 G4TrajectoryContainer* trajectoryContainer = 375 event->GetTrajectoryContainer(); 376 if(!trajectoryContainer) return; 377 int number = trajectoryContainer->entries(); 378 if(number<=0) return; 379 G4VTrajectory* vt = (*trajectoryContainer)[(size_t)0]; 380 381 // AttDefs properties : 382 fAttDefs = (std::map<G4String,G4AttDef>*)vt->GetAttDefs(); 383 if(!fAttDefs) return; 384 385 std::map<G4String,G4AttDef>::const_iterator it; 386 for(it=fAttDefs->begin();it!=fAttDefs->end();++it) { 387 const std::string& sname = (*it).second.GetName(); 388 const std::string& stype = (*it).second.GetValueType(); 389 390 if(sname=="Color") continue; //Do not put some AttDef as property. 391 392 //printf("debug : add %s %s\n",sname.c_str(),stype.c_str()); 393 394 if( (stype=="int") || 395 (stype=="G4int") ) { 396 addProperty((*it).first,Lib::Property::INTEGER); 397 } else if( (stype=="double") || 398 (stype=="float") || 399 (stype=="G4double") || 400 (stype=="G4float") ) { 401 addProperty((*it).first,Lib::Property::DOUBLE); 402 } else if( (stype=="std::string") || 403 (stype=="G4String") ) { 404 addProperty((*it).first,Lib::Property::STRING); 405 } else if( (stype=="std::vector<double>") || 406 (stype=="std::vector<float>") || 407 (stype=="G4ThreeVector") || 408 (stype=="std::vector<G4double>") || 409 (stype=="std::vector<G4float>") ) { 410 addProperty((*it).first,Lib::Property::VECTOR_DOUBLE); 411 } else { 412 addProperty((*it).first,Lib::Property::STRING); 413 } 414 } 415 } 416 ////////////////////////////////////////////////////////////////////////////// 417 Slash::Core::IValue* G4Lab::TrajectoryAccessor::findAttDefsValue( 418 Slash::Data::IAccessor::Data aData 419 ,const std::string& aName 420 ,void* 421 ) 422 ////////////////////////////////////////////////////////////////////////////// 423 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 424 { 425 //printf("debug : find %s (%ld)...\n",aName.c_str(),fAttDefs); 426 427 if(!fAttDefs) return new Lib::Value(); 428 429 G4VTrajectory* obj = (G4VTrajectory*)aData; 430 std::vector<G4AttValue>* vec = obj->CreateAttValues(); 431 if(!vec) { 432 Lib::Out out(printer()); 433 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 434 << " problem to get AttValues fot trajectory." 435 << Lib::endl; 436 return new Lib::Value(); 437 } 438 439 unsigned int number = vec->size(); 440 for(unsigned int index=0;index<number;index++) { 441 const G4AttValue& val = (*vec)[index]; 442 if(aName==val.GetName()) { 443 const std::string& stype = (*fAttDefs)[val.GetName()].GetValueType(); 444 if( (stype=="int") || 445 (stype=="G4int") ) { 446 int v; 447 if(!Lib::smanip::toint(val.GetValue(),v)) { 448 Lib::Out out(printer()); 449 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 450 << " " << Lib::sout(val.GetValue()) << " not a G4int." 451 << Lib::endl; 452 delete vec; 453 return new Lib::Value(); 454 } 455 delete vec; 456 return new Lib::Value(v); 457 } else if( (stype=="double") || 458 (stype=="float") || 459 (stype=="G4double") || 460 (stype=="G4float") ) { 461 double v = 0; 462 if(!Lib::smanip::todouble(val.GetValue(),v)) { 463 std::vector<double> vs; 464 if(!BestUnit::toDoubles(printer(),val.GetValue(),vs)) { 465 delete vec; 466 return new Lib::Value(); 467 } 468 if(vs.size()!=1) { 469 Lib::Out out(printer()); 470 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 471 << " " << Lib::sout(val.GetValue()) << " not a double." 472 << Lib::endl; 473 delete vec; 474 return new Lib::Value(); 475 } 476 v = vs[0]; 477 } 478 delete vec; 479 return new Lib::Value(v); 480 } else if( (stype=="std::string") || 481 (stype=="G4String") ) { 482 delete vec; 483 return new Lib::Value(val.GetValue()); 484 } else if(stype=="G4ThreeVector") { 485 std::vector<double> vs; 486 if(!BestUnit::toDoubles(printer(),val.GetValue(),vs)) { 487 delete vec; 488 return new Lib::Value(); 489 } 490 if(vs.size()!=3) { 491 Lib::Out out(printer()); 492 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 493 << " " << Lib::sout(val.GetValue()) << " not a vector." 494 << Lib::endl; 495 delete vec; 496 return new Lib::Value(); 497 } 498 delete vec; 499 return new Lib::Value(vs); 500 } else if( (stype=="std::vector<float>") || 501 (stype=="std::vector<G4float>") ) { 502 void* p; 503 if(!Lib::smanip::topointer(val.GetValue(),p)) { 504 Lib::Out out(printer()); 505 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 506 << " " << Lib::sout(val.GetValue()) << " not a pointer." 507 << Lib::endl; 508 delete vec; 509 return new Lib::Value(); 510 } 511 std::vector<float>* vvec = (std::vector<float>*)p; 512 unsigned int vn = vvec->size(); 513 std::vector<double> array(vn); 514 for(unsigned int vi=0;vi<vn;vi++) array[vi] = double((*vvec)[vi]); 515 delete vec; 516 return new Lib::Value(array); 517 } else if( (stype=="std::vector<double>") || 518 (stype=="std::vector<G4double>") ){ 519 void* p; 520 if(!Lib::smanip::topointer(val.GetValue(),p)) { 521 Lib::Out out(printer()); 522 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 523 << " " << Lib::sout(val.GetValue()) << " not a pointer." 524 << Lib::endl; 525 delete vec; 526 return new Lib::Value(); 527 } 528 std::vector<double>* vvec = (std::vector<double>*)p; 529 delete vec; 530 return new Lib::Value(*vvec); 531 } else { 532 delete vec; 533 return new Lib::Value(val.GetValue()); 534 } 535 } 536 } 537 Lib::Out out(printer()); 538 out << "G4Lab::TrajectoryAccessor::findAttDefsValue :" 539 << " AttValue not found for property " << Lib::sout(aName) << "." 540 << Lib::endl; 541 delete vec; 542 return new Lib::Value(); 543 }
Note: See TracChangeset
for help on using the changeset viewer.