source: snovis/trunk/source/G4Lab/cxx/HitsCollectionAccessor.cxx@ 260

Last change on this file since 260 was 233, checked in by barrand, 19 years ago
  • Property svn:eol-style set to native
File size: 14.9 KB
Line 
1// this :
2#include <G4Lab/HitsCollectionAccessor.h>
3
4// Inventor :
5#include <Inventor/nodes/SoSeparator.h>
6#include <Inventor/nodes/SoTransform.h>
7#include <Inventor/nodes/SoLightModel.h>
8#include <Inventor/nodes/SoDrawStyle.h>
9
10// HEPVis :
11#include <HEPVis/misc/SoStyleCache.h>
12#include <HEPVis/nodes/SoPolyhedron.h>
13#include <HEPVis/nodes/SoHighlightMaterial.h>
14
15#ifdef WIN32
16#undef pascal // Clash between windef.h and Geant4/SystemOfUnits.hh
17#endif
18
19// Geant4 :
20#include <G4RunManager.hh>
21#include <G4Event.hh>
22#include <G4SDManager.hh>
23#include <G4HCofThisEvent.hh>
24#include <G4LogicalVolume.hh>
25#include <G4Transform3D.hh>
26#include <G4AttDef.hh>
27#include <G4AttValue.hh>
28#include <G4Polyhedron.hh>
29#include <G4Colour.hh>
30
31// Lib :
32#include <Slash/Core/ISession.h>
33#include <Slash/Data/IIterator.h>
34#include <Lib/Out.h>
35#include <Lib/Value.h>
36#include <Lib/smanip.h>
37#include <Lib/sout.h>
38
39// G4Lab :
40#include <G4Lab/Transform3D.h>
41#include <G4Lab/Polyhedron.h>
42
43//////////////////////////////////////////////////////////////////////////////
44G4Lab::HitsCollectionAccessor::HitsCollectionAccessor(
45 Slash::Core::ISession& aSession
46,const std::string& aHC
47)
48:OnX::InventorAccessor(aSession)
49,fType("HC")
50,fAttDefs(0)
51,fHC(aHC)
52//////////////////////////////////////////////////////////////////////////////
53//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
54{
55 if(fHC!="") fType = fHC;
56}
57//////////////////////////////////////////////////////////////////////////////
58G4Lab::HitsCollectionAccessor::~HitsCollectionAccessor(
59)
60//////////////////////////////////////////////////////////////////////////////
61//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
62{
63}
64//////////////////////////////////////////////////////////////////////////////
65std::string G4Lab::HitsCollectionAccessor::name(
66) const
67//////////////////////////////////////////////////////////////////////////////
68//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
69{
70 return fType;
71}
72//////////////////////////////////////////////////////////////////////////////
73const std::string& G4Lab::HitsCollectionAccessor::HCName(
74) const
75//////////////////////////////////////////////////////////////////////////////
76//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
77{
78 return fHC;
79}
80namespace G4Lab {
81 class HitsCollectionIterator : public virtual Slash::Data::IIterator {
82 public: //Slash::Data::IIterator
83 virtual Slash::Data::IAccessor::Data object() {
84 if(fCurrent>=fVector.GetSize()) return 0;
85 G4VHit* hit = fVector.GetHit(fCurrent);
86 return hit;
87 }
88 virtual void next() { fCurrent++;}
89 virtual void* tag() { return 0;}
90 public:
91 HitsCollectionIterator(G4VHitsCollection& aVector)
92 :fVector(aVector),fCurrent(0) {}
93 virtual ~HitsCollectionIterator() {}
94 private:
95 G4VHitsCollection& fVector;
96 unsigned int fCurrent;
97 };
98}
99//////////////////////////////////////////////////////////////////////////////
100Slash::Data::IIterator* G4Lab::HitsCollectionAccessor::iterator(
101)
102//////////////////////////////////////////////////////////////////////////////
103//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
104{
105 const std::vector<std::string>& args = iteratorArguments();
106 if(args.size()) fHC = args[0];
107
108 // Initialize properties here since we declare the hit AttDefs
109 // as properties and then we need at least one hit.
110 clearProperties();
111 fAttDefs = 0;
112
113 G4VHitsCollection* hc = getCollection(printer(),fHC);
114 if(!hc) return 0;
115
116 if(hc->GetSize()) {
117 G4VHit* hit = hc->GetHit(0);
118 if(hit) {
119 fAttDefs = (std::map<G4String,G4AttDef>*)hit->GetAttDefs();
120 if(fAttDefs) {
121 std::map<G4String,G4AttDef>::const_iterator it;
122 for(it=fAttDefs->begin();it!=fAttDefs->end();++it) {
123 const std::string& sname = (*it).second.GetName();
124 if((sname!="LV") &&
125 (sname!="TSF") &&
126 (sname!="Color")) {
127 const std::string& stype = (*it).second.GetValueType();
128 if(stype=="G4int") {
129 addProperty((*it).first,Lib::Property::INTEGER);
130 } else if((stype=="G4double")||(stype=="G4float")) {
131 addProperty((*it).first,Lib::Property::DOUBLE);
132 } else if((stype=="std::vector<G4float>")||
133 (stype=="std::vector<G4double>")) {
134 addProperty((*it).first,Lib::Property::VECTOR_DOUBLE);
135 } else {
136 addProperty((*it).first,Lib::Property::STRING);
137 }
138 }
139 }
140 }
141 }
142 }
143
144 return new HitsCollectionIterator(*hc);
145}
146//////////////////////////////////////////////////////////////////////////////
147Slash::Core::IValue* G4Lab::HitsCollectionAccessor::findValue(
148 Slash::Data::IAccessor::Data aData
149,const std::string& aName
150,void*
151)
152//////////////////////////////////////////////////////////////////////////////
153//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
154{
155 if(!fAttDefs) {
156 Lib::Out out(printer());
157 out << "G4Lab::HitsCollectionAccessor::value :"
158 << " problem to get AttDefs fot hit collection " << Lib::sout(fHC)
159 << "."
160 << Lib::endl;
161 return new Lib::Value();
162 }
163
164 G4VHit* obj = (G4VHit*)aData;
165 std::vector<G4AttValue>* vec = obj->CreateAttValues();
166 if(!vec) {
167 Lib::Out out(printer());
168 out << "G4Lab::HitsCollectionAccessor::value :"
169 << " problem to get AttValues fot hit collection " << Lib::sout(fHC)
170 << "."
171 << Lib::endl;
172 return new Lib::Value();
173 }
174
175 unsigned int number = vec->size();
176 for(unsigned int index=0;index<number;index++) {
177 const G4AttValue& val = (*vec)[index];
178 if(aName==val.GetName()) {
179 const std::string& stype = (*fAttDefs)[val.GetName()].GetValueType();
180 if(stype=="G4int") {
181 int v;
182 if(!Lib::smanip::toint(val.GetValue(),v)) {
183 Lib::Out out(printer());
184 out << "G4Lab::HitsCollectionAccessor::value :"
185 << " " << Lib::sout(val.GetValue()) << " not a G4int."
186 << Lib::endl;
187 delete vec;
188 return new Lib::Value();
189 }
190 delete vec;
191 return new Lib::Value(v);
192 } else if((stype=="G4double")||(stype=="G4float")) {
193 double v;
194 if(!Lib::smanip::todouble(val.GetValue(),v)) {
195 Lib::Out out(printer());
196 out << "G4Lab::HitsCollectionAccessor::value :"
197 << " " << Lib::sout(val.GetValue()) << " not a double."
198 << Lib::endl;
199 delete vec;
200 return new Lib::Value();
201 }
202 delete vec;
203 return new Lib::Value(v);
204 } else if(stype=="std::vector<G4float>") {
205 void* p;
206 if(!Lib::smanip::topointer(val.GetValue(),p)) {
207 Lib::Out out(printer());
208 out << "G4Lab::HitsCollectionAccessor::value :"
209 << " " << Lib::sout(val.GetValue()) << " not a pointer."
210 << Lib::endl;
211 delete vec;
212 return new Lib::Value();
213 }
214 std::vector<float>* vvec = (std::vector<float>*)p;
215 unsigned int vn = vvec->size();
216 std::vector<double> array(vn);
217 for(unsigned int vi=0;vi<vn;vi++) array[vi] = double((*vvec)[vi]);
218 delete vec;
219 return new Lib::Value(array);
220 } else if(stype=="std::vector<G4double>") {
221 void* p;
222 if(!Lib::smanip::topointer(val.GetValue(),p)) {
223 Lib::Out out(printer());
224 out << "G4Lab::HitsCollectionAccessor::value :"
225 << " " << Lib::sout(val.GetValue()) << " not a pointer."
226 << Lib::endl;
227 delete vec;
228 return new Lib::Value();
229 }
230 std::vector<double>* vvec = (std::vector<double>*)p;
231 delete vec;
232 return new Lib::Value(*vvec);
233 } else {
234 delete vec;
235 return new Lib::Value(val.GetValue());
236 }
237 }
238 }
239 Lib::Out out(printer());
240 out << "G4Lab::HitsCollectionAccessor::value :"
241 << " AttValue not found for property " << Lib::sout(aName) << "."
242 << Lib::endl;
243 delete vec;
244 return new Lib::Value();
245}
246//////////////////////////////////////////////////////////////////////////////
247void G4Lab::HitsCollectionAccessor::beginVisualize(
248)
249//////////////////////////////////////////////////////////////////////////////
250//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
251{
252 OnX::InventorAccessor::beginVisualize();
253
254 std::string style = "HC("+fHC+")";
255 if(!isStyle(style)) style = "HC"; //Default.
256 fillSoGC(style);
257}
258//////////////////////////////////////////////////////////////////////////////
259void G4Lab::HitsCollectionAccessor::visualize(
260 Slash::Data::IAccessor::Data aData
261,void*
262)
263//////////////////////////////////////////////////////////////////////////////
264// The hit must have the "LV" and "TSF" AttDef.
265//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
266{
267 G4VHit* obj = (G4VHit*)aData;
268 G4LogicalVolume* lv = hitLogicalVolume(*obj);
269 if(!lv) return;
270 G4Transform3D* tsf = hitTransform3D(*obj);
271 if(!tsf) return;
272 G4VSolid* solid = lv->GetSolid();
273 if(!solid) return;
274 //G4Material* material = lv->GetMaterial();
275
276 int old_nstep = HepPolyhedron::GetNumberOfRotationSteps();
277 HepPolyhedron::SetNumberOfRotationSteps(fSoGC.getRotationSteps());
278 G4Polyhedron* g4Polyhedron = solid->CreatePolyhedron();
279 HepPolyhedron::SetNumberOfRotationSteps(old_nstep);
280 if(!g4Polyhedron) return;
281
282 G4Lab::Polyhedron* polyhedron = new G4Lab::Polyhedron(*g4Polyhedron);
283 delete g4Polyhedron;
284 if(!polyhedron) return;
285
286 SoSeparator* separator = new SoSeparator;
287 separator->setName("sceneGraph");
288
289 {G4Colour color;
290 if(hitColor(*obj,color)) {
291 SbColor sbColor((float)color.GetRed(),
292 (float)color.GetGreen(),
293 (float)color.GetBlue());
294 float transp = 1.0F - (float)color.GetAlpha();
295 SoStyleCache* styleCache = fSoGC.getStyleCache();
296 separator->addChild(
297 styleCache->getHighlightMaterial
298 (sbColor,fSoGC.getHighlightColor(),transp));
299 } else {
300 separator->addChild(fSoGC.getHighlightMaterial());
301 }
302 separator->addChild(fSoGC.getDrawStyle());
303 separator->addChild(fSoGC.getLightModel());}
304
305 {SoTransform* transform = new SoTransform;
306 G4Lab::Transform3D* t = new G4Lab::Transform3D(*tsf);
307 SbMatrix* matrix = t->getMatrix();
308 transform->setMatrix(*matrix);
309 delete matrix;
310 delete t;
311 separator->addChild(transform);}
312
313 // Build name (for picking) :
314 {std::string s;
315 Lib::smanip::printf(s,128,"%s/0x%lx",fHC.c_str(),(unsigned long)obj);
316 SbName name(s.c_str());
317 //FIXME : can't cache due to the below setName for picking.
318 //FIXME SoPolyhedron* soPolyhedron = fSoGC.getPolyhedron(*polyhedron);
319 SoPolyhedron* soPolyhedron = new SoPolyhedron(*polyhedron);
320 if(fSoGC.getModeling()==SbModeling_wire_frame) {
321 soPolyhedron->solid.setValue(FALSE);
322 //FIXME : handle reduceWireFrame.
323 soPolyhedron->reducedWireFrame.setValue(TRUE);
324 } else {
325 soPolyhedron->solid.setValue(TRUE);
326 }
327 delete polyhedron;
328 soPolyhedron->setName(name);
329 separator->addChild(soPolyhedron);}
330
331 fSoRegion->doIt(SbAddNode(separator,"dynamicScene"));
332}
333//////////////////////////////////////////////////////////////////////////////
334//////////////////////////////////////////////////////////////////////////////
335//////////////////////////////////////////////////////////////////////////////
336G4VHitsCollection* G4Lab::HitsCollectionAccessor::getCollection(
337 Slash::Core::IWriter& aPrinter
338,const std::string& aName
339)
340//////////////////////////////////////////////////////////////////////////////
341//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
342{
343 G4RunManager* runManager = G4RunManager::GetRunManager();
344 if(!runManager) {
345 Lib::Out out(aPrinter);
346 out << "No G4RunManager." << Lib::endl;
347 return 0;
348 }
349 const G4Event* event = runManager->GetCurrentEvent();
350 if(!event) {
351 Lib::Out out(aPrinter);
352 out << "No event." << Lib::endl;
353 return 0;
354 }
355 G4SDManager* sdManager = G4SDManager::GetSDMpointer();
356 if(!sdManager) {
357 Lib::Out out(aPrinter);
358 out << "No G4SDManager." << Lib::endl;
359 return 0;
360 }
361
362 // Get Hit collection of this event
363 G4HCofThisEvent* hce = event->GetHCofThisEvent();
364 if(!hce) {
365 Lib::Out out(aPrinter);
366 out << "No G4HCofThisEvent." << Lib::endl;
367 return 0;
368 }
369 G4int cid = sdManager->GetCollectionID(aName);
370 int nc = hce->GetNumberOfCollections();
371 if((cid<0)||(cid>=nc)) {
372 Lib::Out out(aPrinter);
373 out << "Collection id not found for " << Lib::sout(aName) << Lib::endl;
374 return 0;
375 }
376
377 G4VHitsCollection* hc = hce->GetHC(cid);
378 if(!hce) {
379 Lib::Out out(aPrinter);
380 out << "No G4VHitsCollection for " << Lib::sout(aName) << Lib::endl;
381 return 0;
382 }
383 return hc;
384}
385//////////////////////////////////////////////////////////////////////////////
386G4LogicalVolume* G4Lab::HitsCollectionAccessor::hitLogicalVolume(
387 const G4VHit& aHit
388)
389//////////////////////////////////////////////////////////////////////////////
390//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
391{
392 std::vector<G4AttValue>* vec = aHit.CreateAttValues();
393 if(!vec) return 0;
394 std::string value;
395 unsigned int number = vec->size();
396 for(unsigned int index=0;index<number;index++) {
397 const G4AttValue& val = (*vec)[index];
398 if(val.GetName()=="LV") {
399 std::string s = val.GetValue();
400 void* p;
401 if(!Lib::smanip::topointer(s,p)) return 0;
402 return (G4LogicalVolume*)p; //Beurk
403 }
404 }
405 return 0;
406}
407//////////////////////////////////////////////////////////////////////////////
408G4Transform3D* G4Lab::HitsCollectionAccessor::hitTransform3D(
409 const G4VHit& aHit
410)
411//////////////////////////////////////////////////////////////////////////////
412//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
413{
414 std::vector<G4AttValue>* vec = aHit.CreateAttValues();
415 if(!vec) return 0;
416 std::string value;
417 unsigned int number = vec->size();
418 for(unsigned int index=0;index<number;index++) {
419 const G4AttValue& val = (*vec)[index];
420 if(val.GetName()=="TSF") {
421 std::string s = val.GetValue();
422 void* p;
423 if(!Lib::smanip::topointer(s,p)) return 0;
424 return (G4Transform3D*)p; //Beurk
425 }
426 }
427 return 0;
428}
429//////////////////////////////////////////////////////////////////////////////
430bool G4Lab::HitsCollectionAccessor::hitColor(
431 const G4VHit& aHit
432,G4Colour& aColor
433)
434//////////////////////////////////////////////////////////////////////////////
435//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
436{
437 std::vector<G4AttValue>* vec = aHit.CreateAttValues();
438 if(vec) {
439 std::string value;
440 unsigned int number = vec->size();
441 for(unsigned int index=0;index<number;index++) {
442 const G4AttValue& val = (*vec)[index];
443 if(val.GetName()=="Color") {
444 std::string s = val.GetValue();
445 double r,g,b,a;
446 if(!Lib::smanip::torgba(s,r,g,b,a)) break;
447 aColor = G4Colour(r,g,b,a);
448 return true;
449 }
450 }
451 }
452 aColor = G4Colour();
453 return false;
454}
Note: See TracBrowser for help on using the repository browser.