]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPainterHelper.cxx
In AliMUONSDigitizerV2:
[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 "AliMUONPainterRegistry.h"
31 #include "AliMUONPadStatusMaker.h"
32 #include "AliMUONVCalibParam.h"
33 #include "AliMUONVDigit.h"
34 #include "AliMUONVTrackerData.h"
35 #include "AliMpCDB.h"
36 #include "AliMpConstants.h"
37 #include "AliMpDDLStore.h"
38 #include "AliMpDEIterator.h"
39 #include "AliMpDEManager.h"
40 #include "AliMpExMap.h"
41 #include "AliMpMotifMap.h"
42 #include "AliMpMotifPosition.h"
43 #include "AliMpPCB.h"
44 #include "AliMpPad.h"
45 #include "AliMpSector.h"
46 #include "AliMpSegmentation.h"
47 #include "AliMpSlat.h"
48 #include "AliMpStationType.h"
49 #include "AliMpVPadIterator.h"
50 #include <Riostream.h>
51 #include <TArrayI.h>
52 #include <TCanvas.h>
53 #include <TClass.h>
54 #include <TCollection.h>
55 #include <TFile.h>
56 #include <TGLabel.h>
57 #include <TGMsgBox.h>
58 #include <TGeoMatrix.h>
59 #include <TLine.h>
60 #include <TList.h>
61 #include <TMap.h>
62 #include <TMath.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 fEnv(0x0),
90 fReal(0x0),
91 fExploded(0x0)
92 {
93   /// ctor
94   
95   if ( ! AliMpCDB::LoadMpSegmentation() ) 
96   {
97     AliFatal("Could not access mapping from OCDB !");
98   }
99   
100   // Load DDL store
101   if ( ! AliMpCDB::LoadDDLStore() ) 
102   {
103     AliFatal("Could not access DDL Store from OCDB !");
104   }  
105 }
106
107 //_____________________________________________________________________________
108 AliMUONPainterHelper::~AliMUONPainterHelper()
109 {
110   /// dtor
111   delete fReal;
112   delete fExploded;
113   fEnv->Save();
114   fgInstance = 0;
115 }
116
117 //_____________________________________________________________________________
118 AliMUONContourHandler*
119 AliMUONPainterHelper::Exploded() const
120 {
121   /// Create exploded contour handler
122   if (!fExploded) fExploded = new AliMUONContourHandler(kTRUE);
123   return fExploded;
124 }
125
126 //_____________________________________________________________________________
127 AliMUONContourHandler*
128 AliMUONPainterHelper::Real() const
129 {
130   /// Create real contour handler
131   if (!fReal) fReal = new AliMUONContourHandler(kFALSE);
132   return fReal;
133 }
134
135 //_____________________________________________________________________________
136 AliMUONContour*
137 AliMUONPainterHelper::GetContour(const char* contourName, Bool_t explodedView) const
138 {
139   /// Get a contour by name  
140   if (explodedView) 
141   {
142     return Exploded()->GetContour(contourName);
143   }
144   else
145   {
146     if ( fReal ) 
147     {
148       return fReal->GetContour(contourName);
149     }
150   }
151   return 0x0;
152 }
153
154
155 //_____________________________________________________________________________
156 AliMp::CathodType
157 AliMUONPainterHelper::GetCathodeType(Int_t detElemId, Int_t manuId) const
158 {
159   /// Get the cathode type of a given manu
160   
161   AliMp::PlaneType planeType(AliMp::kBendingPlane);
162   if ( manuId & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) )
163   {
164     planeType = AliMp::kNonBendingPlane;
165   }
166   return AliMpDEManager::GetCathod(detElemId,planeType);
167 }
168
169
170 //_____________________________________________________________________________
171 AliMpMotifPosition* 
172 AliMUONPainterHelper::GetMotifPosition(Int_t detElemId, Int_t manuId) const
173 {
174   /// Get a given motif position
175   const AliMpVSegmentation* vseg = AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
176   if (vseg)
177   {
178     return vseg->MotifPosition(manuId);
179   }
180   return 0x0;
181 }
182
183 //_____________________________________________________________________________
184 AliMpPCB*
185 AliMUONPainterHelper::GetPCB(Int_t detElemId, AliMp::CathodType cathodeType, 
186                              Int_t pcbNumber) const
187 {
188   /// Get a given PCB
189   const AliMpSlat* slat = GetSlat(detElemId,cathodeType);
190   if ( slat ) return slat->GetPCB(pcbNumber);
191   return 0x0;
192 }
193
194 //_____________________________________________________________________________
195 AliMpPCB*
196 AliMUONPainterHelper::GetPCB(Int_t detElemId, AliMp::PlaneType planeType, 
197                              Int_t pcbNumber) const
198 {
199   /// Get a given PCB
200   AliMp::CathodType cathodeType = AliMpDEManager::GetCathod(detElemId,
201                                                             planeType);
202   return GetPCB(detElemId,cathodeType,pcbNumber);
203 }
204
205 //_____________________________________________________________________________
206 AliMp::PlaneType
207 AliMUONPainterHelper::GetPlaneType(Int_t manuId) const
208 {
209   /// Get the planeType of a given manu
210   
211   if ( manuId & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) )
212   {
213     return AliMp::kNonBendingPlane;
214   }
215   return AliMp::kBendingPlane;
216 }
217
218 //_____________________________________________________________________________
219 const AliMpSlat*
220 AliMUONPainterHelper::GetSlat(Int_t detElemId, AliMp::PlaneType planeType) const
221 {
222   /// Get a given slat
223   
224   AliMp::CathodType cathodeType = AliMpDEManager::GetCathod(detElemId,
225                                                           planeType);
226
227   return GetSlat(detElemId,cathodeType);
228 }
229
230 //_____________________________________________________________________________
231 const AliMpSector*
232 AliMUONPainterHelper::GetSector(Int_t detElemId, AliMp::PlaneType planeType) const
233 {
234   /// Get a given sector
235   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
236   if ( stationType != AliMp::kStation12 ) return 0x0;
237   
238   AliMp::CathodType cathodeType = AliMpDEManager::GetCathod(detElemId,planeType);
239   
240   return AliMpSegmentation::Instance()->GetSector(detElemId,cathodeType);
241 }
242
243 //_____________________________________________________________________________
244 const AliMpSlat*
245 AliMUONPainterHelper::GetSlat(Int_t detElemId, AliMp::CathodType cathodeType) const
246 {
247   /// Get a given slat
248   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
249   if ( stationType != AliMp::kStation345 ) return 0x0;
250
251   return AliMpSegmentation::Instance()->GetSlat(detElemId,cathodeType);
252 }
253
254 //_____________________________________________________________________________
255 const AliMpSlat*
256 AliMUONPainterHelper::GetSlat(Int_t detElemId, Int_t manuId) const
257 {
258   /// Get a given slat
259   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
260   if ( stationType != AliMp::kStation345 ) return 0x0;
261
262   return AliMpSegmentation::Instance()->GetSlatByElectronics(detElemId,manuId);
263 }
264
265 //_____________________________________________________________________________
266 AliMUONPainterHelper*
267 AliMUONPainterHelper::Instance()
268 {
269   /// Return the global and unique instance of this class
270   
271   if (fgInstance) return fgInstance;
272
273   AliCodeTimerAutoClass("",0);
274
275   fgInstance = new AliMUONPainterHelper;
276   fgInstance->fEnv = new AliMUONPainterEnv;
277   return fgInstance;
278 }
279
280 //_____________________________________________________________________________
281 void 
282 AliMUONPainterHelper::Global2Local(Int_t detElemId, 
283                                     Double_t xg, Double_t yg, Double_t zg,
284                                     Double_t& xl, Double_t& yl, Double_t& zl) const
285 {
286   /// Local to global transformation of coordinates
287   
288   TGeoHMatrix* matrix = static_cast<TGeoHMatrix*>(Exploded()->GetTransformations()->GetValue(detElemId));
289   Double_t pg[3] = { xg, yg, zg };
290   Double_t pl[3] = { 0., 0., 0. };
291   matrix->MasterToLocal(pg, pl);
292   xl = pl[0];
293   yl = pl[1];
294   zl = pl[2];
295 }
296
297 //_____________________________________________________________________________
298 void 
299 AliMUONPainterHelper::Local2Global(Int_t detElemId, 
300                                    Double_t xl, Double_t yl, Double_t zl,
301                                    Double_t& xg, Double_t& yg, Double_t& zg) const
302 {
303   /// Local to (exploded) global transformation of coordinates
304   
305   TGeoHMatrix* matrix = static_cast<TGeoHMatrix*>(Exploded()->GetTransformations()->GetValue(detElemId));
306   Double_t pl[3] = { xl, yl, zl };
307   Double_t pg[3] = { 0., 0., 0. };
308   matrix->LocalToMaster(pl, pg);
309   xg = pg[0];
310   yg = pg[1];
311   zg = pg[2];
312 }
313
314 //_____________________________________________________________________________
315 Int_t
316 AliMUONPainterHelper::ColorFromValue(Double_t value, Double_t min, Double_t max) const
317
318   /// Convert a value into a color, fitting within a given range
319   
320   Int_t rv;
321   
322   if (value > max) rv = 1;
323   else if (value <= min) rv = 0;
324   else
325   {
326 //    if  ( TMath::AreEqualRel(max,min,1E-6) ) return gStyle->GetColorPalette(1);
327     if ( TMath::Abs(max-min) < 0.5*1E-6*TMath::Abs(max)+TMath::Abs(min) ) return gStyle->GetColorPalette(1);
328     Double_t range = max - min;
329     Double_t offset = value - min;
330     rv = gStyle->GetColorPalette( 1 + int( offset*(gStyle->GetNumberOfColors()-2)/range - 0.5 ) );
331   }
332   return rv;
333 }
334
335 //_____________________________________________________________________________
336 AliMUONContour* 
337 AliMUONPainterHelper::MergeContours(const TObjArray& contours, const char* contourName, Bool_t explodedGeometry)
338 {
339   /// Merge a set of contours (delegating to the contour maker)
340   
341   AliMUONContourMaker maker;
342   
343   AliMUONContour* contour = maker.MergeContour(contours,contourName);
344   
345   if (contour) 
346   {
347     RegisterContour(contour,explodedGeometry);
348   }
349   return contour;
350 }
351
352
353 //_____________________________________________________________________________
354 void
355 AliMUONPainterHelper::Print(Option_t* opt) const
356 {
357   /// Printout
358   TString sopt(opt);
359   sopt.ToUpper();
360   
361   if ( sopt.Length() == 0 )
362   {
363     if ( fExploded ) fExploded->Print();
364     if ( fReal ) fReal->Print();
365   }  
366 }
367
368 //_____________________________________________________________________________
369 void
370 AliMUONPainterHelper::RegisterContour(AliMUONContour* contour, Bool_t explodedView)
371 {
372   /// contour is adopted by contourMaker
373   AliCodeTimerAuto("",0)
374   AliDebug(1,contour->GetName());
375   AliMUONContourHandler* ch = fReal;
376   if ( explodedView ) 
377   {
378     ch = Exploded();
379   }
380   if (!ch)
381   {
382     AliError(Form("ContourHandler for %s view is not created yet !",explodedView ? "EXPLODED" : "REAL"));
383   }
384   else
385   {
386     if ( ch->GetContour(contour->GetName()) )
387     {
388       AliError(Form("Contour with name %s is already there",contour->GetName()));
389       return;
390     }
391     ch->Adopt(contour);
392   }
393 }
394
395 //_____________________________________________________________________________
396 TString 
397 AliMUONPainterHelper::ChamberName(Int_t chamberId) const
398 {
399   /// Build a name for one chamber
400   return Form("Chamber%1d",chamberId+1);
401 }
402
403 //_____________________________________________________________________________
404 TString 
405 AliMUONPainterHelper::StationName(Int_t stationId) const
406 {
407   /// Build a name for one station
408   return Form("Station%1d",stationId+1);
409 }
410
411 //_____________________________________________________________________________
412 TString 
413 AliMUONPainterHelper::DEName(Int_t detElemId) const
414 {
415   /// Build a name for one detection element
416   return Form("DE%04d",detElemId);
417 }
418
419 //_____________________________________________________________________________
420 TString 
421 AliMUONPainterHelper::ManuName(Int_t manuId) const
422 {
423   /// Build a name for one manu
424   return Form("MANU%04d",manuId);
425 }
426
427 //_____________________________________________________________________________
428 TString 
429 AliMUONPainterHelper::BusPatchName(Int_t busPatchId) const
430 {
431   /// Build a name for one buspatch
432   return Form("BUSPATCH%04d",busPatchId);
433 }
434
435 //_____________________________________________________________________________
436 TString 
437 AliMUONPainterHelper::PCBName(Int_t pcbNumber) const
438 {
439   /// Build a name for one pcb
440   return Form("PCB%1d",pcbNumber);
441 }
442
443 //_____________________________________________________________________________
444 TString 
445 AliMUONPainterHelper::ChamberPathName(Int_t chamberId) const
446 {
447   /// Build a name for one chamber
448   return Form("%s/%s",StationName(chamberId/2).Data(),ChamberName(chamberId).Data());
449 }
450
451 //_____________________________________________________________________________
452 TString 
453 AliMUONPainterHelper::StationPathName(Int_t stationId) const
454 {
455   /// Build a name for one station
456   return StationName(stationId);
457 }
458
459 //_____________________________________________________________________________
460 TString 
461 AliMUONPainterHelper::DEPathName(Int_t detElemId) const
462 {
463   /// Build a name for one detection element
464   
465   Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
466   
467   return Form("%s/%s/%s",
468               StationName(chamberId/2).Data(),
469               ChamberName(chamberId).Data(),
470               DEName(detElemId).Data());
471 }
472
473 //_____________________________________________________________________________
474 TString 
475 AliMUONPainterHelper::ManuPathName(Int_t detElemId, Int_t manuId) const
476 {
477   /// Build a name for one manu
478   return Form("%s/%s",DEPathName(detElemId).Data(),ManuName(manuId).Data());
479 }
480
481 //_____________________________________________________________________________
482 TString 
483 AliMUONPainterHelper::BusPatchPathName(Int_t busPatchId) const
484 {
485   /// Build a name for one buspatch
486   Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
487   
488   return Form("%s/%s",DEPathName(detElemId).Data(),BusPatchName(busPatchId).Data());
489 }
490
491 //_____________________________________________________________________________
492 TString 
493 AliMUONPainterHelper::PCBPathName(Int_t detElemId, Int_t pcbNumber) const
494 {
495   /// Build a name for one pcb
496   return Form("%s/%s",DEPathName(detElemId).Data(),PCBName(pcbNumber).Data());
497 }
498
499 //_____________________________________________________________________________
500 TString
501 AliMUONPainterHelper::FormatValue(const char* name, Double_t value) const
502 {
503   /// Format a double value to be displayed
504   /// FIXME: should insure we have the right number of significant digits here...
505   
506   TString sname(name);
507   
508   sname.ToUpper();
509   if (sname.Contains("BIT"))
510   {
511     Int_t i = (Int_t)(value);
512     TString rv = Form("%s = 0x%x",name,i);
513     cout << rv << ":" << AliMUONPadStatusMaker::AsString(i) << endl;
514     return rv;
515   }
516   else
517   {
518     return Form("%s = %e",name,value);
519   }
520 }
521
522 //_____________________________________________________________________________
523 TObjArray*
524 AliMUONPainterHelper::GetAllContoursAsArray(Bool_t explodedView) const
525 {
526   /// Get the contours in a specially arranged array (orderer by hierarchy level)
527   
528   if ( explodedView ) 
529   {
530     return Exploded()->AllContourArray();
531   }
532   else
533   {
534     return Real()->AllContourArray();
535   }
536 }
537
538