]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPainterHelper.cxx
Fix memory leak (in simulations with trigger chamber efficiency < 1)
[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     Double_t range = max - min;
328     Double_t offset = value - min;
329     rv = gStyle->GetColorPalette( 1 + int( offset*(gStyle->GetNumberOfColors()-2)/range - 0.5 ) );
330   }
331   return rv;
332 }
333
334 //_____________________________________________________________________________
335 AliMUONContour* 
336 AliMUONPainterHelper::MergeContours(const TObjArray& contours, const char* contourName, Bool_t explodedGeometry)
337 {
338   /// Merge a set of contours (delegating to the contour maker)
339   
340   AliMUONContourMaker maker;
341   
342   AliMUONContour* contour = maker.MergeContour(contours,contourName);
343   
344   if (contour) 
345   {
346     RegisterContour(contour,explodedGeometry);
347   }
348   return contour;
349 }
350
351
352 //_____________________________________________________________________________
353 void
354 AliMUONPainterHelper::Print(Option_t* opt) const
355 {
356   /// Printout
357   TString sopt(opt);
358   sopt.ToUpper();
359   
360   if ( sopt.Length() == 0 )
361   {
362     if ( fExploded ) fExploded->Print();
363     if ( fReal ) fReal->Print();
364   }  
365 }
366
367 //_____________________________________________________________________________
368 void
369 AliMUONPainterHelper::RegisterContour(AliMUONContour* contour, Bool_t explodedView)
370 {
371   /// contour is adopted by contourMaker
372   AliCodeTimerAuto("",0)
373   AliDebug(1,contour->GetName());
374   AliMUONContourHandler* ch = fReal;
375   if ( explodedView ) 
376   {
377     ch = Exploded();
378   }
379   if (!ch)
380   {
381     AliError(Form("ContourHandler for %s view is not created yet !",explodedView ? "EXPLODED" : "REAL"));
382   }
383   else
384   {
385     if ( ch->GetContour(contour->GetName()) )
386     {
387       AliError(Form("Contour with name %s is already there",contour->GetName()));
388       return;
389     }
390     ch->Adopt(contour);
391   }
392 }
393
394 //_____________________________________________________________________________
395 TString 
396 AliMUONPainterHelper::ChamberName(Int_t chamberId) const
397 {
398   /// Build a name for one chamber
399   return Form("Chamber%1d",chamberId+1);
400 }
401
402 //_____________________________________________________________________________
403 TString 
404 AliMUONPainterHelper::StationName(Int_t stationId) const
405 {
406   /// Build a name for one station
407   return Form("Station%1d",stationId+1);
408 }
409
410 //_____________________________________________________________________________
411 TString 
412 AliMUONPainterHelper::DEName(Int_t detElemId) const
413 {
414   /// Build a name for one detection element
415   return Form("DE%04d",detElemId);
416 }
417
418 //_____________________________________________________________________________
419 TString 
420 AliMUONPainterHelper::ManuName(Int_t manuId) const
421 {
422   /// Build a name for one manu
423   return Form("MANU%04d",manuId);
424 }
425
426 //_____________________________________________________________________________
427 TString 
428 AliMUONPainterHelper::BusPatchName(Int_t busPatchId) const
429 {
430   /// Build a name for one buspatch
431   return Form("BUSPATCH%04d",busPatchId);
432 }
433
434 //_____________________________________________________________________________
435 TString 
436 AliMUONPainterHelper::PCBName(Int_t pcbNumber) const
437 {
438   /// Build a name for one pcb
439   return Form("PCB%1d",pcbNumber);
440 }
441
442 //_____________________________________________________________________________
443 TString 
444 AliMUONPainterHelper::ChamberPathName(Int_t chamberId) const
445 {
446   /// Build a name for one chamber
447   return Form("%s/%s",StationName(chamberId/2).Data(),ChamberName(chamberId).Data());
448 }
449
450 //_____________________________________________________________________________
451 TString 
452 AliMUONPainterHelper::StationPathName(Int_t stationId) const
453 {
454   /// Build a name for one station
455   return StationName(stationId);
456 }
457
458 //_____________________________________________________________________________
459 TString 
460 AliMUONPainterHelper::DEPathName(Int_t detElemId) const
461 {
462   /// Build a name for one detection element
463   
464   Int_t chamberId = AliMpDEManager::GetChamberId(detElemId);
465   
466   return Form("%s/%s/%s",
467               StationName(chamberId/2).Data(),
468               ChamberName(chamberId).Data(),
469               DEName(detElemId).Data());
470 }
471
472 //_____________________________________________________________________________
473 TString 
474 AliMUONPainterHelper::ManuPathName(Int_t detElemId, Int_t manuId) const
475 {
476   /// Build a name for one manu
477   return Form("%s/%s",DEPathName(detElemId).Data(),ManuName(manuId).Data());
478 }
479
480 //_____________________________________________________________________________
481 TString 
482 AliMUONPainterHelper::BusPatchPathName(Int_t busPatchId) const
483 {
484   /// Build a name for one buspatch
485   Int_t detElemId = AliMpDDLStore::Instance()->GetDEfromBus(busPatchId);
486   
487   return Form("%s/%s",DEPathName(detElemId).Data(),BusPatchName(busPatchId).Data());
488 }
489
490 //_____________________________________________________________________________
491 TString 
492 AliMUONPainterHelper::PCBPathName(Int_t detElemId, Int_t pcbNumber) const
493 {
494   /// Build a name for one pcb
495   return Form("%s/%s",DEPathName(detElemId).Data(),PCBName(pcbNumber).Data());
496 }
497
498 //_____________________________________________________________________________
499 TString
500 AliMUONPainterHelper::FormatValue(const char* name, Double_t value) const
501 {
502   /// Format a double value to be displayed
503   /// FIXME: should insure we have the right number of significant digits here...
504   
505   TString sname(name);
506   
507   sname.ToUpper();
508   if (sname.Contains("BIT"))
509   {
510     Int_t i = (Int_t)(value);
511     TString rv = Form("%s = 0x%x",name,i);
512     cout << rv << ":" << AliMUONPadStatusMaker::AsString(i) << endl;
513     return rv;
514   }
515   else
516   {
517     return Form("%s = %e",name,value);
518   }
519 }
520
521 //_____________________________________________________________________________
522 TObjArray*
523 AliMUONPainterHelper::GetAllContoursAsArray(Bool_t explodedView) const
524 {
525   /// Get the contours in a specially arranged array (orderer by hierarchy level)
526   
527   if ( explodedView ) 
528   {
529     return Exploded()->AllContourArray();
530   }
531   else
532   {
533     return Real()->AllContourArray();
534   }
535 }
536
537