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