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