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