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