Fix histo name
[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   }
215   
216   AliMUONVTrackerData* d2 = reg->DataSource(d2name);
217   if (!d2)
218   {
219     AliError(Form("Cannot find data source %s",d2name));
220   }
221   
222   Double_t (*difffunction)(Double_t,Double_t)=0x0;
223   TString suffix("unknown");
224   
225   if ( difftype == fgkDifference ) 
226   {
227     difffunction = Difference;
228     suffix = "D";
229   }
230   if ( difftype == fgkAbsoluteDifference ) 
231   {
232     difffunction = AbsoluteDifference;
233     suffix = "AD";
234   }
235   if ( difftype == fgkRelativeDifference ) 
236   {
237     difffunction = RelativeDifference;
238     suffix = "RD";
239   }
240   if ( difftype == fgkAbsoluteRelativeDifference ) 
241   {
242     difffunction = AbsoluteRelativeDifference;
243     suffix = "ARD";
244   }
245   
246   TString basename = fBasename->GetText(); 
247   
248   AliMUONVTrackerData* d = CompareData(*d1,*d2,Form("%s:%s",basename.Data(),suffix.Data()),difffunction);
249   
250   AliMUONVTrackerDataMaker* dw = new AliMUONTrackerDataWrapper(d);
251   
252   AliMUONPainterDataRegistry::Instance()->Register(dw);
253 }
254
255 //______________________________________________________________________________
256 AliMUONVTrackerData*
257 AliMUONTrackerDataCompareDialog::CompareData(const AliMUONVTrackerData& d1,
258                                              const AliMUONVTrackerData& d2,
259                                              const char* outname,
260                                              Double_t(*diff)(Double_t,Double_t)) const
261 {
262   /// Compare two data objects, using the diff method
263   
264   if ( d1.NumberOfDimensions() != d2.NumberOfDimensions() ) 
265   {
266     AliError("Cannot compare data of incompatible dimensions");
267     return 0x0;
268   }
269   
270   AliMpManuIterator it;
271   Int_t detElemId, manuId;
272   
273   AliMUONVStore* store = new AliMUON2DMap(kTRUE);
274   
275   while ( it.Next(detElemId,manuId) )
276   {
277     if ( d1.HasDetectionElement(detElemId) && d2.HasDetectionElement(detElemId) &&
278          d1.HasManu(detElemId,manuId) && d2.HasManu(detElemId,manuId) )
279     {
280       AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
281       
282       AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(store->FindObject(detElemId,manuId));
283       
284       if (!param)
285             {
286               param = new AliMUONCalibParamND(d1.ExternalDimension(),64,detElemId,manuId,
287                                         AliMUONVCalibParam::InvalidFloatValue());
288               store->Add(param);
289             }
290       
291       for ( Int_t i = 0; i < AliMpConstants::ManuNofChannels(); ++i ) 
292             {
293               if ( de->IsConnectedChannel(manuId,i) )
294         {
295           for ( Int_t k = 0; k < d1.ExternalDimension(); ++k ) 
296           {
297             
298             Double_t d = diff(d1.Channel(detElemId,manuId,i,k),
299                                   d2.Channel(detElemId,manuId,i,k));
300           
301             param->SetValueAsDouble(i,k,d);
302           }
303         }
304             }
305     }
306   }
307   
308   AliMUONVTrackerData* d = new AliMUONTrackerData(outname,outname,d1.ExternalDimension(),kTRUE);
309   for ( Int_t k = 0; k < d1.ExternalDimension(); ++k ) 
310   {
311     d->SetDimensionName(k,Form("D:%s",d1.ExternalDimensionName(k).Data()));
312   }
313   d->Add(*store);
314   
315   return d;
316 }
317