Fix coverity defect
[u/mrichter/AliRoot.git] / MUON / AliMUONPainterDataSourceFrame.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 "AliMUONPainterDataSourceFrame.h"
20
21 #include "AliLog.h"
22 #include "AliCDBEntry.h"
23 #include "AliCDBManager.h"
24 #include "AliMUONChamberPainter.h"
25 #include "AliMUONMchViewApplication.h"
26 #include "AliMUONPainterDataRegistry.h"
27 #include "AliMUONPainterDataSourceItem.h"
28 #include "AliMUONPainterEnv.h"
29 #include "AliMUONPainterHelper.h"
30 #include "AliMUONPainterMatrix.h"
31 #include "AliMUONPainterRegistry.h"
32 #include "AliMUONRecoParam.h"
33 #include "AliMUONTrackerConditionDataMaker.h"
34 #include "AliMUONTrackerDataMaker.h"
35 #include "AliRawReader.h"
36 #include <TCanvas.h>
37 #include <TGButton.h>
38 #include <TGComboBox.h>
39 #include <TGFileDialog.h>
40 #include <TGNumberEntry.h>
41 #include <TGTextEntry.h>
42 #include <TGrid.h>
43 #include <TObjArray.h>
44 #include <TObjString.h>
45 #include <TMath.h>
46 #include <TRegexp.h>
47 #include <TString.h>
48 #include <TSystem.h>
49
50 ///\class AliMUONPainterDataSourceFrame
51 ///
52 /// A complete frame to select and display various data sources to 
53 /// be displayed : either raw data or OCDB data. 
54 /// Later on we might add digits and clusters for instance.
55 ///
56 ///\author Laurent Aphecetche, Subatech
57
58 const char* AliMUONPainterDataSourceFrame::fgkNumberOfDataSourcesKey = "NumberOfDataSources";
59 const char* AliMUONPainterDataSourceFrame::fgkDataSourceURIKey = "DataSourceURI.%d";
60
61 ///\cond CLASSIMP
62 ClassImp(AliMUONPainterDataSourceFrame)
63 ///\endcond
64
65 //_____________________________________________________________________________
66 AliMUONPainterDataSourceFrame::AliMUONPainterDataSourceFrame(const TGWindow* p, UInt_t w, UInt_t h)
67 : TGCompositeFrame(p,w,h,kVerticalFrame),
68   fRecentSourceSelector(new TGGroupFrame(this,"Recent sources",kHorizontalFrame)),
69   fRawSelector(new TGGroupFrame(this,"Raw file URI",kHorizontalFrame)),
70   fRawSelector2(new TGCompositeFrame(fRawSelector,w,h,kVerticalFrame)),
71   fRawSelector21(new TGCompositeFrame(fRawSelector2,w,h,kHorizontalFrame)),
72   fRawSelector22(new TGCompositeFrame(fRawSelector2,w,h,kHorizontalFrame)),
73   fRawSelector24(new TGCompositeFrame(fRawSelector2,w,h,kHorizontalFrame)),
74   fRawSelector23(new TGCompositeFrame(fRawSelector2,w,h,kHorizontalFrame)),
75   fCalibrateNoGain(new TGCheckButton(fRawSelector22,"Ped sub")),
76   fCalibrateGainConstantCapa(new TGCheckButton(fRawSelector22,"Ped sub+gain (capa cste)")),
77   fCalibrateGain(new TGCheckButton(fRawSelector22,"Full calib (Ped sub+gain w/ capa)")),
78   fCalibrateEmelecGain(new TGCheckButton(fRawSelector22,"Full calib (Ped sub+inj gain w/ capa)")),
79   fHistogramButton(new TGCheckButton(fRawSelector23,"Histogram")),
80   fHistoMin(new TGNumberEntry(fRawSelector23,0)),
81   fHistoMax(new TGNumberEntry(fRawSelector23,4096)),
82   fEventRangeButton(new TGCheckButton(fRawSelector23,"Event range")),
83   fEventMin(new TGNumberEntry(fRawSelector23,-1,10)),
84   fEventMax(new TGNumberEntry(fRawSelector23,-1,10)),
85   fRawOCDBPath(new TGTextEntry(fRawSelector24,"alien://folder=/alice/data/2012/OCDB")),
86   fOCDBSelector(new TGGroupFrame(this,"OCDB Path",kHorizontalFrame)),
87   fDataReaders(new TGGroupFrame(this,"Data sources")),
88   fFilePath(new TGTextEntry(fRawSelector21,"")),
89   fOCDBPath(new TGTextEntry(fOCDBSelector,"alien://folder=/alice/data/2012/OCDB")),
90   fRunSelector(new TGNumberEntry(fOCDBSelector,0,10)),
91   fOCDBTypes(new TGComboBox(fOCDBSelector)),
92   fRecentSources(new TGComboBox(fRecentSourceSelector)),
93   fItems(new TObjArray),
94   fACFSelector(new TGGroupFrame(this,"ASCII Calib File",kHorizontalFrame)),
95   fACFPath(new TGTextEntry(fACFSelector,"")),
96   fACFTypes(new TGComboBox(fACFSelector))
97 {
98   /// Ctor
99   
100     AliMUONPainterDataRegistry* reg = AliMUONPainterDataRegistry::Instance();
101     
102     reg->Connect("DataMakerWasRegistered(AliMUONVTrackerDataMaker*)",
103                  "AliMUONPainterDataSourceFrame",
104                  this,
105                  "DataMakerWasRegistered(AliMUONVTrackerDataMaker*)");
106     
107     reg->Connect("DataMakerWasUnregistered(AliMUONVTrackerDataMaker*)",
108                  "AliMUONPainterDataSourceFrame",
109                  this,
110                  "DataMakerWasUnregistered(AliMUONVTrackerDataMaker*)");
111     
112     fItems->SetOwner(kFALSE);
113     
114     /// Recent source selection
115     
116     AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
117     
118     Int_t nsources = env->Integer(fgkNumberOfDataSourcesKey);
119     
120     for ( Int_t i = 0; i < nsources; ++i )
121     {
122       AddRecentSource(env->String(Form(fgkDataSourceURIKey,i)));
123     }
124
125     fRecentSources->Resize(100,20);
126     
127     TGButton* createRecentButton = new TGTextButton(fRecentSourceSelector,"Create data source");
128     createRecentButton->Connect("Clicked()",
129                                 "AliMUONPainterDataSourceFrame",
130                                 this,
131                                 "OpenRecentSource()");
132     
133     fRecentSourceSelector->AddFrame(fRecentSources,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
134     fRecentSourceSelector->AddFrame(createRecentButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
135                      
136     /// Raw file selection
137     
138     TGButton* openButton = new TGPictureButton(fRawSelector21,
139                                            gClient->GetPicture("fileopen.xpm"));
140     openButton->SetToolTipText("Click to open file dialog");
141                                         
142     fRawSelector2->AddFrame(fRawSelector21, new TGLayoutHints(kLHintsExpandX,5,5,5,5));
143     fRawSelector2->AddFrame(fRawSelector22, new TGLayoutHints(kLHintsExpandX,5,5,5,5));
144     fRawSelector2->AddFrame(fRawSelector24, new TGLayoutHints(kLHintsTop,5,5,5,5));
145     fRawSelector2->AddFrame(fRawSelector23, new TGLayoutHints(kLHintsExpandX,5,5,5,5));
146
147     fRawSelector21->AddFrame(openButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
148     fRawSelector21->AddFrame(fFilePath, new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
149
150     fRawSelector22->AddFrame(fCalibrateNoGain, new TGLayoutHints(kLHintsTop,5,5,5,5));
151     fRawSelector22->AddFrame(fCalibrateGainConstantCapa, new TGLayoutHints(kLHintsTop,5,5,5,5));
152     fRawSelector22->AddFrame(fCalibrateGain, new TGLayoutHints(kLHintsTop,5,5,5,5));
153     fRawSelector22->AddFrame(fCalibrateEmelecGain, new TGLayoutHints(kLHintsTop,5,5,5,5));
154   
155     fRawSelector24->AddFrame(fRawOCDBPath, new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
156     fRawOCDBPath->SetEnabled(kFALSE);
157     
158     fRawSelector23->AddFrame(fHistogramButton,new TGLayoutHints(kLHintsTop,5,5,5,5));    
159     fHistogramButton->Connect("Clicked()","AliMUONPainterDataSourceFrame",this,"HistogramButtonClicked()");
160     fHistoMin->SetState(kFALSE);
161     fHistoMax->SetState(kFALSE);    
162     fRawSelector23->AddFrame(fHistoMin,new TGLayoutHints(kLHintsTop,5,5,5,5));
163     fRawSelector23->AddFrame(fHistoMax,new TGLayoutHints(kLHintsTop,5,5,5,5));
164     
165
166   fRawSelector23->AddFrame(fEventRangeButton,new TGLayoutHints(kLHintsTop,5,5,5,5));    
167   fEventRangeButton->Connect("Clicked()","AliMUONPainterDataSourceFrame",this,"EventRangeButtonClicked()");
168   fEventMin->SetState(kFALSE);
169   fEventMax->SetState(kFALSE);      
170   
171   fEventMin->SetFormat(TGNumberFormat::kNESInteger);
172   fEventMax->SetFormat(TGNumberFormat::kNESInteger);
173
174   fRawSelector23->AddFrame(fEventMin,new TGLayoutHints(kLHintsTop,5,5,5,5));
175   fRawSelector23->AddFrame(fEventMax,new TGLayoutHints(kLHintsTop,5,5,5,5));
176   
177     TGButton* createRawButton = new TGTextButton(fRawSelector,"Create data source");
178     
179     fRawSelector->AddFrame(fRawSelector2, new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
180     fRawSelector->AddFrame(createRawButton, new TGLayoutHints(kLHintsCenterY,5,5,5,5));
181         
182     fCalibrateNoGain->Connect("Clicked()","AliMUONPainterDataSourceFrame",this,"CalibrateButtonClicked()");
183     fCalibrateGainConstantCapa->Connect("Clicked()","AliMUONPainterDataSourceFrame",this,"CalibrateButtonClicked()");
184     fCalibrateGain->Connect("Clicked()","AliMUONPainterDataSourceFrame",this,"CalibrateButtonClicked()");
185   fCalibrateEmelecGain->Connect("Clicked()","AliMUONPainterDataSourceFrame",this,"CalibrateButtonClicked()");
186
187     openButton->Connect("Clicked()",
188                         "AliMUONPainterDataSourceFrame",
189                         this,
190                         "OpenFileDialog()");
191
192     createRawButton->Connect("Clicked()",
193                         "AliMUONPainterDataSourceFrame",
194                         this,
195                         "CreateRawDataSource()");
196     
197     /// OCDB selection
198     
199   fOCDBTypes->AddEntry("Config",7);
200   fOCDBTypes->AddEntry("Occupancy",4);
201   fOCDBTypes->AddEntry("HV",3);
202   fOCDBTypes->AddEntry("Pedestals",0);
203   fOCDBTypes->AddEntry("Gains",1);
204   fOCDBTypes->AddEntry("StatusMap",5);
205   fOCDBTypes->AddEntry("Status",6);
206   fOCDBTypes->AddEntry("Capacitances",2);
207   fOCDBTypes->AddEntry("RejectList",8);
208   fOCDBTypes->Select(0);
209   fOCDBTypes->Resize(80,20);
210     
211     TGButton* createOCDBButton = new TGTextButton(fOCDBSelector,"Create data source");
212     createOCDBButton->Connect("Clicked()",
213                              "AliMUONPainterDataSourceFrame",
214                              this,
215                              "CreateOCDBDataSource()");
216
217   const char* ocdbToolTip = "Use URL style for either alien or local OCDB (foo://bar). For example :\n"
218   "alien://folder=/alice/data.../OCDB\n"
219   "or\nlocal:///home/user/aliroot (mind the 3 slashes there !)";
220   
221   fRawOCDBPath->SetToolTipText(ocdbToolTip);
222   fOCDBPath->SetToolTipText(ocdbToolTip);
223   
224     fOCDBSelector->AddFrame(fOCDBPath,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));    
225     fOCDBSelector->AddFrame(fRunSelector,new TGLayoutHints(kLHintsTop,5,5,5,5));
226     fOCDBSelector->AddFrame(fOCDBTypes,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
227     fOCDBSelector->AddFrame(createOCDBButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
228     
229     
230     /// ASCII calibration file selection
231     
232     TGButton* openButtonACF = new TGPictureButton(fACFSelector,
233                                                   gClient->GetPicture("fileopen.xpm"));
234     openButtonACF->SetToolTipText("Click to open file dialog");
235
236   fACFTypes->AddEntry("Config",7);
237   fACFTypes->AddEntry("Occupancy",4);
238   fACFTypes->AddEntry("Pedestals",0);
239   fACFTypes->AddEntry("Gains",1);
240   fACFTypes->AddEntry("Capacitances",2);
241   fACFTypes->Select(0);
242   fACFTypes->Resize(100,20);
243     
244     fACFSelector->AddFrame(openButtonACF,new TGLayoutHints(kLHintsTop,5,5,5,5));                                      
245     fACFSelector->AddFrame(fACFPath, new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
246     fACFSelector->AddFrame(fACFTypes,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
247
248     TGButton* createACFButton = new TGTextButton(fACFSelector,"Create data source");
249     createACFButton->Connect("Clicked()",
250                               "AliMUONPainterDataSourceFrame",
251                               this,                              
252                              "CreateACFDataSource()");
253     
254     openButtonACF->Connect("Clicked()",
255                            "AliMUONPainterDataSourceFrame",
256                            this,
257                            "OpenFileDialogACF()");
258     
259     fACFSelector->AddFrame(createACFButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
260
261     AddFrame(fRecentSourceSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
262
263     AddFrame(fRawSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
264
265     AddFrame(fOCDBSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
266
267     AddFrame(fACFSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
268     
269     AddFrame(fDataReaders, new TGLayoutHints(kLHintsExpandX,10,10,10,10));
270     
271 }
272
273 //_____________________________________________________________________________
274 AliMUONPainterDataSourceFrame::~AliMUONPainterDataSourceFrame()
275 {
276   /// dtor
277   
278   delete fItems;
279 }
280
281 //_____________________________________________________________________________
282 void
283 AliMUONPainterDataSourceFrame::AddRecentSource(const char* name)
284 {  
285   /// Add a source to the list of recently used sources
286   
287   TGListBox* lb = fRecentSources->GetListBox();
288   
289   for ( Int_t i = 0; i < lb->GetNumberOfEntries(); ++i ) 
290   {
291     TGTextLBEntry* t = (TGTextLBEntry*)lb->GetEntry(i);
292     TString s(t->GetText()->GetString());
293     if ( s == name ) 
294     {
295       return;
296     }
297   }
298   
299   fRecentSources->AddEntry(name,lb->GetNumberOfEntries());
300   fRecentSources->MapSubwindows();
301   fRecentSources->Layout();
302 }
303
304 //_____________________________________________________________________________
305 void
306 AliMUONPainterDataSourceFrame::CalibrateButtonClicked()
307 {
308   /// Calibrate button was clicked.
309   
310   if ( fCalibrateNoGain->IsOn() ||
311        fCalibrateGainConstantCapa->IsOn() ||
312        fCalibrateGain->IsOn() || 
313        fCalibrateEmelecGain->IsOn() ) 
314   {
315     fRawOCDBPath->SetEnabled(kTRUE);
316     fRawOCDBPath->SetFocus();
317   }
318   else
319   {
320     fRawOCDBPath->SetEnabled(kFALSE);
321   }
322 }
323
324 //_____________________________________________________________________________
325 void
326 AliMUONPainterDataSourceFrame::HistogramButtonClicked()
327 {
328   /// Histogram button was clicked.
329   
330   if ( fHistogramButton->IsOn() )
331   {
332     fHistoMin->SetState(kTRUE);
333     fHistoMax->SetState(kTRUE);
334   }
335   else
336   {
337     fHistoMin->SetState(kFALSE);
338     fHistoMax->SetState(kFALSE);
339   }
340 }
341
342 //_____________________________________________________________________________
343 void
344 AliMUONPainterDataSourceFrame::EventRangeButtonClicked()
345 {
346   /// EventRange button was clicked.
347   
348   if ( fEventRangeButton->IsOn() )
349   {
350     fEventMin->SetState(kTRUE);
351     fEventMax->SetState(kTRUE);
352   }
353   else
354   {
355     fEventMin->SetIntNumber(-1);
356     fEventMax->SetIntNumber(-1);
357     fEventMin->SetState(kFALSE);
358     fEventMax->SetState(kFALSE);
359   }
360 }
361
362 //_____________________________________________________________________________
363 void
364 AliMUONPainterDataSourceFrame::CreateACFDataSource()
365 {
366   /// Create an ACF data source (using information from the widgets)
367   
368   TString acfPath = fACFPath->GetText();
369   TGTextLBEntry* t = static_cast<TGTextLBEntry*>(fACFTypes->GetSelectedEntry());
370   TString type = t->GetText()->GetString();
371   
372   CreateACFDataSource(acfPath,type);
373   
374   fACFPath->SetText("");
375 }
376
377
378 //_____________________________________________________________________________
379 void
380 AliMUONPainterDataSourceFrame::CreateOCDBDataSource()
381 {
382   /// Create an OCDB data source (using information from the widgets)
383   
384   TString cdbPath = fOCDBPath->GetText();
385   Int_t runNumber = fRunSelector->GetIntNumber();
386   TGTextLBEntry* t = static_cast<TGTextLBEntry*>(fOCDBTypes->GetSelectedEntry());
387   TString type = t->GetText()->GetString();
388   
389   CreateOCDBDataSource(cdbPath,runNumber,type);  
390 }
391
392 //_____________________________________________________________________________
393 void
394 AliMUONPainterDataSourceFrame::CreateACFDataSource(const TString& uri)
395 {
396   /// Create an ACF data source, given it's URI
397   
398   TObjArray* a = uri.Tokenize(";");
399   TString acfPath = static_cast<TObjString*>(a->At(1))->String();
400   TString type = static_cast<TObjString*>(a->At(2))->String();
401   
402   CreateACFDataSource(acfPath,type);
403   
404   delete a;
405 }
406
407
408 //_____________________________________________________________________________
409 void
410 AliMUONPainterDataSourceFrame::CreateOCDBDataSource(const TString& uri)
411 {
412   /// Create an OCDB data source, given it's URI
413   
414   TObjArray* a = uri.Tokenize(";");
415   TString cdbPath = static_cast<TObjString*>(a->At(1))->String();
416   TString srun = static_cast<TObjString*>(a->At(2))->String();
417   TString type = static_cast<TObjString*>(a->At(3))->String();
418   
419   CreateOCDBDataSource(cdbPath,atoi(srun.Data()),type);
420   
421   delete a;
422 }
423
424 //_____________________________________________________________________________
425 void
426 AliMUONPainterDataSourceFrame::RegisterDataSource(AliMUONVTrackerDataMaker* reader,
427                                                   const char* dsName)
428 {
429   /// Register a new data source
430  
431   if ( reader && reader->IsValid() ) 
432   {
433     AliMUONMchViewApplication* app = dynamic_cast<AliMUONMchViewApplication*>(gApplication);
434     if (!app)
435     {
436       AliError("Could not cast application to the expected type ! CHECK THAT !");
437     }
438     
439     AliMUONPainterDataRegistry::Instance()->Register(reader);
440     
441     AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
442     
443     Int_t n = env->Integer(fgkNumberOfDataSourcesKey);
444     
445     env->Set(fgkNumberOfDataSourcesKey,n+1);
446     
447     env->Set(Form(fgkDataSourceURIKey,n),dsName);
448     
449     env->Save();
450     
451     AddRecentSource(dsName);
452     
453     if ( app ) 
454     {
455       
456       TString name(dsName);
457       name.ToUpper();
458       
459       if ( name.Contains("PED") )
460       {
461         CreatePedestalCanvases(reader->Data());
462       }
463     }
464   }  
465 }
466
467 //_____________________________________________________________________________
468 void
469 AliMUONPainterDataSourceFrame::CreatePedestalCanvases(AliMUONVTrackerData* data,
470                                                       Double_t pedMin, Double_t pedMax,
471                                                       Double_t sigmaMin, Double_t sigmaMax)
472 {
473   /// Create 4 canvases with the pedestals contained in data
474   /// to show mean and sigma, for bending and non bending, with given limits
475                                                  
476   TList matrices;
477   
478   AliMUONAttPainter att[2];
479   
480   att[0].SetViewPoint(kTRUE,kFALSE);
481   att[0].SetCathode(kFALSE,kFALSE);
482   att[0].SetPlane(kTRUE,kFALSE);
483   
484   att[1].SetViewPoint(kTRUE,kFALSE);
485   att[1].SetCathode(kFALSE,kFALSE);
486   att[1].SetPlane(kFALSE,kTRUE);
487   
488   for ( Int_t iatt = 0; iatt < 2; ++iatt ) 
489   {
490     matrices.Add(CreateFullTracker(data,0,pedMin,pedMax,att[iatt]));
491     matrices.Add(CreateFullTracker(data,1,sigmaMin,sigmaMax,att[iatt]));
492   }
493   
494   TIter next(&matrices);
495   AliMUONPainterMatrix* matrix;
496   
497   Int_t w = TMath::Nint(gClient->GetDisplayWidth()*0.9);
498   Int_t h = TMath::Nint(gClient->GetDisplayHeight()*0.9);
499   
500   Int_t x[] = { 0, 0, 20 + w/2, 20 + w/2 };
501   Int_t y[] = { 0, h/2+30, 0, h/2+30 };
502   
503   Int_t i(0);
504   
505   while ( ( matrix = static_cast<AliMUONPainterMatrix*>(next())) )
506   {
507     TCanvas* c = matrix->CreateCanvas(x[i],y[i],w/2,h/2);
508     c->Draw();
509     c->SaveAs(Form("%s.png",c->GetName()));
510     ++i;
511   }
512 }
513
514 //_____________________________________________________________________________
515 AliMUONPainterMatrix*
516 AliMUONPainterDataSourceFrame::CreateFullTracker(AliMUONVTrackerData* data, 
517                                                  Int_t dim, 
518                                                  Double_t xmin, Double_t xmax,
519                                                  const AliMUONAttPainter& att)
520 {
521   /// Generate, draw and register a matrix of 10 painters to show all the tracker
522   /// chambers
523     
524   AliMUONPainterMatrix* matrix = new AliMUONPainterMatrix("Tracker",5,2);
525   
526   for ( Int_t ichamber = 0; ichamber < 10; ++ichamber )
527   {
528     AliMUONVPainter* painter = new AliMUONChamberPainter(att,ichamber);
529     
530     painter->SetResponder("BUSPATCH");
531     
532     painter->SetOutlined("*",kFALSE);
533     
534     matrix->Adopt(painter);    
535   }
536   
537   matrix->SetData("MANU",data,dim);    
538   matrix->SetDataRange(xmin,xmax);    
539   
540   AliMUONPainterRegistry::Instance()->Register(matrix);
541     
542   return matrix;
543 }
544
545
546 //_____________________________________________________________________________
547 void
548 AliMUONPainterDataSourceFrame::CreateACFDataSource(const TString& acfPath, const TString& type)
549 {
550   /// Create an ACF data source for a given (path,type) 
551
552   AliMUONVTrackerDataMaker* reader = new AliMUONTrackerConditionDataMaker(acfPath.Data(),
553                                                                           type.Data());
554   
555   RegisterDataSource(reader,Form("ACF;%s;%s",acfPath.Data(),type.Data()));
556 }
557
558 //_____________________________________________________________________________
559 void
560 AliMUONPainterDataSourceFrame::CreateOCDBDataSource(const TString& cdbPath,
561                                                     Int_t runNumber,
562                                                     const TString& type)
563 {
564   /// Create an OCDB data source for a given (path,runnumber,type) triplet
565   
566   AliMUONVTrackerDataMaker* reader = new AliMUONTrackerConditionDataMaker(runNumber,
567                                                                           cdbPath.Data(),
568                                                                           type.Data());
569   
570   RegisterDataSource(reader,Form("OCDB;%s;%d;%s",cdbPath.Data(),runNumber,type.Data()));
571 }
572
573 //_____________________________________________________________________________
574 void 
575 AliMUONPainterDataSourceFrame::CreateRawDataSource()
576 {
577   /// Create a new raw data source (using info from the widgets)
578   
579   TString uri(gSystem->ExpandPathName(fFilePath->GetText()));
580
581   TString name("RAW");
582   Bool_t fromMemory(kFALSE);
583   
584   if ( uri.Contains(TRegexp("^mem")) )
585   {
586     fromMemory = kTRUE;
587   }
588   else
589   {
590     if ( gSystem->AccessPathName(uri.Data()) )
591     {
592       AliError(Form("File %s does not exist",uri.Data()));
593       fFilePath->SetText("");
594       return;
595     }
596   }
597
598   TString calibMode("");
599   
600   if ( fCalibrateGain->IsOn() ) 
601   {
602     calibMode = "GAIN";
603     name = "CALC";
604   }
605   
606   if ( fCalibrateGainConstantCapa->IsOn() ) 
607   {
608     calibMode = "GAINCONSTANTCAPA";
609     name = "CALG";
610   }
611
612   if ( fCalibrateEmelecGain->IsOn() ) 
613   {
614     calibMode = "INJECTIONGAIN";
615     name = "CALE";
616   }
617   
618   if ( fCalibrateNoGain->IsOn() ) 
619   {
620     calibMode = "NOGAIN";
621     name = "CALZ";
622   }
623   
624   uri = Form("%s%s%s;%s;%s;%s;%s;%s;%s;%s",
625              ( fHistogramButton->IsOn() ? "H":""),
626              ( fromMemory ? "M" : ""),
627              name.Data(),uri.Data(),
628              ( strlen(fRawOCDBPath->GetText()) > 0 ? fRawOCDBPath->GetText() : " "),
629              ( calibMode.Length() > 0 ? calibMode.Data() : " "),
630              Form("%e",fHistoMin->GetNumber()),
631              Form("%e",fHistoMax->GetNumber()),
632              Form("%d",(Int_t)(fEventMin->GetIntNumber())),
633              Form("%d",(Int_t)(fEventMax->GetIntNumber())));
634   
635   if ( CreateRawDataSource(uri) )
636   {
637     fFilePath->SetText("");
638     fRawOCDBPath->SetText("");
639   }
640 }
641
642 //_____________________________________________________________________________
643 Bool_t 
644 AliMUONPainterDataSourceFrame::CreateRawDataSource(const TString& uri)
645 {
646   /// Create a new raw data source, given its URI
647   
648   TString filename;
649   TString ocdbPath;
650   TString calibMode;
651   TString sxmin("0.0");
652   TString sxmax("4096.0");
653   TString emin("-1");
654   TString emax("-1");
655   
656   TObjArray* a = uri.Tokenize(";");
657   
658   filename = static_cast<TObjString*>(a->At(1))->String();
659   
660   if ( a->GetLast() > 1 ) 
661   {
662     ocdbPath = static_cast<TObjString*>(a->At(2))->String();
663     if ( ocdbPath == " " ) ocdbPath = "";
664   }
665
666   if ( a->GetLast() > 2 ) 
667   {
668     calibMode = static_cast<TObjString*>(a->At(3))->String();
669     if ( calibMode == " " ) calibMode = "";
670   }
671
672   if ( a->GetLast() > 3 ) 
673   {
674     sxmin = static_cast<TObjString*>(a->At(4))->String();
675   }
676
677   if ( a->GetLast() > 4 ) 
678   {
679     sxmax = static_cast<TObjString*>(a->At(5))->String();
680   }
681   
682   if ( a->GetLast() > 5 )
683   {
684     emin = static_cast<TObjString*>(a->At(6))->String();
685   }
686
687   if ( a->GetLast() > 6 )
688   {
689     emax = static_cast<TObjString*>(a->At(7))->String();
690   }
691   
692   AliRawReader* rawReader = 0x0;
693
694   if ( filename.Contains(TRegexp("^alien")) )
695   {
696     // insure we've initialized the grid...
697     if (!gGrid)
698     {
699       TGrid::Connect("alien://");
700     }
701   }
702   
703   rawReader = AliRawReader::Create(filename.Data());
704
705   if (!rawReader)
706   {
707     AliError(Form("Could not open file %s",filename.Data()));
708     fFilePath->SetText("");
709     return kFALSE;
710   }
711   
712   /// Basic test to see if the file is correct
713   /// and to get run numbre
714   Int_t runNumber(-1);
715   Bool_t ok = rawReader->NextEvent();
716   if (!ok)
717   {
718     AliError(Form("File %s does not seem to be a raw data file",filename.Data()));    
719     fFilePath->SetText("");
720     return kFALSE;
721   }
722   else
723   {
724     runNumber = rawReader->GetRunNumber();    
725   }
726
727   rawReader->RewindEvents();
728   
729   AliMUONVTrackerDataMaker* reader(0x0);
730   Bool_t histogram(kFALSE);
731   
732   if ( uri.Contains(TRegexp("^H")) ) histogram = kTRUE;
733
734   if ( ocdbPath.Length() > 0 ) 
735   {
736         
737     AliMUONRecoParam* recoParam(0x0);
738     
739     AliCDBEntry* e = AliCDBManager::Instance()->Get("MUON/Calib/RecoParam",runNumber);
740     if (e)
741     {
742       TObject* o = e->GetObject();
743       if ( o->IsA() == TObjArray::Class() )
744       {
745         TIter next(static_cast<TObjArray*>(o));
746         AliMUONRecoParam* p;
747         while ( ( p = static_cast<AliMUONRecoParam*>(next()) ))
748         {
749           if ( p->IsDefault()) recoParam = p;
750         }
751       }
752       else
753       {
754         recoParam = static_cast<AliMUONRecoParam*>(o);
755       }
756     }
757     
758     reader = new AliMUONTrackerDataMaker(recoParam,
759                                          rawReader,
760                                          ocdbPath.Data(),
761                                          calibMode.Data(),
762                                          histogram,
763                                          sxmin.Atof(),
764                                          sxmax.Atof());
765   }
766   else
767   {
768     reader = new AliMUONTrackerDataMaker(rawReader,histogram);
769   }
770
771   reader->SetEventRange(emin.Atoi(),emax.Atoi());
772   
773   reader->SetSource(filename.Data());
774
775   TString dsName(uri);
776   
777   if ( emin.Atoi() <= emax.Atoi() )
778   {
779     // we have an event range
780     if ( emin.Atoi() == emax.Atoi())
781     {
782       dsName += Form("[%d]",emin.Atoi());
783     }
784     else
785     {
786       dsName += Form("[%d,%d]",emin.Atoi(),emax.Atoi());
787     }
788   }
789   
790   RegisterDataSource(reader,dsName.Data());
791                        
792   return kTRUE;
793 }
794
795 //_____________________________________________________________________________
796 void 
797 AliMUONPainterDataSourceFrame::DataMakerWasRegistered(AliMUONVTrackerDataMaker* reader)
798 {
799   /// Update ourselves as a new data reader was created
800   
801   AliMUONPainterDataSourceItem* item = new AliMUONPainterDataSourceItem(fDataReaders,100,20,reader);
802       
803   item->Connect("StartRunning()",
804                 "AliMUONPainterDataSourceFrame",
805                 this,
806                 "StartRunning()");
807
808   item->Connect("StopRunning()",
809                 "AliMUONPainterDataSourceFrame",
810                 this,
811                 "StopRunning()");
812   
813   fDataReaders->AddFrame(item);
814   
815   fItems->Add(item);
816
817   fDataReaders->MapSubwindows();
818   fDataReaders->Resize();
819 }
820
821 //_____________________________________________________________________________
822 void 
823 AliMUONPainterDataSourceFrame::DataMakerWasUnregistered(const AliMUONVTrackerDataMaker* maker)
824 {
825   /// Update ourselves as a data reader was deleted
826   
827   AliMUONPainterDataSourceItem* theItem(0x0);
828   
829   TIter next(fItems);
830   AliMUONPainterDataSourceItem* item;
831   
832   while ( ( item = static_cast<AliMUONPainterDataSourceItem*>(next()) ) && !theItem )
833   {
834     if ( item->DataMaker() == maker ) 
835     {
836       theItem = item;
837     }
838   }
839   
840   if  (!theItem) return;
841   
842   fDataReaders->RemoveFrame(theItem);
843   fItems->Remove(theItem);
844   theItem->DestroyWindow();
845   delete theItem;
846   
847   fDataReaders->MapSubwindows();
848   fDataReaders->Resize();
849
850 }
851
852 //_____________________________________________________________________________
853 void
854 AliMUONPainterDataSourceFrame::OpenFileDialog()
855 {
856   /// Open a file dialog to select a file to be read
857   
858   TGFileInfo fileInfo;
859   
860   const char* fileTypes[] = { 
861     "ROOT files","*.root",
862     "DATE files","*.raw",
863     "All files","*",
864     0,0 };
865   
866   fileInfo.fFileTypes = fileTypes;
867   delete[] fileInfo.fIniDir;
868
869   AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
870   
871   fileInfo.fIniDir = StrDup(env->String("LastOpenDir","."));
872   
873   new TGFileDialog(gClient->GetRoot(),gClient->GetRoot(),
874                    kFDOpen,&fileInfo);
875   
876   fFilePath->SetText(gSystem->ExpandPathName(Form("%s",fileInfo.fFilename)));
877   
878   env->Set("LastOpenDir",fileInfo.fIniDir);
879   env->Save();  
880 }
881
882
883 //_____________________________________________________________________________
884 void
885 AliMUONPainterDataSourceFrame::OpenFileDialogACF()
886 {
887   /// Open a file dialog to select an ASCII calibration file to be read
888   
889   TGFileInfo fileInfo;
890   
891   const char* fileTypes[] = { 
892     "All files","*",
893     0,0 };
894   
895   fileInfo.fFileTypes = fileTypes;
896   delete[] fileInfo.fIniDir;
897   
898   AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
899   
900   fileInfo.fIniDir = StrDup(env->String("LastOpenDirACF","."));
901   
902   new TGFileDialog(gClient->GetRoot(),gClient->GetRoot(),
903                    kFDOpen,&fileInfo);
904   
905   fACFPath->SetText(gSystem->ExpandPathName(Form("%s",fileInfo.fFilename)));
906   
907   env->Set("LastOpenDirACF",fileInfo.fIniDir);
908   env->Save();  
909 }
910
911
912 //_____________________________________________________________________________
913 void
914 AliMUONPainterDataSourceFrame::OpenRecentSource()
915 {
916   /// Open one source from the recently used ones
917   
918   TGTextLBEntry* t = (TGTextLBEntry*)fRecentSources->GetSelectedEntry();
919
920   TString uri(t->GetText()->GetString());
921   
922   if ( uri.Contains(TRegexp("^RAW")) || uri.Contains(TRegexp("^HRAW")) || 
923        uri.Contains(TRegexp("^CAL")) || uri.Contains(TRegexp("^HCAL")) ||
924        uri.Contains(TRegexp("^MEM")) )
925   {
926     CreateRawDataSource(uri);
927   }
928   else if ( uri.Contains(TRegexp("^OCDB")) )
929   {
930     CreateOCDBDataSource(uri);
931   }
932   else if ( uri.Contains(TRegexp("^ACF")) )
933   {
934     CreateACFDataSource(uri);
935   }
936   
937   fRecentSources->Select(-1);
938 }
939
940 //_____________________________________________________________________________
941 void
942 AliMUONPainterDataSourceFrame::StartRunning()
943 {
944   /// One data source starts running. Disable the Run button of the other ones
945   
946   AliMUONPainterDataSourceItem* item = reinterpret_cast<AliMUONPainterDataSourceItem*> (gTQSender);
947   
948   AliInfo("");
949   
950   TIter next(fItems);
951   AliMUONPainterDataSourceItem* o;
952   while ( ( o = static_cast<AliMUONPainterDataSourceItem*>(next()) ) )
953   {
954     if ( o != item ) 
955     {
956       o->DisableRun();
957     }
958   }
959 }  
960
961 //_____________________________________________________________________________
962 void
963 AliMUONPainterDataSourceFrame::StopRunning()
964 {
965   /// One data source stops running. Enable the Run button of all items
966   
967   TIter next(fItems);
968   AliMUONPainterDataSourceItem* o;
969   while ( ( o = static_cast<AliMUONPainterDataSourceItem*>(next()) ) )
970   {
971     o->EnableRun();
972   }
973 }
974