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

Last change on this file since 233 was 233, checked in by barrand, 17 years ago
  • Property svn:eol-style set to native
File size: 10.4 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
23// Slash :
24#include <Slash/Core/ISession.h>
25#include <Slash/Data/IIterator.h>
26
27// Lib :
28#include <Lib/Debug.h>
29#include <Lib/Out.h>
30#include <Lib/Value.h>
31#include <Lib/smanip.h>
32
33// G4Lab :
34#include <G4Lab/Trajectory.h>
35#include <G4Lab/SoG4Trajectories.h>
36
37//////////////////////////////////////////////////////////////////////////////
38G4Lab::TrajectoryAccessor::TrajectoryAccessor(
39 Slash::Core::ISession& aSession
40)
41:OnX::InventorAccessor(aSession)
42,fType("Trajectory")
43,fModeling("immediate_all")
44,fG4Lab(false)
45,fSeparator(0)
46,fCoordinate(0)
47,fIndex(0)
48//////////////////////////////////////////////////////////////////////////////
49//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
50{
51  Lib::Debug::increment("G4Lab::TrajectoryAccessor");
52}
53//////////////////////////////////////////////////////////////////////////////
54G4Lab::TrajectoryAccessor::~TrajectoryAccessor(
55) 
56//////////////////////////////////////////////////////////////////////////////
57//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
58{
59  Lib::Debug::decrement("G4Lab::TrajectoryAccessor");
60}
61//////////////////////////////////////////////////////////////////////////////
62std::string G4Lab::TrajectoryAccessor::name(
63) const
64//////////////////////////////////////////////////////////////////////////////
65//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
66{
67  return fType;
68}
69namespace G4Lab {
70  class TrajectoryIterator : public virtual Slash::Data::IIterator {
71  public: //Slash::Data::IIterator
72    virtual Slash::Data::IAccessor::Data object() {
73      if(fIterator==fVector.end()) return 0;
74      return *fIterator;
75    }
76    virtual void next() { ++fIterator; }
77    virtual void* tag() { return 0;}
78  public:
79    TrajectoryIterator(G4TrajectoryContainer& aVector)
80      :fVector(*(aVector.GetVector())) {
81      fIterator = fVector.begin();
82    }
83    virtual ~TrajectoryIterator() {}
84  private:
85    TrajectoryVector& fVector;
86    TrajectoryVector::iterator fIterator;
87  };
88}
89//////////////////////////////////////////////////////////////////////////////
90Slash::Data::IIterator* G4Lab::TrajectoryAccessor::iterator(
91) 
92//////////////////////////////////////////////////////////////////////////////
93//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
94{
95  // Set properties here since a user G4Trajectory may
96  // had been declared very lately.
97  clearProperties();
98  // G4Trajectory properties :
99  addProperty("particle",Lib::Property::STRING);
100  addProperty("track",Lib::Property::INTEGER);
101  addProperty("parent",Lib::Property::INTEGER);
102  addProperty("charge",Lib::Property::DOUBLE);
103  if(isG4LabTrajectory()) {
104    // G4Lab::Trajectory properties :
105    addProperty("energy",Lib::Property::DOUBLE);
106    addProperty("totalEnergy",Lib::Property::DOUBLE);
107    addProperty("globalTime",Lib::Property::DOUBLE);
108  }
109  //addProperty("id",Lib::Property::POINTER);
110
111  G4RunManager* runManager = G4RunManager::GetRunManager();
112  if(!runManager) return 0;
113  const G4Event* event = runManager->GetCurrentEvent();
114  if(!event) {
115    Lib::Out out(fSession.printer());
116    out << "No event." << Lib::endl;
117    return 0;
118  }
119  G4TrajectoryContainer* trajectoryContainer = 
120    event->GetTrajectoryContainer();
121  if(!trajectoryContainer) {
122    Lib::Out out(fSession.printer());
123    out << "No trajectory container." << Lib::endl;
124    return 0;
125  }
126
127  fG4Lab = isG4LabTrajectory();
128
129  return new TrajectoryIterator(*trajectoryContainer);
130}
131//////////////////////////////////////////////////////////////////////////////
132Slash::Core::IValue* G4Lab::TrajectoryAccessor::findValue(
133 Slash::Data::IAccessor::Data aData
134,const std::string& aName
135,void*
136) 
137//////////////////////////////////////////////////////////////////////////////
138//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
139{
140  G4Trajectory* obj = (G4Trajectory*)aData;
141  //if(aName=="id") {
142    //return new Lib::Value((void*)obj);
143  //} else
144  if(aName=="particle") {
145    return new Lib::Value(obj->GetParticleName());
146  } else if(aName=="track") {
147    return new Lib::Value(obj->GetTrackID());
148  } else if(aName=="parent") {
149    return new Lib::Value(obj->GetParentID());
150  } else if(aName=="charge") {
151    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;
156    if(aName=="energy") {
157      return new Lib::Value(obj->GetKineticEnergy());
158    } else if(aName=="totalEnergy") {
159      return new Lib::Value(obj->GetTotalEnergy());
160    } else if(aName=="globalTime") {
161      return new Lib::Value(obj->GetGlobalTime());
162    } else {
163      return new Lib::Value();
164    }
165  }
166}
167//////////////////////////////////////////////////////////////////////////////
168void G4Lab::TrajectoryAccessor::beginVisualize(
169) 
170//////////////////////////////////////////////////////////////////////////////
171//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
172{
173  OnX::InventorAccessor::beginVisualize();
174
175  // modeling.trajectories:immediate_all
176  // modeling.trajectories:immediate_time
177  // modeling.trajectories:pickable
178  if(!fSession.parameterValue("modeling.trajectories",fModeling))
179    fModeling = "immediate_all";
180
181  //fSession.out().println("debug : modeling \"%s\".",fModeling.c_str());
182
183  if(fModeling=="pickable") {
184
185    fSeparator = new SoSeparator;
186    fCoordinate = new SoCoordinate3;
187    fSeparator->addChild(fCoordinate);
188    fIndex = 0;
189
190  } else if( (fModeling=="immediate_all") || (fModeling=="immediate_time") ) {
191    // Scene graph :
192    SoSeparator* separator = new SoSeparator;
193    separator->setName("sceneGraph");
194   
195    separator->addChild(fSoGC.getHighlightMaterial());
196    separator->addChild(fSoGC.getDrawStyle());
197    separator->addChild(fSoGC.getLightModel());
198
199    SoG4Trajectories* soG4Trajectories = new SoG4Trajectories;
200    separator->addChild(soG4Trajectories);
201
202    if(fModeling=="immediate_time") {
203      soG4Trajectories->model.setValue(SoG4Trajectories::TIMED);
204      //soG4Trajectories->timeSteps.setValue(1000);
205
206      SoCalculator* calculator = new SoCalculator();
207      calculator->a.connectFrom(&soG4Trajectories->timeSteps);
208      calculator->expression.set1Value(0,"oa=2.0/a");
209
210      SoTimeCounter* timeCounter = new SoTimeCounter;
211#ifdef WIN32
212#undef max
213#endif
214      timeCounter->max.connectFrom(&soG4Trajectories->timeSteps);
215      timeCounter->frequency.connectFrom(&calculator->oa);
216      timeCounter->reset.connectFrom(&soG4Trajectories->timeIndex);
217
218      soG4Trajectories->timeIndex.connectFrom(&timeCounter->output);
219
220    }
221
222    fSoRegion->doIt(SbAddNode(separator,"dynamicScene"));
223  }
224}
225//////////////////////////////////////////////////////////////////////////////
226void G4Lab::TrajectoryAccessor::visualize(
227 Slash::Data::IAccessor::Data aData
228,void*
229) 
230//////////////////////////////////////////////////////////////////////////////
231//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
232{
233  if(!fSeparator) return; // not in "pickable" mode.
234
235  G4Trajectory* obj = (G4Trajectory*)aData;
236  if(!obj) return;
237
238  int pointn = obj->GetPointEntries();
239  if(pointn<=0) return;
240 
241  SbVec3f* points = new SbVec3f[pointn];
242  int32_t* coordIndex = new int32_t[pointn+1];
243
244  G4TrajectoryPoint* tp;
245  G4ThreeVector pos;
246  for (int i = 0; i < pointn ; i++) {
247    tp = (G4TrajectoryPoint*)(obj->GetPoint(i)); //?
248    pos = tp->GetPosition();
249    points[i].setValue((float)pos.x(),(float)pos.y(),(float)pos.z());
250    coordIndex[i] = fIndex+i;
251  }
252  coordIndex[pointn] = SO_END_LINE_INDEX;
253 
254  //Get a style corresponding to Trajectory type.
255 {std::string s = obj->GetParticleName();
256  Lib::smanip::strip(s);
257  std::string style = "Trajectory("+s+")";
258  if(!isStyle(style)) {
259    style = "Trajectory_"+s; //Backward compatibility.
260    if(!isStyle(style)) {
261      style = "Trajectory"; //Default.
262    }
263  }
264  fillSoGC(style);}
265
266  // Build name (for picking) :
267  std::string s;
268  Lib::smanip::printf(s,128,"Trajectory/0x%lx",(unsigned long)obj);
269  SbName name(s.c_str());
270
271  // Scene graph :
272  SoSeparator* separator = new SoSeparator;
273  separator->setName("sceneGraph");
274  fSeparator->addChild(separator);
275 
276  separator->addChild(fSoGC.getHighlightMaterial());
277  separator->addChild(fSoGC.getDrawStyle());
278  separator->addChild(fSoGC.getLightModel());
279
280  fCoordinate->point.setValues(fIndex,pointn,points);
281  fIndex += pointn;
282
283  SoIndexedLineSet* lineSet = new SoIndexedLineSet;
284  lineSet->coordIndex.setValues(0,pointn+1,coordIndex);
285  lineSet->setName(name);
286  separator->addChild(lineSet);
287
288  delete [] coordIndex;
289  delete [] points;
290}
291//////////////////////////////////////////////////////////////////////////////
292void G4Lab::TrajectoryAccessor::endVisualize(
293) 
294//////////////////////////////////////////////////////////////////////////////
295//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
296{
297  if(fSeparator) {
298    if(fSeparator->getNumChildren()==1) {
299      fSeparator->unref();
300    } else {
301      fSoRegion->doIt(SbAddNode(fSeparator,"dynamicScene"));
302    }
303    fSeparator = 0;
304    fCoordinate = 0;
305    fIndex = 0;
306  }
307}
308//////////////////////////////////////////////////////////////////////////////
309bool G4Lab::TrajectoryAccessor::isG4LabTrajectory(
310)
311//////////////////////////////////////////////////////////////////////////////
312//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
313{
314  G4RunManager* runManager = G4RunManager::GetRunManager();
315  if(!runManager) return false;
316  const G4Event* event = runManager->GetCurrentEvent();
317  if(!event) return false;
318  G4TrajectoryContainer* trajectoryContainer = 
319    event->GetTrajectoryContainer();
320  if(!trajectoryContainer) return false;
321  int number = trajectoryContainer->entries();
322  if(number<=0) return false;
323  G4Lab::Trajectory* trajectory = 
324    dynamic_cast<G4Lab::Trajectory*>((G4Trajectory*)(*trajectoryContainer)[(size_t)0]);
325  return (trajectory?true:false);
326}
Note: See TracBrowser for help on using the repository browser.