]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPainterHelper.cxx
Make the exploded transformations on-demand only
[u/mrichter/AliRoot.git] / MUON / AliMUONPainterHelper.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 #include <cstdlib>
19 #include "AliMUONPainterHelper.h"
20
21 #include "AliCodeTimer.h"
22 #include "AliLog.h"
23 #include "AliMUONContour.h"
24 #include "AliMUONContourHandler.h"
25 #include "AliMUONContourMaker.h"
26 #include "AliMUONGeometryDetElement.h"
27 #include "AliMUONGeometryTransformer.h"
28 #include "AliMUONManuContourMaker.h"
29 #include "AliMUONPainterEnv.h"
30 #include "AliMUONPainterMatrix.h"
31 #include "AliMUONPainterRegistry.h"
32 #include "AliMUONPadStatusMaker.h"
33 #include "AliMUONVCalibParam.h"
34 #include "AliMUONVDigit.h"
35 #include "AliMUONVTrackerData.h"
36 #include "AliMpCDB.h"
37 #include "AliMpConstants.h"
38 #include "AliMpDDLStore.h"
39 #include "AliMpDEIterator.h"
40 #include "AliMpDEManager.h"
41 #include "AliMpExMap.h"
42 #include "AliMpMotifMap.h"
43 #include "AliMpMotifPosition.h"
44 #include "AliMpPCB.h"
45 #include "AliMpPad.h"
46 #include "AliMpSector.h"
47 #include "AliMpSegmentation.h"
48 #include "AliMpSlat.h"
49 #include "AliMpStationType.h"
50 #include "AliMpVPadIterator.h"
51 #include <Riostream.h>
52 #include <TArrayI.h>
53 #include <TCanvas.h>
54 #include <TClass.h>
55 #include <TCollection.h>
56 #include <TFile.h>
57 #include <TGLabel.h>
58 #include <TGMsgBox.h>
59 #include <TGeoMatrix.h>
60 #include <TLine.h>
61 #include <TList.h>
62 #include <TMap.h>
63 #include <TObjArray.h>
64 #include <TObjString.h>
65 #include <TStyle.h>
66 #include <TSystem.h>
67 #include <TVector2.h>
68 #include <TVector3.h>
69 #include <TVirtualPad.h>
70 #include <TVirtualX.h>
71
72 #include "AliMUONChamberPainter.h"
73
74 ///\class AliMUONPainterHelper
75 ///
76 /// Helper class for painters
77 ///
78 ///\author Laurent Aphecetche, Subatech
79
80 ///\cond CLASSIMP
81 ClassImp(AliMUONPainterHelper)
82 ///\endcond
83
84 AliMUONPainterHelper* AliMUONPainterHelper::fgInstance(0x0);
85
86 //_____________________________________________________________________________
87 AliMUONPainterHelper::AliMUONPainterHelper() : 
88 TObject(),
89 fPainterMatrices(0x0),
90 fEnv(0x0),
91 fReal(0x0),
92 fExploded(0x0)
93 {
94   /// ctor
95   
96   if ( ! AliMpCDB::LoadMpSegmentation() ) 
97   {
98     AliFatal("Could not access mapping from OCDB !");
99   }
100   
101   // Load DDL store
102   if ( ! AliMpCDB::LoadDDLStore() ) 
103   {
104     AliFatal("Could not access DDL Store from OCDB !");
105   }  
106 }
107
108 //_____________________________________________________________________________
109 AliMUONPainterHelper::~AliMUONPainterHelper()
110 {
111   /// dtor
112   delete fPainterMatrices;
113   delete fReal;
114   delete fExploded;
115   fEnv->Save();
116   fgInstance = 0;
117 }
118
119 //_____________________________________________________________________________
120 AliMUONContourHandler*
121 AliMUONPainterHelper::Exploded() const
122 {
123   /// Create exploded contour handler
124   if (!fExploded) fExploded = new AliMUONContourHandler(kTRUE);
125   return fExploded;
126 }
127
128 //_____________________________________________________________________________
129 AliMUONContourHandler*
130 AliMUONPainterHelper::Real() const
131 {
132   /// Create real contour handler
133   if (!fReal) fReal = new AliMUONContourHandler(kFALSE);
134   return fReal;
135 }
136
137 //_____________________________________________________________________________
138 AliMUONContour*
139 AliMUONPainterHelper::GetContour(const char* contourName, Bool_t explodedView) const
140 {
141   /// Get a contour by name  
142   if (explodedView) 
143   {
144     return Exploded()->GetContour(contourName);
145   }
146   else
147   {
148     if ( fReal ) 
149     {
150       return fReal->GetContour(contourName);
151     }
152   }
153   return 0x0;
154 }
155
156 //_____________________________________________________________________________
157 void
158 AliMUONPainterHelper::GenerateDefaultMatrices()
159 {
160   /// Kind of bootstrap method to trigger the generation of all contours
161   
162   AliCodeTimerAuto("",0);
163   
164   fPainterMatrices = new TObjArray;
165   fPainterMatrices->SetOwner(kFALSE);
166   
167   TObjArray attributes;
168   
169   AliMUONAttPainter att;
170   
171   att.SetViewPoint(kTRUE,kFALSE);
172   att.SetCathode(kFALSE,kFALSE);
173   att.SetPlane(kTRUE,kFALSE);
174   attributes.Add(new AliMUONAttPainter(att));
175
176   TIter next(&attributes);
177   AliMUONAttPainter* a;
178   
179   while ( ( a = static_cast<AliMUONAttPainter*>(next()) ) )
180   {
181     AliMUONPainterMatrix* matrix = new AliMUONPainterMatrix("Tracker",5,2);
182     
183     for ( Int_t i = 0; i < 10; ++i )
184     {
185       AliMUONVPainter* painter = new AliMUONChamberPainter(*a,i);
186       
187       painter->SetResponder("Chamber");
188       
189       painter->SetOutlined("*",kFALSE);
190       
191       painter->SetOutlined("MANU",kTRUE);
192       
193       for ( Int_t j = 0; j < 3; ++j ) 
194       {
195         painter->SetLine(j,1,4-j);
196       }
197       
198       matrix->Adopt(painter);    
199     }
200     AliMUONPainterRegistry::Instance()->Register(matrix);
201     fPainterMatrices->Add(matrix);
202   }
203 }
204
205 //_____________________________________________________________________________
206 AliMp::CathodType
207 AliMUONPainterHelper::GetCathodeType(Int_t detElemId, Int_t manuId) const
208 {
209   /// Get the cathode type of a given manu
210   
211   AliMp::PlaneType planeType(AliMp::kBendingPlane);
212   if ( manuId & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) )
213   {
214     planeType = AliMp::kNonBendingPlane;
215   }
216   return AliMpDEManager::GetCathod(detElemId,planeType);
217 }
218
219
220 //_____________________________________________________________________________
221 AliMpMotifPosition* 
222 AliMUONPainterHelper::GetMotifPosition(Int_t detElemId, Int_t manuId) const
223 {
224   /// Get a given motif position
225   const AliMpVSegmentation* vseg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
226   if (vseg)
227   {
228     return vseg->MotifPosition(manuId);
229   }
230   return 0x0;
231 }
232
233 //_____________________________________________________________________________
234 AliMpPCB*
235 AliMUONPainterHelper::GetPCB(Int_t detElemId, AliMp::CathodType cathodeType, 
236                              Int_t pcbNumber) const
237 {
238   /// Get a given PCB
239   const AliMpSlat* slat = GetSlat(detElemId,cathodeType);
240   if ( slat ) return slat->GetPCB(pcbNumber);
241   return 0x0;
242 }
243
244 //_____________________________________________________________________________
245 AliMpPCB*
246 AliMUONPainterHelper::GetPCB(Int_t detElemId, AliMp::PlaneType planeType, 
247                              Int_t pcbNumber) const
248 {
249   /// Get a given PCB
250   AliMp::CathodType cathodeType = AliMpDEManager::GetCathod(detElemId,
251                                                             planeType);
252   return GetPCB(detElemId,cathodeType,pcbNumber);
253 }
254
255 //_____________________________________________________________________________
256 AliMp::PlaneType
257 AliMUONPainterHelper::GetPlaneType(Int_t manuId) const
258 {
259   /// Get the planeType of a given manu
260   
261   if ( manuId & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) )
262   {
263     return AliMp::kNonBendingPlane;
264   }
265   return AliMp::kBendingPlane;
266 }
267
268 //_____________________________________________________________________________
269 const AliMpSlat*
270 AliMUONPainterHelper::GetSlat(Int_t detElemId, AliMp::PlaneType planeType) const
271 {
272   /// Get a given slat
273   
274   AliMp::CathodType cathodeType = AliMpDEManager::GetCathod(detElemId,
275                                                           planeType);
276
277   return GetSlat(detElemId,cathodeType);
278 }
279
280 //_____________________________________________________________________________
281 const AliMpSector*
282 AliMUONPainterHelper::GetSector(Int_t detElemId, AliMp::PlaneType planeType) const
283 {
284   /// Get a given sector
285   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
286   if ( stationType != AliMp::kStation12 ) return 0x0;
287   
288   AliMp::CathodType cathodeType = AliMpDEManager::GetCathod(detElemId,planeType);
289   
290   return AliMpSegmentation::Instance()->GetSector(detElemId,cathodeType);
291 }
292
293 //_____________________________________________________________________________
294 const AliMpSlat*
295 AliMUONPainterHelper::GetSlat(Int_t detElemId, AliMp::CathodType cathodeType) const
296 {
297   /// Get a given slat
298   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
299   if ( stationType != AliMp::kStation345 ) return 0x0;
300
301   return AliMpSegmentation::Instance()->GetSlat(detElemId,cathodeType);
302 }
303
304 //_____________________________________________________________________________
305 const AliMpSlat*
306 AliMUONPainterHelper::GetSlat(Int_t detElemId, Int_t manuId) const
307 {
308   /// Get a given slat
309   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
310   if ( stationType != AliMp::kStation345 ) return 0x0;
311
312   return AliMpSegmentation::Instance()->GetSlatByElectronics(detElemId,manuId);
313 }
314
315 //_____________________________________________________________________________
316 AliMUONPainterHelper*
317 AliMUONPainterHelper::Instance()
318 {
319   /// Return the global and unique instance of this class
320   
321   if (fgInstance) return fgInstance;
322
323   AliCodeTimerAutoClass("",0);
324
325   fgInstance = new AliMUONPainterHelper;
326   fgInstance->fEnv = new AliMUONPainterEnv;
327   return fgInstance;
328 }
329
330 //_____________________________________________________________________________
331 void 
332 AliMUONPainterHelper::Global2Local(Int_t detElemId, 
333                                     Double_t xg, Double_t yg, Double_t zg,
334                                     Double_t& xl, Double_t& yl, Double_t& zl) const
335 {
336   /// Local to global transformation of coordinates
337   
338   TGeoHMatrix* matrix = static_cast<TGeoHMatrix*>(Exploded()->GetTransformations()->GetValue(detElemId));
339   Double_t pg[3] = { xg, yg, zg };
340   Double_t pl[3] = { 0., 0., 0. };
341   matrix->MasterToLocal(pg, pl);
342   xl = pl[0];
343   yl = pl[1];
344   zl = pl[2];
345 }
346
347 //_____________________________________________________________________________
348 void 
349 AliMUONPainterHelper::Local2Global(Int_t detElemId, 
350                                    Double_t xl, Double_t yl, Double_t zl,
351                                    Double_t& xg, Double_t& yg, Double_t& zg) const
352 {
353   /// Local to (exploded) global transformation of coordinates
354   
355   TGeoHMatrix* matrix = static_cast<TGeoHMatrix*>(Exploded()->GetTransformations()->GetValue(detElemId));
356   Double_t pl[3] = { xl, yl, zl };
357   Double_t pg[3] = { 0., 0., 0. };
358   matrix->LocalToMaster(pl, pg);
359   xg = pg[0];
360   yg = pg[1];
361   zg = pg[2];
362 }
363
364 //_____________________________________________________________________________
365 Int_t
366 AliMUONPainterHelper::ColorFromValue(Double_t value, Double_t min, Double_t max) const
367
368   /// Convert a value into a color, fitting within a given range
369   
370   Int_t rv;
371   
372   if (value > max) rv = 1;
373   else if (value <= min) rv = 0;
374   else
375   {
376     if  ( max == min ) return gStyle->GetColorPalette(1);
377     Double_t range = max - min;
378     Double_t offset = value - min;
379     rv = gStyle->GetColorPalette( 1 + int( offset*(gStyle->GetNumberOfColors()-2)/range - 0.5 ) );
380   }
381   return rv;
382 }
383
384 //_____________________________________________________________________________
385 AliMUONContour* 
386 AliMUONPainterHelper::MergeContours(const TObjArray& contours, const char* contourName, Bool_t explodedGeometry)
387 {
388   /// Merge a set of contours (delegating to the contour maker)
389   
390   AliMUONContourMaker maker;
391   
392   AliMUONContour* contour = maker.MergeContour(contours,contourName);
393   
394   if (contour) 
395   {
396     RegisterContour(contour,explodedGeometry);
397   }
398   return contour;
399 }
400
401
402 //_____________________________________________________________________________
403 void
404 AliMUONPainterHelper::Print(Option_t* opt) const
405 {
406   /// Printout
407   TString sopt(opt);
408   sopt.ToUpper();
409   
410   if ( sopt.Length() == 0 )
411   {
412     if ( fExploded ) fExploded->Print();
413     if ( fReal ) fReal->Print();
414   }
415   
416   if ( fPainterMatrices && ( sopt.Contains("MATRI") || sopt.Contains("FULL") ) )
417   {
418     fPainterMatrices->Print(opt);
419   }
420 }
421
422 //_____________________________________________________________________________
423 void
424 AliMUONPainterHelper::RegisterContour(AliMUONContour* contour, Bool_t explodedView)
425 {
426   /// contour is adopted by contourMaker
427   AliCodeTimerAuto("",0)
428   AliDebug(1,contour->GetName());
429   AliMUONContourHandler* ch = fReal;
430   if ( explodedView ) 
431   {
432     ch = Exploded();
433   }
434   if (!ch)
435   {
436     AliError(Form("ContourHandler for %s view is not created yet !",explodedView ? "EXPLODED" : "REAL"));
437   }
438   else
439   {
440     if ( ch->GetContour(contour->GetName()) )
441     {
442       AliError(Form("Contour with name %s is already there",contour->GetName()));
443       return;
444     }
445     ch->Adopt(contour);
446   }
447 }
448
449 //_____________________________________________________________________________
450 TString 
451 AliMUONPainterHelper::ChamberName(Int_t chamberId) const
452 {
453   /// Build a name for one chamber
454   return Form("Chamber%1d",chamberId+1);
455 }
456
457 //_____________________________________________________________________________
458 TString 
459 AliMUONPainterHelper::StationName(Int_t stationId) const
460 {
461   /// Build a name for one station
462   return Form("Station%1d",stationId+1);
463 }
464
465 //_____________________________________________________________________________
466 TString 
467 AliMUONPainterHelper::DEName(Int_t detElemId) const
468 {
469   /// Build a name for one detection element
470   return Form("DE%04d",detElemId);
471 }
472
473 //_____________________________________________________________________________
474 TString 
475 AliMUONPainterHelper::ManuName(Int_t manuId) const
476 {
477   /// Build a name for one manu
478   return Form("MANU%04d",manuId);
479 }
480
481 //_____________________________________________________________________________
482 TString 
483 AliMUONPainterHelper::BusPatchName(Int_t busPatchId) const
484 {
485   /// Build a name for one buspatch
486   return Form("BUSPATCH%04d",busPatchId);
487 }
488
489 //_____________________________________________________________________________
490 TString 
491 AliMUONPainterHelper::PCBName(Int_t pcbNumber) const
492 {
493   /// Build a name for one pcb
494   return Form("PCB%1d",pcbNumber);
495 }
496
497 //_____________________________________________________________________________
498 TString 
499 AliMUONPainterHelper::ChamberPathName(Int_t chamberId) const
500 {
501   /// Build a name for one chamber
502   return Form("%s/%s",StationName(chamberId/2).Data(),ChamberName(chamberId).Data());
503 }
504
505 //_____________________________________________________________________________
506 TString 
507 AliMUONPainterHelper::StationPathName(Int_t stationId) const
508 {
509   /// Build a name for one station
510   return StationName(stationId);
511 }
512
513 //_____________________________________________________________________________
514 TString 
515 AliMUONPainterHelper::DEPathName(Int_t detElemId) const
516 {
517   /// Build a name for one detection element
518   
519   Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
520   
521   return Form("%s/%s/%s",
522               StationName(chamberId/2).Data(),
523               ChamberName(chamberId).Data(),
524               DEName(detElemId).Data());
525 }
526
527 //_____________________________________________________________________________
528 TString 
529 AliMUONPainterHelper::ManuPathName(Int_t detElemId, Int_t manuId) const
530 {
531   /// Build a name for one manu
532   return Form("%s/%s",DEPathName(detElemId).Data(),ManuName(manuId).Data());
533 }
534
535 //_____________________________________________________________________________
536 TString 
537 AliMUONPainterHelper::BusPatchPathName(Int_t busPatchId) const
538 {
539   /// Build a name for one buspatch
540   Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
541   
542   return Form("%s/%s",DEPathName(detElemId).Data(),BusPatchName(busPatchId).Data());
543 }
544
545 //_____________________________________________________________________________
546 TString 
547 AliMUONPainterHelper::PCBPathName(Int_t detElemId, Int_t pcbNumber) const
548 {
549   /// Build a name for one pcb
550   return Form("%s/%s",DEPathName(detElemId).Data(),PCBName(pcbNumber).Data());
551 }
552
553 //_____________________________________________________________________________
554 TString
555 AliMUONPainterHelper::FormatValue(const char* name, Double_t value) const
556 {
557   /// Format a double value to be displayed
558   /// FIXME: should insure we have the right number of significant digits here...
559   
560   TString sname(name);
561   
562   sname.ToUpper();
563   if (sname.Contains("BIT"))
564   {
565     Int_t i = (Int_t)(value);
566     TString rv = Form("%s = 0x%x",name,i);
567     cout << rv << ":" << AliMUONPadStatusMaker::AsString(i) << endl;
568     return rv;
569   }
570   else
571   {
572     return Form("%s = %e",name,value);
573   }
574 }
575
576 //_____________________________________________________________________________
577 TObjArray*
578 AliMUONPainterHelper::GetAllContoursAsArray(Bool_t explodedView) const
579 {
580   /// Get the contours in a specially arranged array (orderer by hierarchy level)
581   
582   if ( explodedView ) 
583   {
584     return Exploded()->AllContourArray();
585   }
586   else
587   {
588     return Real()->AllContourArray();
589   }
590 }
591
592