source: snovis/trunk/source/G4Lab/cxx/DigitsCollectionAccessor.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: 12.7 KB
Line 
1// this :
2#include <G4Lab/DigitsCollectionAccessor.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#include <Inventor/nodes/SoCoordinate3.h>
10
11// HEPVis :
12#include <HEPVis/misc/SoStyleCache.h>
13#include <HEPVis/nodes/SoPolyhedron.h>
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 <G4Event.hh>
23#include <G4DigiManager.hh>
24#include <G4DCofThisEvent.hh>
25#include <G4LogicalVolume.hh>
26#include <G4Transform3D.hh>
27#include <G4AttDef.hh>
28#include <G4AttValue.hh>
29#include <G4Polyhedron.hh>
30#include <G4Colour.hh>
31#include <G4VDigi.hh>
32#include <G4VDigiCollection.hh>
33
34// Lib :
35#include <Slash/Core/ISession.h>
36#include <Slash/Data/IIterator.h>
37#include <Lib/Out.h>
38#include <Lib/Value.h>
39#include <Lib/smanip.h>
40#include <Lib/sout.h>
41
42// G4Lab :
43#include <G4Lab/Transform3D.h>
44#include <G4Lab/Polyhedron.h>
45
46//////////////////////////////////////////////////////////////////////////////
47G4Lab::DigitsCollectionBaseAccessor::DigitsCollectionBaseAccessor(
48 Slash::Core::ISession& aSession
49,G4RunManager* aRunManager
50,const std::string& aDC
51)
52:OnX::InventorAccessor(aSession)
53,fType("DC")
54,fRunManager(aRunManager)
55,fAttDefs(0)
56,fDC(aDC)
57//////////////////////////////////////////////////////////////////////////////
58//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
59{
60  if(fDC!="") fType = fDC;
61}
62//////////////////////////////////////////////////////////////////////////////
63G4Lab::DigitsCollectionBaseAccessor::~DigitsCollectionBaseAccessor(
64) 
65//////////////////////////////////////////////////////////////////////////////
66//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
67{
68}
69//////////////////////////////////////////////////////////////////////////////
70std::string G4Lab::DigitsCollectionBaseAccessor::name(
71) const
72//////////////////////////////////////////////////////////////////////////////
73//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
74{
75  return fType;
76}
77//////////////////////////////////////////////////////////////////////////////
78const std::string& G4Lab::DigitsCollectionBaseAccessor::DCName(
79) const
80//////////////////////////////////////////////////////////////////////////////
81//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
82{
83  return fDC;
84}
85//////////////////////////////////////////////////////////////////////////////
86Slash::Core::IValue* G4Lab::DigitsCollectionBaseAccessor::findValue(
87 Slash::Data::IAccessor::Data aData
88,const std::string& aName
89,void*
90) 
91//////////////////////////////////////////////////////////////////////////////
92//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
93{
94  if(!fAttDefs) {
95    Lib::Out out(printer());
96    out << "G4Lab::DigitsCollectionBaseAccessor::value :" 
97        << " problem to get AttDefs fot digi collection " << Lib::sout(fDC) 
98        << "." 
99        << Lib::endl;
100    return new Lib::Value();
101  }
102
103  G4VDigi* obj = (G4VDigi*)aData; 
104  std::vector<G4AttValue>* vec = obj->CreateAttValues();
105  if(!vec) {
106    Lib::Out out(printer());
107    out << "G4Lab::DigitsCollectionBaseAccessor::value :" 
108        << " problem to get AttValues fot digi collection " << Lib::sout(fDC) 
109        << "." 
110        << Lib::endl;
111    return new Lib::Value();
112  }
113 
114  unsigned int number = vec->size();
115  for(unsigned int index=0;index<number;index++) {
116    const G4AttValue& val = (*vec)[index];
117    if(aName==val.GetName()) {
118      const std::string& stype = (*fAttDefs)[val.GetName()].GetValueType();
119      if(stype=="G4int") {
120        int v;
121        if(!Lib::smanip::toint(val.GetValue(),v)) {
122          Lib::Out out(printer());
123          out << "G4Lab::DigitsCollectionBaseAccessor::value :" 
124              << " " << Lib::sout(val.GetValue()) << " not a G4int." 
125              << Lib::endl;
126          delete vec;
127          return new Lib::Value();
128        }
129        delete vec;
130        return new Lib::Value(v);
131      } else if((stype=="G4double")||(stype=="G4float")) {
132        double v;
133        if(!Lib::smanip::todouble(val.GetValue(),v)) {
134          Lib::Out out(printer());
135          out << "G4Lab::DigitsCollectionBaseAccessor::value :" 
136              << " " << Lib::sout(val.GetValue()) << " not a double." 
137              << Lib::endl;
138          delete vec;
139          return new Lib::Value();
140        }
141        delete vec;
142        return new Lib::Value(v);
143      } else {
144        delete vec;
145        return new Lib::Value(val.GetValue());
146      }
147    }
148  }
149  Lib::Out out(printer());
150  out << "G4Lab::DigitsCollectionBaseAccessor::value :" 
151      << " AttValue not found for property " << Lib::sout(aName) << "." 
152      << Lib::endl;
153  delete vec;
154  return new Lib::Value();
155}
156//////////////////////////////////////////////////////////////////////////////
157void G4Lab::DigitsCollectionBaseAccessor::beginVisualize(
158) 
159//////////////////////////////////////////////////////////////////////////////
160//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
161{
162  OnX::InventorAccessor::beginVisualize();
163
164  std::string style = "DC("+fDC+")";
165  if(!isStyle(style)) style = "DC"; //Default.
166  fillSoGC(style);
167}
168//////////////////////////////////////////////////////////////////////////////
169void G4Lab::DigitsCollectionBaseAccessor::visualize(
170 Slash::Data::IAccessor::Data aData
171,void*
172) 
173//////////////////////////////////////////////////////////////////////////////
174// The digi must have the "LV" and "TSF" AttDef.
175//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
176{
177  G4VDigi* obj = (G4VDigi*)aData; 
178  G4LogicalVolume* lv = digiLogicalVolume(*obj);
179  if(!lv) return;
180  G4Transform3D* tsf = digiTransform3D(*obj);
181  if(!tsf) return;
182  G4VSolid* solid = lv->GetSolid();
183  if(!solid) return;
184  //G4Material* material = lv->GetMaterial();
185
186  int old_nstep = HepPolyhedron::GetNumberOfRotationSteps();
187  HepPolyhedron::SetNumberOfRotationSteps(fSoGC.getRotationSteps());
188  G4Polyhedron* g4Polyhedron = solid->CreatePolyhedron();
189  HepPolyhedron::SetNumberOfRotationSteps(old_nstep);
190  if(!g4Polyhedron) return;
191
192  G4Lab::Polyhedron* polyhedron = new G4Lab::Polyhedron(*g4Polyhedron);
193  delete g4Polyhedron;
194  if(!polyhedron) return;
195
196  SoSeparator* separator = new SoSeparator;
197  separator->setName("sceneGraph");
198     
199  G4Colour color;
200  if(digiColor(*obj,color)) {
201    SbColor sbColor((float)color.GetRed(),
202                    (float)color.GetGreen(),
203                    (float)color.GetBlue());
204    float transp = 1.0F - (float)color.GetAlpha();
205    SoStyleCache* styleCache = fSoGC.getStyleCache();
206    separator->addChild(
207      styleCache->getHighlightMaterial
208        (sbColor,fSoGC.getHighlightColor(),transp));
209  } else {
210    separator->addChild(fSoGC.getHighlightMaterial());
211  }
212
213  separator->addChild(fSoGC.getDrawStyle());
214  separator->addChild(fSoGC.getLightModel());
215
216  SoTransform* transform = new SoTransform;
217  G4Lab::Transform3D* t = new G4Lab::Transform3D(*tsf);
218  SbMatrix* matrix = t->getMatrix();
219  transform->setMatrix(*matrix);
220  delete matrix;
221  delete t;
222  separator->addChild(transform);
223         
224  // Build name (for picking) :
225  std::string s;
226  Lib::smanip::printf(s,128,"%s/0x%lx",fDC.c_str(),(unsigned long)obj);
227  SbName name(s.c_str());
228  //FIXME : can't cache due to the below setName for picking.
229  //FIXME SoPolyhedron* soPolyhedron = fSoGC.getPolyhedron(*polyhedron);
230  SoPolyhedron* soPolyhedron = new SoPolyhedron(*polyhedron);
231  if(fSoGC.getModeling()==SbModeling_wire_frame) {
232    soPolyhedron->solid.setValue(FALSE);
233    //FIXME : handle reduceWireFrame.
234    soPolyhedron->reducedWireFrame.setValue(TRUE);
235  } else {
236    soPolyhedron->solid.setValue(TRUE);
237  }
238  delete polyhedron;
239  soPolyhedron->setName(name);
240       
241  separator->addChild(soPolyhedron);
242         
243  fSoRegion->doIt(SbAddNode(separator,"dynamicScene"));
244}
245//////////////////////////////////////////////////////////////////////////////
246void G4Lab::DigitsCollectionBaseAccessor::setProperties(
247 G4VDigi& aDigi
248) 
249//////////////////////////////////////////////////////////////////////////////
250//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
251{
252  fAttDefs = (std::map<G4String,G4AttDef>*)aDigi.GetAttDefs();
253  if(!fAttDefs) return;
254  std::map<G4String,G4AttDef>::const_iterator it;
255  for(it=fAttDefs->begin();it!=fAttDefs->end();++it) {
256    const std::string& sname = (*it).second.GetName();
257    if((sname!="LV") && 
258       (sname!="TSF") && 
259       (sname!="Color")) {
260      const std::string& stype = (*it).second.GetValueType();   
261      if(stype=="G4int") {
262        addProperty((*it).first,Lib::Property::INTEGER);
263      } else if((stype=="G4double")||(stype=="G4float")) {
264        addProperty((*it).first,Lib::Property::DOUBLE);
265      } else {
266        addProperty((*it).first,Lib::Property::STRING);
267      }
268    }
269  }
270}
271//////////////////////////////////////////////////////////////////////////////
272//////////////////////////////////////////////////////////////////////////////
273//////////////////////////////////////////////////////////////////////////////
274G4VDigiCollection* G4Lab::DigitsCollectionBaseAccessor::getCollection(
275 Slash::Core::IWriter& aPrinter
276,const std::string& aName
277) 
278//////////////////////////////////////////////////////////////////////////////
279//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
280{
281  if(!fRunManager) {
282    Lib::Out out(aPrinter);
283    out << "No G4RunManager." << Lib::endl;
284    return 0;
285  }
286  const G4Event* event = fRunManager->GetCurrentEvent();
287  if(!event) {
288    Lib::Out out(aPrinter);
289    out << "No event." << Lib::endl;
290    return 0;
291  }
292  G4DigiManager* digiManager = G4DigiManager::GetDMpointer();
293  if(!digiManager) {
294    Lib::Out out(aPrinter);
295    out << "No G4DigiManager." << Lib::endl;
296    return 0;
297  }
298
299  // Get Digi collection of this event
300  G4DCofThisEvent* dce  = event->GetDCofThisEvent();
301  if(!dce) {
302    Lib::Out out(aPrinter);
303    out << "No G4DCofThisEvent." << Lib::endl;
304    return 0;
305  }
306  G4int cid = digiManager->GetDigiCollectionID(aName);
307  int nc = dce->GetNumberOfCollections();
308  if((cid<0)||(cid>=nc)) {
309    Lib::Out out(aPrinter);
310    out << "Collection id not found for " << Lib::sout(aName) << Lib::endl;
311    return 0;
312  }
313
314  G4VDigiCollection* dc = dce->GetDC(cid);
315  if(!dce) {
316    Lib::Out out(aPrinter);
317    out << "No G4VDigiCollection for " << Lib::sout(aName) << Lib::endl;
318    return 0;
319  }
320  return dc;
321}
322//////////////////////////////////////////////////////////////////////////////
323G4LogicalVolume* G4Lab::DigitsCollectionBaseAccessor::digiLogicalVolume(
324 const G4VDigi& aDigi
325) 
326//////////////////////////////////////////////////////////////////////////////
327//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
328{
329  std::vector<G4AttValue>* vec = aDigi.CreateAttValues();
330  if(!vec) return 0;
331  std::string value;
332  unsigned int number = vec->size();
333  for(unsigned int index=0;index<number;index++) {
334    const G4AttValue& val = (*vec)[index];
335    if(val.GetName()=="LV") {
336      std::string s = val.GetValue();
337      void* p;
338      if(!Lib::smanip::topointer(s,p)) return 0;
339      return (G4LogicalVolume*)p; //Beurk
340    }
341  }
342  return 0;
343}
344//////////////////////////////////////////////////////////////////////////////
345G4Transform3D* G4Lab::DigitsCollectionBaseAccessor::digiTransform3D(
346 const G4VDigi& aDigi
347) 
348//////////////////////////////////////////////////////////////////////////////
349//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
350{
351  std::vector<G4AttValue>* vec = aDigi.CreateAttValues();
352  if(!vec) return 0;
353  std::string value;
354  unsigned int number = vec->size();
355  for(unsigned int index=0;index<number;index++) {
356    const G4AttValue& val = (*vec)[index];
357    if(val.GetName()=="TSF") {
358      std::string s = val.GetValue();
359      void* p;
360      if(!Lib::smanip::topointer(s,p)) return 0;
361      return (G4Transform3D*)p; //Beurk
362    }
363  }
364  return 0;
365}
366//////////////////////////////////////////////////////////////////////////////
367bool G4Lab::DigitsCollectionBaseAccessor::digiColor(
368 const G4VDigi& aDigi
369,G4Colour& aColor
370) 
371//////////////////////////////////////////////////////////////////////////////
372//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
373{ 
374  std::vector<G4AttValue>* vec = aDigi.CreateAttValues();
375  if(vec) {
376    std::string value;
377    unsigned int number = vec->size();
378    for(unsigned int index=0;index<number;index++) {
379      const G4AttValue& val = (*vec)[index];
380      if(val.GetName()=="Color") {
381        std::string s = val.GetValue();
382        double r,g,b,a;
383        if(!Lib::smanip::torgba(s,r,g,b,a)) break;
384        aColor = G4Colour(r,g,b,a);
385        return true;
386      }
387    }
388  }
389  aColor = G4Colour();
390  return false;
391}
Note: See TracBrowser for help on using the repository browser.