next50 trigger mask in AliHLTGlobalEsdConverterComponent
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackerDataCompareDialog.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 "AliMUONTrackerDataCompareDialog.h"
19
20 /// \class AliMUONTrackerDataCompareDialog
21 ///
22 /// Widget to select 2 VTrackerData objects (D1,D2) to be compared
23 ///
24 /// The type of differences that can be used are : 
25 ///
26 /// - Difference = plain difference D1-D2
27 /// - Absolute difference = absolute value of the preceeding = |D1-D2|
28 /// - Relative difference = relative difference = (D1-D2)/D1
29 /// - Absolute relative difference = absolute value of preceeding = |(D1-D2)/D1|
30 ///
31 /// \author Laurent Aphecetche, Subatech
32 ///
33
34 #include "AliLog.h"
35 #include "AliMUON2DMap.h"
36 #include "AliMUONCalibParamND.h"
37 #include "AliMUONPainterDataRegistry.h"
38 #include "AliMUONTrackerData.h"
39 #include "AliMUONTrackerDataWrapper.h"
40 #include "AliMUONVTrackerData.h"
41 #include "AliMpConstants.h"
42 #include "AliMpDDLStore.h"
43 #include "AliMpDetElement.h"
44 #include "AliMpManuIterator.h"
45 #include <TGComboBox.h>
46 #include <TGLabel.h>
47 #include <TGTextEntry.h>
48 #include <TTimer.h>
49
50 /// \cond CLASSIMP
51 ClassImp(AliMUONTrackerDataCompareDialog)
52 /// \endcond
53
54 const Int_t AliMUONTrackerDataCompareDialog::fgkDifference(1);
55 const Int_t AliMUONTrackerDataCompareDialog::fgkAbsoluteDifference(2);
56 const Int_t AliMUONTrackerDataCompareDialog::fgkRelativeDifference(3);
57 const Int_t AliMUONTrackerDataCompareDialog::fgkAbsoluteRelativeDifference(4);
58 const Int_t AliMUONTrackerDataCompareDialog::fgkAll(5);
59
60 namespace
61 {
62   
63 #define PRECISION 1E-12
64   
65   Double_t Difference(Double_t v1, Double_t v2)
66   {
67     Double_t d = v1-v2;
68     return TMath::Abs(d) < PRECISION ? 0.0 : d;
69   }
70     
71   Double_t AbsoluteDifference(Double_t v1, Double_t v2)
72   {
73     return TMath::Abs(Difference(v1,v2));
74   }
75   
76   
77   Double_t RelativeDifference(Double_t v1, Double_t v2)
78   {
79     if ( TMath::Abs(v1) < PRECISION ) return 0.0;
80     return (v1-v2)/v1;
81   }
82   
83   Double_t AbsoluteRelativeDifference(Double_t v1, Double_t v2)
84   {
85     return TMath::Abs(RelativeDifference(v1,v2));
86   }
87 }
88
89
90 //_____________________________________________________________________________
91 AliMUONTrackerDataCompareDialog::AliMUONTrackerDataCompareDialog(const TGWindow* p, const TGWindow* main, UInt_t w, UInt_t h)
92 : TGTransientFrame(p,main,w,h),
93 fF1(new TGHorizontalFrame(this)),
94 fData1(new TGComboBox(fF1)),
95 fF2(new TGHorizontalFrame(this)),
96 fData2(new TGComboBox(fF2)),
97 fF3(new TGHorizontalFrame(this)),
98 fDiffType(new TGComboBox(fF3)),
99 fF4(new TGHorizontalFrame(this)),
100 fBasename(new TGTextEntry(fF4)),
101 fButtonFrame(new TGHorizontalFrame(this)),
102 fOK(new TGTextButton(fButtonFrame,"OK")),
103 fCancel(new TGTextButton(fButtonFrame,"Cancel"))
104 {
105   /// ctor
106   
107   SetCleanup(kDeepCleanup);
108   
109   AliMUONPainterDataRegistry* reg = AliMUONPainterDataRegistry::Instance();
110   
111   for ( Int_t i = 0; i < reg->NumberOfDataSources(); ++i ) 
112   {
113     AliMUONVTrackerData* data = reg->DataSource(i);
114     fData1->AddEntry(data->GetName(),i);
115     fData2->AddEntry(data->GetName(),i);
116   }
117   
118   fDiffType->AddEntry("Difference",fgkDifference);
119   fDiffType->AddEntry("Absolute difference",fgkAbsoluteDifference);
120   fDiffType->AddEntry("Relative difference",fgkRelativeDifference);
121   fDiffType->AddEntry("Absolute relative difference",fgkAbsoluteRelativeDifference);
122   fDiffType->AddEntry("All four",fgkAll);
123
124   fData1->Select(0);
125   fData2->Select(0);
126   fDiffType->Select(4);
127   
128   fF1->AddFrame(new TGLabel(fF1,"First data"),new TGLayoutHints(kLHintsLeft|kLHintsTop,5,5,5,5));
129   fF1->AddFrame(fData1,new TGLayoutHints(kLHintsRight|kLHintsExpandX|kLHintsTop,5,5,5,5));
130
131   fF2->AddFrame(new TGLabel(fF2,"Second data"),new TGLayoutHints(kLHintsLeft|kLHintsTop,5,5,5,5));
132   fF2->AddFrame(fData2,new TGLayoutHints(kLHintsRight|kLHintsExpandX|kLHintsTop,5,5,5,5));
133
134   fF3->AddFrame(new TGLabel(fF3,"Difference type"),new TGLayoutHints(kLHintsLeft|kLHintsTop,5,5,5,5));
135   fF3->AddFrame(fDiffType,new TGLayoutHints(kLHintsRight|kLHintsExpandX|kLHintsTop,5,5,5,5));
136
137   fF4->AddFrame(new TGLabel(fF4,"Output basename"),new TGLayoutHints(kLHintsLeft|kLHintsTop,5,5,5,5));
138   fF4->AddFrame(fBasename,new TGLayoutHints(kLHintsRight|kLHintsExpandX|kLHintsTop,5,5,5,5));
139
140   AddFrame(fF1,new TGLayoutHints(kLHintsLeft|kLHintsExpandX|kLHintsTop,5,5,5,5));
141   AddFrame(fF2,new TGLayoutHints(kLHintsLeft|kLHintsExpandX|kLHintsTop,5,5,5,5));
142   AddFrame(fF3,new TGLayoutHints(kLHintsLeft|kLHintsExpandX|kLHintsTop,5,5,5,5));
143   AddFrame(fF4,new TGLayoutHints(kLHintsLeft|kLHintsExpandX|kLHintsTop,5,5,5,5));
144   
145   fButtonFrame->AddFrame(fOK,new TGLayoutHints(kLHintsLeft|kLHintsTop,5,5,5,5));
146   fButtonFrame->AddFrame(fCancel,new TGLayoutHints(kLHintsRight|kLHintsTop,5,5,5,5));
147
148   AddFrame(fButtonFrame,new TGLayoutHints(kLHintsLeft|kLHintsExpandX|kLHintsTop,5,5,5,5));
149   
150   fData1->Resize(200,20);
151   fData2->Resize(200,20);
152   fDiffType->Resize(200,20);
153   
154   fOK->Connect("Clicked()", "AliMUONTrackerDataCompareDialog",this,"DoOK()");
155   fCancel->Connect("Clicked()","AliMUONTrackerDataCompareDialog",this,"DoCancel()");
156 }
157
158 //_____________________________________________________________________________
159 AliMUONTrackerDataCompareDialog::~AliMUONTrackerDataCompareDialog()
160 {
161   /// dtor
162 }
163
164 //______________________________________________________________________________
165 void
166 AliMUONTrackerDataCompareDialog::DoOK()
167 {
168   /// Do the job.
169   
170   TGTextLBEntry* t1 = static_cast<TGTextLBEntry*>(fData1->GetSelectedEntry());
171   TString s1 = t1->GetText()->GetString();
172   TGTextLBEntry* t2 = static_cast<TGTextLBEntry*>(fData2->GetSelectedEntry());
173   TString s2 = t2->GetText()->GetString();
174   
175   Int_t nd = fDiffType->GetSelected();
176   
177   if ( nd == fgkAll ) 
178   {
179     CompareData(s1.Data(),s2.Data(),fgkDifference);
180     CompareData(s1.Data(),s2.Data(),fgkRelativeDifference);
181     CompareData(s1.Data(),s2.Data(),fgkAbsoluteDifference);
182     CompareData(s1.Data(),s2.Data(),fgkAbsoluteRelativeDifference);
183   }
184   else
185   {
186     CompareData(s1.Data(),s2.Data(),nd);
187   }
188   
189   TTimer::SingleShot(150,"AliMUONTrackerDataCompareDialog",this,"CloseWindow()");
190 }
191
192 //______________________________________________________________________________
193 void
194 AliMUONTrackerDataCompareDialog::DoCancel()
195 {
196   /// Kills the dialog
197   TTimer::SingleShot(150,"AliMUONTrackerDataCompareDialog",this,"CloseWindow()");
198 }
199
200 //______________________________________________________________________________
201 void
202 AliMUONTrackerDataCompareDialog::CompareData(const char* d1name,
203                                              const char* d2name,
204                                              Int_t difftype) const
205 {
206   /// Compare two data sources
207   
208   AliMUONPainterDataRegistry* reg = AliMUONPainterDataRegistry::Instance();
209   
210   AliMUONVTrackerData* d1 = reg->DataSource(d1name);
211   if (!d1)
212   {
213     AliError(Form("Cannot find data source %s",d1name));
214     return;
215   }
216   
217   AliMUONVTrackerData* d2 = reg->DataSource(d2name);
218   if (!d2)
219   {
220     AliError(Form("Cannot find data source %s",d2name));
221     return;
222   }
223   
224   Double_t (*difffunction)(Double_t,Double_t)=0x0;
225   TString suffix("unknown");
226   
227   if ( difftype == fgkDifference ) 
228   {
229     difffunction = Difference;
230     suffix = "D";
231   }
232   if ( difftype == fgkAbsoluteDifference ) 
233   {
234     difffunction = AbsoluteDifference;
235     suffix = "AD";
236   }
237   if ( difftype == fgkRelativeDifference ) 
238   {
239     difffunction = RelativeDifference;
240     suffix = "RD";
241   }
242   if ( difftype == fgkAbsoluteRelativeDifference ) 
243   {
244     difffunction = AbsoluteRelativeDifference;
245     suffix = "ARD";
246   }
247   
248   if ( difffunction ) 
249   {
250     TString basename = fBasename->GetText(); 
251   
252     AliMUONVTrackerData* d = CompareData(*d1,*d2,Form("%s:%s",basename.Data(),suffix.Data()),difffunction);
253     
254     AliMUONVTrackerDataMaker* dw = new AliMUONTrackerDataWrapper(d);
255     
256     AliMUONPainterDataRegistry::Instance()->Register(dw);
257   }
258 }
259
260 //______________________________________________________________________________
261 AliMUONVTrackerData*
262 AliMUONTrackerDataCompareDialog::CompareData(const AliMUONVTrackerData& d1,
263                                              const AliMUONVTrackerData& d2,
264                                              const char* outname,
265                                              Double_t(*diff)(Double_t,Double_t)) const
266 {
267   /// Compare two data objects, using the diff method
268   
269   if ( d1.NumberOfDimensions() != d2.NumberOfDimensions() ) 
270   {
271     AliError("Cannot compare data of incompatible dimensions");
272     return 0x0;
273   }
274   
275   AliMpManuIterator it;
276   Int_t detElemId, manuId;
277   
278   AliMUONVStore* store = new AliMUON2DMap(kTRUE);
279   
280   while ( it.Next(detElemId,manuId) )
281   {
282     if ( d1.HasDetectionElement(detElemId) && d2.HasDetectionElement(detElemId) &&
283          d1.HasManu(detElemId,manuId) && d2.HasManu(detElemId,manuId) )
284     {
285       AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
286       
287       AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(store->FindObject(detElemId,manuId));
288       
289       if (!param)
290             {
291               param = new AliMUONCalibParamND(d1.ExternalDimension(),64,detElemId,manuId,
292                                         AliMUONVCalibParam::InvalidFloatValue());
293               store->Add(param);
294             }
295       
296       for ( Int_t i = 0; i < AliMpConstants::ManuNofChannels(); ++i ) 
297             {
298               if ( de->IsConnectedChannel(manuId,i) )
299         {
300           for ( Int_t k = 0; k < d1.ExternalDimension(); ++k ) 
301           {
302             
303             Double_t d = diff(d1.Channel(detElemId,manuId,i,k),
304                                   d2.Channel(detElemId,manuId,i,k));
305           
306             param->SetValueAsDouble(i,k,d);
307           }
308         }
309             }
310     }
311   }
312   
313   AliMUONVTrackerData* d = new AliMUONTrackerData(outname,outname,d1.ExternalDimension(),kTRUE);
314   for ( Int_t k = 0; k < d1.ExternalDimension(); ++k ) 
315   {
316     d->SetDimensionName(k,Form("D:%s",d1.ExternalDimensionName(k).Data()));
317   }
318   d->Add(*store);
319   
320   return d;
321 }
322