MuonCounterViewer.cc
Go to the documentation of this file.
1 #include "MuonCounterViewer.h"
2 
3 #include <evt/Event.h>
4 #include <det/Detector.h>
5 #include <mdet/MDetector.h>
6 #include <mdet/Counter.h>
7 #include <mdet/Module.h>
8 #include <mdet/Scintillator.h>
9 #include <sdet/Station.h>
10 #include <utl/TimeStamp.h>
11 #include <utl/AugerUnits.h>
12 #include <utl/Point.h>
13 #include <utl/CoordinateSystemPtr.h>
14 #include <utl/ErrorLogger.h>
15 #include <utl/TransformationMatrix.h>
16 #include <utl/Transformation.h>
17 #include <utl/MathConstants.h>
18 #include <utl/Line.h>
19 #include <fwk/LocalCoordinateSystem.h>
20 #include <fwk/CentralConfig.h>
21 
22 #include <sstream>
23 #include <memory>
24 #include <ctime>
25 #include <fstream>
26 
27 #include <TGeoManager.h>
28 #include <TGeoMaterial.h>
29 #include <TGeoMedium.h>
30 #include <TGeoMatrix.h>
31 #include <TGeoVolume.h>
32 #include <TVirtualGeoPainter.h>
33 
34 using namespace std;
35 using namespace utl;
36 using namespace fwk;
37 
38 
39 namespace MuonCounterViewerAG {
40 
41 
42  void
43  ScintBuilder::Box(const mdet::Scintillator& s, double w, double l, double h)
44  {
45  // A new scint name.
46  std::ostringstream iStrScint;
47  iStrScint << "scint"
48  << s.GetId() << "_"
49  << s.GetModule().GetId() << "_"
50  << s.GetModule().GetCounter().GetId();
51  // Keep the reference.
52  fVolume = fManager->MakeBox(iStrScint.str().c_str(), fMedium, w / utl::m / 2 , l / utl::m / 2, h / utl::m / 2);
53  }
54 
55 
58  {
59  det::Detector::GetInstance().Update(GetCurrentSystemTime());
60  const Branch topB = CentralConfig::GetInstance()->GetTopBranch("MuonCounterViewer");
61  topB.GetChild("CounterId").GetData(counterId);
62  return eSuccess;
63  }
64 
66  MuonCounterViewer::Run(evt::Event& /*event*/)
67  {
68  const mdet::MDetector& d = det::Detector::GetInstance().GetMDetector();
72  auto_ptr<TGeoManager> geom(new TGeoManager("counter", "A muon counter."));
73  TGeoMaterial* matVacuum = new TGeoMaterial("Vacuum", 0,0,0);
74  TGeoMedium* vacuum = new TGeoMedium("Vacuum",1, matVacuum);
75  // For constructing Scintillator volumes
76  ScintBuilder builder;
77  // XXX Consider to represent the hierarchy also here...
78  // Make a box for the top:
79  TGeoVolume *top = geom->MakeBox("TOP", vacuum, 1, 1, 1);
80  geom->SetTopVolume(top);
81 
82  for (Itc ic = d.AllCountersBegin(); ic != d.AllCountersEnd(); ++ic) {
83  const mdet::Counter& c = *ic;
84 
85  if (ic->GetId() != counterId)
86  continue;
87 
88  // Ground.
89  const double groundHeight = 1 * utl::cm;
90  TGeoVolume* ground = geom->MakeBox("ground", vacuum, 10 * utl::m , 10 * utl::m, groundHeight / 2);
91  TGeoTranslation* transGround = new TGeoTranslation(0,0,
93  ground->SetLineColor(kOrange-1);
94  top->AddNode(ground, 1, transGround);
95  // Build tags.
96  std::ostringstream iStrCounter;
97  iStrCounter<< "counter" << c.GetId();
98  TGeoVolume* cCenter = geom->MakeSphere(iStrCounter.str().c_str(),vacuum, 10*utl::cm,12*utl::cm);
99  // No need to rotate, as where putting an sphere.
100  TGeoTranslation* transCounter = new TGeoTranslation(c.GetPosition().GetX(c.GetLocalCoordinateSystem()) / utl::m,
103  cCenter->SetLineColor(kBlack);
104  top->AddNode(cCenter, 1, transCounter);
105  // Tank.
106  TGeoVolume* tank = geom->MakeTube("tank", vacuum, 0,
108  c.GetAssociatedTank().GetHeight() / utl::m / 2);
109  TGeoTranslation* transTank = new TGeoTranslation(
112  c.GetAssociatedTank().GetHeight() / utl::m / 2 +
114  tank->SetLineColor(41);
115  top->AddNode(tank, 1, transTank);
116  for (Itm im = c.ModulesBegin(); im != c.ModulesEnd(); ++im) {
117  const mdet::Module& m = *im;
118  std::ostringstream iStrModule;
119  iStrModule << "module" << m.GetId() << "_" << c.GetId();
120  TGeoVolume* mCenter = geom->MakeSphere(iStrModule.str().c_str(),vacuum, 10*utl::cm,12*utl::cm);
121  // No need to rotate, as we're putting an sphere.
122  TGeoTranslation* transModule = new TGeoTranslation(m.GetPosition().GetX(c.GetLocalCoordinateSystem()) / utl::m,
125  mCenter->SetLineColor(kViolet);
126  top->AddNode(mCenter, 1, transModule);
127  for (Its is = m.ScintillatorsBegin(); is != m.ScintillatorsEnd(); ++is) {
128  const mdet::Scintillator& s = *is;
129  // Scintillator.
130  utl::Point sCenter(0, 0, 0, s.GetLocalCoordinateSystem());
131  double xxx = sCenter.GetX(c.GetLocalCoordinateSystem());
132  double yyy = sCenter.GetY(c.GetLocalCoordinateSystem());
133  double zzz = sCenter.GetZ(c.GetLocalCoordinateSystem());
134  // Find the rotation from scin to counter reference frame.
136  ->GetTransformationTo(c.GetLocalCoordinateSystem()).Decompose().get<1>();
137  // Construct the intermediate object to perform "active" transformation.
139  // Since there's no matrix project we build a local basis...
140  utl::Point xUnit(1, 0, 0, s.GetLocalCoordinateSystem());
141  utl::Point yUnit(0, 1, 0, s.GetLocalCoordinateSystem());
142  utl::Point zUnit(0, 0, 1, s.GetLocalCoordinateSystem());
143  // ... rotate the vectors to the counter system.
144  xUnit = scintPol * xUnit;
145  yUnit = scintPol * yUnit;
146  zUnit = scintPol * zUnit;
147  // Plain'ol vector for Root.
148  double rotMatrix[9];
149  // So that by querying the resulting compoenents (in the same system)
150  // we can retrieve the matrix.
151  rotMatrix [ 0 ] = xUnit.GetX(s.GetLocalCoordinateSystem());
152  rotMatrix [ 1 ] = yUnit.GetX(s.GetLocalCoordinateSystem());
153  rotMatrix [ 2 ] = zUnit.GetX(s.GetLocalCoordinateSystem());
154  rotMatrix [ 3 ] = xUnit.GetY(s.GetLocalCoordinateSystem());
155  rotMatrix [ 4 ] = yUnit.GetY(s.GetLocalCoordinateSystem());
156  rotMatrix [ 5 ] = zUnit.GetY(s.GetLocalCoordinateSystem());
157  rotMatrix [ 6 ] = xUnit.GetZ(s.GetLocalCoordinateSystem());
158  rotMatrix [ 7 ] = yUnit.GetZ(s.GetLocalCoordinateSystem());
159  rotMatrix [ 8 ] = zUnit.GetZ(s.GetLocalCoordinateSystem());
160  // So build the "rootation".
161  TGeoRotation* rot = new TGeoRotation();
162  rot->SetMatrix(rotMatrix);
163  /*
164  * "The rotation won't be owned" tells the doxy from the constructor of
165  * TGeoCombiTrans: so register it.
166  * In other parts of the docs (see TGeoMatrix): "unless explicitly used
167  * for positioning nodes (TGeoVolume::AddNode()) all
168  * matrices deletion have to be managed by users. Matrices passed to geometry
169  * have to be created by using new() operator and their deletion is done by
170  * TGeoManager class".
171  *
172  * The use of an stack variable for rot was used, but it led to a seg-fault:
173  * it isn't owned but may be pointed: this seems to be the case, see
174  * void TGeoCombiTrans::SetRotation(const TGeoRotation *rot)
175  * which is the method called by the constructor.
176  */
177  rot->RegisterYourself();
178  // Compose the translation with the rotation.
179  // Build the box with its dimensions.
180  TGeoVolume* v = s.VisitShape(builder(geom.get(), vacuum)).GetVolume();
181  // Some colors for the Scint, due to its module tag.
182  switch(m.GetAreaKind()) {
184  v->SetLineColor(kRed);
185  break;
187  v->SetLineColor(kBlue);
188  }
189  top->AddNode(v, 1, new TGeoCombiTrans(xxx / utl::m, yyy / utl::m, zzz / utl::m, rot));
190  }
191  }
192  //Only one counter, no more.
193  //break;
194  }
195 
196  /*
197  * With this line the "triggering" initial Draw is added automatically
198  * to the script.
199  */
200  geom->GetGeomPainter()->SetTopVolume(top);
201  /*
202  * Give the script as output.
203  * It seems that it isn't neccesary to
204  * "close" the geometry.
205  * See the following commented call to this function for more info.
206  *
207  * Enclose the dumped code in a method.
208  * I just tried in root-5.24 and note that the output of SavePrimitive method
209  * uses some variales without prior declaration. If the script is used directly,
210  * that gives problems (just because of the lack of declaration) but if the script
211  * is run inside a methos, it is just fine.
212  */
213 
214  std::ostringstream iStrFile;
215  iStrFile << "CounterViewer_" << counterId << ".C";
216  ofstream fout(iStrFile.str().c_str());
217  fout << "void CounterViewer_" << counterId << "() {" << endl;
218  top->SavePrimitive(fout);
219  fout << "}" << endl;
220 
221  /*
222  * This call, in certain oportunities, gave a problem during Voxelizing: Wooops!!! appears in
223  * standard-output:
224  * /lib/tls/i686/cmov/libc.so.6[0xb0ce53f4]
225  * /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb0e98031]
226  * /usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb0e9808d]
227  * /usr/local/auger/External/ROOT/pro/lib/libGeom.so(_ZN15TGeoVoxelFinder7SortAllEPKc+0x6f1)[0xb0fede31]
228  * /usr/local/auger/External/ROOT/pro/lib/libGeom.so(_ZN15TGeoVoxelFinder8VoxelizeEPKc+0x3a)[0xb0fe7b5a]
229  * /usr/local/auger/External/ROOT/pro/lib/libGeom.so(_ZN10TGeoVolume8VoxelizeEPKc+0x188)[0xb0fdf5e8]
230  * /usr/local/auger/External/ROOT/pro/lib/libGeom.so(_ZN11TGeoManager8VoxelizeEPKc+0xb1)[0xb0f849e1]
231  * /usr/local/auger/External/ROOT/pro/lib/libGeom.so(_ZN11TGeoManager13CloseGeometryEPKc+0x1cc)[0xb0f84c1c]
232  * ./userAugerOffline(_ZN19MuonCounterViewerAG17MuonCounterViewer3RunERN3evt5EventE+0x2315)[0x8059295]
233  *
234  * This problem couldn't be isolated and tested with Root 5.20.
235  *
236  * geom->CloseGeometry();
237  */
238  /*
239  * This call works but afterwards the program gives seg-fault in root 5.16. Not in 5.20.
240  * Both of course working in my box with Ubuntu 7.10.
241  *
242  * #0 0x08be481c in ?? ()
243  * #1 0xb2b627da in TCollection::GarbageCollect () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
244  * #2 0xb2b6aefb in TObjArray::Delete () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
245  * #3 0xb0f74832 in TGeoManager::~TGeoManager () from /usr/local/auger/External/ROOT/pro/lib/libGeom.so
246  * #4 0xb2b627da in TCollection::GarbageCollect () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
247  * #5 0xb2b67d66 in TList::Delete () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
248  * #6 0xb2b20d23 in TROOT::~TROOT () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
249  * #7 0xb2b20e70 in __tcf_0 () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
250  * #8 0xb0c91101 in __cxa_finalize () from /lib/tls/i686/cmov/libc.so.6
251  * #9 0xb2ae0693 in __do_global_dtors_aux () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
252  * #10 0xb2f11d7c in _fini () from /usr/local/auger/External/ROOT/pro/lib/libCore.so
253  * #11 0xb7f82153 in ?? () from /lib/ld-linux.so.2
254  * #12 0xb7f90260 in _rtld_global () from /lib/ld-linux.so.2
255  * #13 0x00000000 in ?? ()
256  *
257  * top->SavePrimitive(cout);
258  */
259  return eSuccess;
260  }
261 
262 
264  MuonCounterViewer::Finish()
265  {
266  cout << "\n\n******************\n";
267  INFO("ROOT macro script created");
268 
269  std::ostringstream iStrMessage;
270  iStrMessage << "From command line, execute: root CounterViewer_" << counterId << ".C";
271  INFO(iStrMessage.str().c_str());
272  cout << "n******************\n\n";
273 
274  return eSuccess;
275  }
276 
277 }
Branch GetTopBranch() const
Definition: Branch.cc:63
Point object.
Definition: Point.h:32
const Counter & GetCounter() const
The parent counter.
AreaKind GetAreaKind() const
bool is(const double a, const double b)
Definition: testlib.cc:113
utl::CoordinateSystemPtr GetLocalCoordinateSystem() const
Local system based on position and configured rotations.
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
ScintillatorConstIterator ScintillatorsBegin() const
Begin iterator over the contained scitillators.
utl::Point GetPosition() const
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
ModuleConstIterator ModulesEnd() const
Begin iterator for the Modules contained in the Counter.
Detector associated to muon detector hierarchy.
Definition: MDetector.h:32
utl::Point GetPosition() const
Tank position.
Transformations matrices for afine transformations.
Actual muon-sensitive objects.
Class representing a document branch.
Definition: Branch.h:107
constexpr double s
Definition: AugerUnits.h:163
double GetX(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:206
Array of Scintillator.
V & VisitShape(V &v) const
Callback method for inspecting shape-aware properties.
double GetRadius() const
Radius of the tank (water only)
double GetHeight() const
Height of the tank (water only)
CounterConstIterator AllCountersBegin() const
Begin iterator over all the counters.
Definition: MDetector.h:80
ScintillatorGroup::ConstIterator ScintillatorConstIterator
Scintillator handling.
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
Root detector of the muon detector hierarchy.
CounterGroup::ConstIterator CounterConstIterator
Defines a more meaningful (and shorter) type for iterators.
Definition: MDetector.h:44
double GetY(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:209
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
CounterConstIterator AllCountersEnd() const
End iterator over all the counters.
Definition: MDetector.h:86
ScintillatorConstIterator ScintillatorsEnd() const
End iterator over the contained scintillators.
const Module & GetModule() const
Retrieve the parent mdet::Module.
constexpr double cm
Definition: AugerUnits.h:117
ModuleConstIterator ModulesBegin() const
Begin iterator for the Modules contained in the Counter.
TimeStamp GetCurrentSystemTime()
get current time as reported by system
Definition: TimeStamp.cc:46
Scint shape-aware builder.
const sdet::Station & GetAssociatedTank() const
Retrieve the associated tank.
ModuleGroup::ConstIterator ModuleConstIterator
Convenience typedef for const iterator over the contained Module instances.
int GetId() const
The id of this component.
double GetZ(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:212
constexpr double m
Definition: AugerUnits.h:121

, generated on Tue Sep 26 2023.