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

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