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