source: snovis/trunk/source/G4Lab/cxx/TrajectoryAccessor.cxx

Last change on this file was 288, checked in by barrand, 18 years ago
  • Property svn:eol-style set to native
File size: 18.2 KB
Line 
1// this :
2#include <G4Lab/TrajectoryAccessor.h>
3
4// Inventor :
5#include <Inventor/nodes/SoSeparator.h>
6#include <Inventor/nodes/SoLightModel.h>
7#include <Inventor/nodes/SoDrawStyle.h>
8#include <Inventor/nodes/SoCoordinate3.h>
9#include <Inventor/nodes/SoIndexedLineSet.h>
10#include <Inventor/engines/SoTimeCounter.h>
11#include <Inventor/engines/SoCalculator.h>
12
13// HEPVis :
14#include <HEPVis/nodes/SoHighlightMaterial.h>
15
16#ifdef WIN32
17#undef pascal // Clash between windef.h and Geant4/SystemOfUnits.hh
18#endif
19
20// Geant4 :
21#include <G4RunManager.hh>
22#include <G4VTrajectoryPoint.hh>
23#include <G4AttValue.hh>
24#include <G4UnitsTable.hh>
25
26// Slash :
27#include <Slash/Core/ISession.h>
28#include <Slash/Data/IIterator.h>
29
30// Lib :
31#include <Lib/Debug.h>
32#include <Lib/Out.h>
33#include <Lib/Value.h>
34#include <Lib/smanip.h>
35#include <Lib/sout.h>
36
37// G4Lab :
38#include <G4Lab/Interfaces/IGeant4Trajectory.h>
39#include <G4Lab/SoG4Trajectories.h>
40#include <G4Lab/BestUnit.h>
41
42//////////////////////////////////////////////////////////////////////////////
43G4Lab::TrajectoryAccessor::TrajectoryAccessor(
44 Slash::Core::ISession& aSession
45,G4RunManager* aRunManager
46)
47:OnX::InventorAccessor(aSession)
48,fType("Trajectory")
49,fRunManager(aRunManager)
50,fAttDefs(0)
51,fModeling("immediate_all")
52,fIGeant4Trajectory(false)
53,fSeparator(0)
54,fCoordinate(0)
55,fIndex(0)
56//////////////////////////////////////////////////////////////////////////////
57//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
58{
59 Lib::Debug::increment("G4Lab::TrajectoryAccessor");
60}
61//////////////////////////////////////////////////////////////////////////////
62G4Lab::TrajectoryAccessor::~TrajectoryAccessor(
63)
64//////////////////////////////////////////////////////////////////////////////
65//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
66{
67 Lib::Debug::decrement("G4Lab::TrajectoryAccessor");
68}
69//////////////////////////////////////////////////////////////////////////////
70std::string G4Lab::TrajectoryAccessor::name(
71) const
72//////////////////////////////////////////////////////////////////////////////
73//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
74{
75 return fType;
76}
77namespace G4Lab {
78 class TrajectoryIterator : public virtual Slash::Data::IIterator {
79 public: //Slash::Data::IIterator
80 virtual Slash::Data::IAccessor::Data object() {
81 if(fIterator==fVector.end()) return 0;
82 return *fIterator;
83 }
84 virtual void next() { ++fIterator; }
85 virtual void* tag() { return 0;}
86 public:
87 TrajectoryIterator(G4TrajectoryContainer& aVector)
88 :fVector(*(aVector.GetVector())) {
89 fIterator = fVector.begin();
90 }
91 virtual ~TrajectoryIterator() {}
92 private:
93 TrajectoryVector& fVector;
94 TrajectoryVector::iterator fIterator;
95 };
96}
97//////////////////////////////////////////////////////////////////////////////
98Slash::Data::IIterator* G4Lab::TrajectoryAccessor::iterator(
99)
100//////////////////////////////////////////////////////////////////////////////
101//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
102{
103 // Set properties here since a user G4VTrajectory may
104 // had been declared very lately.
105 clearProperties();
106 // G4VTrajectory properties :
107 addProperty("particle",Lib::Property::STRING);
108 addProperty("track",Lib::Property::INTEGER);
109 addProperty("parent",Lib::Property::INTEGER);
110 addProperty("charge",Lib::Property::DOUBLE);
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) {
118 // G4Lab::Trajectory properties :
119 addProperty("energy",Lib::Property::DOUBLE);
120 addProperty("totalEnergy",Lib::Property::DOUBLE);
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);
127 }
128 //addProperty("id",Lib::Property::POINTER);
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();
137 if(!event) {
138 Lib::Out out(fSession.printer());
139 out << "No event." << Lib::endl;
140 return 0;
141 }
142 G4TrajectoryContainer* trajectoryContainer =
143 event->GetTrajectoryContainer();
144 if(!trajectoryContainer) {
145 Lib::Out out(fSession.printer());
146 out << "No trajectory container." << Lib::endl;
147 return 0;
148 }
149
150 return new TrajectoryIterator(*trajectoryContainer);
151}
152//////////////////////////////////////////////////////////////////////////////
153Slash::Core::IValue* G4Lab::TrajectoryAccessor::findValue(
154 Slash::Data::IAccessor::Data aData
155,const std::string& aName
156,void* aTag
157)
158//////////////////////////////////////////////////////////////////////////////
159//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
160{
161 G4VTrajectory* obj = (G4VTrajectory*)aData;
162 IGeant4Trajectory* iobj = dynamic_cast<IGeant4Trajectory*>(obj);
163
164 //if(aName=="id") {
165 //return new Lib::Value((void*)obj);
166 //} else
167 // G4VTrajectory data :
168 if(aName=="particle") {
169 return new Lib::Value(obj->GetParticleName());
170 } else if(aName=="track") {
171 return new Lib::Value(obj->GetTrackID());
172 } else if(aName=="parent") {
173 return new Lib::Value(obj->GetParentID());
174 } else if(aName=="charge") {
175 return new Lib::Value(obj->GetCharge());
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) {
185 if(aName=="energy") {
186 return new Lib::Value(iobj->kineticEnergy());
187 } else if(aName=="totalEnergy") {
188 return new Lib::Value(iobj->totalEnergy());
189 } else if(aName=="globalTime") {
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);
206}
207//////////////////////////////////////////////////////////////////////////////
208void G4Lab::TrajectoryAccessor::beginVisualize(
209)
210//////////////////////////////////////////////////////////////////////////////
211//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
212{
213 OnX::InventorAccessor::beginVisualize();
214
215 // modeling.trajectories:immediate_all
216 // modeling.trajectories:immediate_time
217 // modeling.trajectories:pickable
218 if(!fSession.parameterValue("modeling.trajectories",fModeling))
219 fModeling = "immediate_all";
220
221 //fSession.out().println("debug : modeling \"%s\".",fModeling.c_str());
222
223 if(fModeling=="pickable") {
224
225 fSeparator = new SoSeparator;
226 fCoordinate = new SoCoordinate3;
227 fSeparator->addChild(fCoordinate);
228 fIndex = 0;
229
230 } else if( (fModeling=="immediate_all") || (fModeling=="immediate_time") ) {
231 // Scene graph :
232 SoSeparator* separator = new SoSeparator;
233 separator->setName("sceneGraph");
234
235 separator->addChild(fSoGC.getHighlightMaterial());
236 separator->addChild(fSoGC.getDrawStyle());
237 separator->addChild(fSoGC.getLightModel());
238
239 SoG4Trajectories* soG4Trajectories = new SoG4Trajectories(fRunManager);
240 separator->addChild(soG4Trajectories);
241
242 if(fModeling=="immediate_time") {
243 soG4Trajectories->model.setValue(SoG4Trajectories::TIMED);
244 //soG4Trajectories->timeSteps.setValue(1000);
245
246 SoCalculator* calculator = new SoCalculator();
247 calculator->a.connectFrom(&soG4Trajectories->timeSteps);
248 calculator->expression.set1Value(0,"oa=2.0/a");
249
250 SoTimeCounter* timeCounter = new SoTimeCounter;
251#ifdef WIN32
252#undef max
253#endif
254 timeCounter->max.connectFrom(&soG4Trajectories->timeSteps);
255 timeCounter->frequency.connectFrom(&calculator->oa);
256 timeCounter->reset.connectFrom(&soG4Trajectories->timeIndex);
257
258 soG4Trajectories->timeIndex.connectFrom(&timeCounter->output);
259
260 }
261
262 fSoRegion->doIt(SbAddNode(separator,"dynamicScene"));
263 }
264}
265//////////////////////////////////////////////////////////////////////////////
266void G4Lab::TrajectoryAccessor::visualize(
267 Slash::Data::IAccessor::Data aData
268,void*
269)
270//////////////////////////////////////////////////////////////////////////////
271//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
272{
273 if(!fSeparator) return; // not in "pickable" mode.
274
275 G4VTrajectory* obj = (G4VTrajectory*)aData;
276 if(!obj) return;
277
278 int pointn = obj->GetPointEntries();
279 if(pointn<=0) return;
280
281 SbVec3f* points = new SbVec3f[pointn];
282 int32_t* coordIndex = new int32_t[pointn+1];
283
284 for (int i = 0; i < pointn ; i++) {
285 G4VTrajectoryPoint* tp = obj->GetPoint(i); //?
286 G4ThreeVector pos = tp->GetPosition();
287 points[i].setValue((float)pos.x(),(float)pos.y(),(float)pos.z());
288 coordIndex[i] = fIndex+i;
289 }
290 coordIndex[pointn] = SO_END_LINE_INDEX;
291
292 //Get a style corresponding to Trajectory type.
293 {std::string s = obj->GetParticleName();
294 Lib::smanip::strip(s);
295 std::string style = "Trajectory("+s+")";
296 if(!isStyle(style)) {
297 style = "Trajectory_"+s; //Backward compatibility.
298 if(!isStyle(style)) {
299 style = "Trajectory"; //Default.
300 }
301 }
302 fillSoGC(style);}
303
304 // Build name (for picking) :
305 std::string s;
306 Lib::smanip::printf(s,128,"Trajectory/0x%lx",(unsigned long)obj);
307 SbName name(s.c_str());
308
309 // Scene graph :
310 SoSeparator* separator = new SoSeparator;
311 separator->setName("sceneGraph");
312 fSeparator->addChild(separator);
313
314 separator->addChild(fSoGC.getHighlightMaterial());
315 separator->addChild(fSoGC.getDrawStyle());
316 separator->addChild(fSoGC.getLightModel());
317
318 fCoordinate->point.setValues(fIndex,pointn,points);
319 fIndex += pointn;
320
321 SoIndexedLineSet* lineSet = new SoIndexedLineSet;
322 lineSet->coordIndex.setValues(0,pointn+1,coordIndex);
323 lineSet->setName(name);
324 separator->addChild(lineSet);
325
326 delete [] coordIndex;
327 delete [] points;
328}
329//////////////////////////////////////////////////////////////////////////////
330void G4Lab::TrajectoryAccessor::endVisualize(
331)
332//////////////////////////////////////////////////////////////////////////////
333//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
334{
335 if(fSeparator) {
336 if(fSeparator->getNumChildren()==1) {
337 fSeparator->unref();
338 } else {
339 fSoRegion->doIt(SbAddNode(fSeparator,"dynamicScene"));
340 }
341 fSeparator = 0;
342 fCoordinate = 0;
343 fIndex = 0;
344 }
345}
346//////////////////////////////////////////////////////////////////////////////
347bool G4Lab::TrajectoryAccessor::isIGeant4Trajectory(
348)
349//////////////////////////////////////////////////////////////////////////////
350//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
351{
352 if(!fRunManager) return false;
353 const G4Event* event = fRunManager->GetCurrentEvent();
354 if(!event) return false;
355 G4TrajectoryContainer* trajectoryContainer =
356 event->GetTrajectoryContainer();
357 if(!trajectoryContainer) return false;
358 int number = trajectoryContainer->entries();
359 if(number<=0) return false;
360 G4VTrajectory* vt = (*trajectoryContainer)[(size_t)0];
361 IGeant4Trajectory* trajectory = dynamic_cast<IGeant4Trajectory*>(vt);
362 return (trajectory?true:false);
363}
364//////////////////////////////////////////////////////////////////////////////
365void 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//////////////////////////////////////////////////////////////////////////////
417Slash::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 TracBrowser for help on using the repository browser.