]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPainterDataSourceFrame.cxx
Bug fix (Marek)
[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 "AliMUONPainterDataSourceFrame.h"
19
20 #include "AliMUONPainterDataSourceItem.h"
21 #include "AliMUONPainterEnv.h"
22 #include "AliMUONPainterHelper.h"
23 #include "AliMUONPainterRegistry.h"
24 #include "AliMUONTrackerOCDBDataMaker.h"
25 #include "AliMUONTrackerRawDataMaker.h"
26 #include "AliMUONVTrackerDataMaker.h"
27 #include "AliLog.h"
28 #include "AliRawReaderDate.h"
29 #include "AliRawReaderRoot.h"
30 #include <TGButton.h>
31 #include <TGComboBox.h>
32 #include <TGFileDialog.h>
33 #include <TGNumberEntry.h>
34 #include <TGTextEntry.h>
35 #include <TGrid.h>
36 #include <TObjArray.h>
37 #include <TObjString.h>
38 #include <TRegexp.h>
39 #include <TString.h>
40 #include <TSystem.h>
41
42 ///\class AliMUONPainterDataSourceFrame
43 ///
44 /// A complete frame to select and display various data sources to 
45 /// be displayed : either raw data or OCDB data. 
46 /// Later on we might add digits and clusters for instance.
47 ///
48 ///\author Laurent Aphecetche, Subatech
49
50 const char* AliMUONPainterDataSourceFrame::fgkNumberOfDataSourcesKey = "NumberOfDataSources";
51 const char* AliMUONPainterDataSourceFrame::fgkDataSourceURIKey = "DataSourceURI.%d";
52
53 ///\cond CLASSIMP
54 ClassImp(AliMUONPainterDataSourceFrame)
55 ///\endcond
56
57 //_____________________________________________________________________________
58 AliMUONPainterDataSourceFrame::AliMUONPainterDataSourceFrame(const TGWindow* p, UInt_t w, UInt_t h)
59 : TGCompositeFrame(p,w,h,kVerticalFrame),
60   fRecentSourceSelector(new TGGroupFrame(p,"Recent sources",kHorizontalFrame)),
61   fRawSelector(new TGGroupFrame(p,"Raw file URI",kHorizontalFrame)),
62   fOCDBSelector(new TGGroupFrame(p,"OCDB Path",kHorizontalFrame)),
63   fDataReaders(new TGGroupFrame(p,"Data sources")),
64   fFilePath(new TGTextEntry(fRawSelector,"")),
65   fRawOCDBPath(new TGTextEntry(fRawSelector,"")),
66   fOCDBPath(new TGTextEntry(fOCDBSelector,"")),
67   fRunSelector(new TGNumberEntry(fOCDBSelector,0)),
68   fOCDBTypes(new TGComboBox(fOCDBSelector)),
69   fRecentSources(new TGComboBox(fRecentSourceSelector)),
70   fItems(new TObjArray)
71 {
72   /// Ctor
73   
74     AliMUONPainterRegistry* reg = AliMUONPainterRegistry::Instance();
75     
76     reg->Connect("DataReaderWasRegistered(AliMUONVTrackerDataMaker*)",
77                  "AliMUONPainterDataSourceFrame",
78                  this,
79                  "DataReaderWasRegistered(AliMUONVTrackerDataMaker*)");
80     
81     reg->Connect("DataReaderWasUnregistered(AliMUONVTrackerDataMaker*)",
82                  "AliMUONPainterDataSourceFrame",
83                  this,
84                  "DataReaderWasUnregistered(AliMUONVTrackerDataMaker*)");
85     
86     fItems->SetOwner(kFALSE);
87     
88     /// Recent source selection
89     
90     AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
91     
92     Int_t nsources = env->Integer(fgkNumberOfDataSourcesKey);
93     
94     for ( Int_t i = 0; i < nsources; ++i )
95     {
96       AddRecentSource(env->String(Form(fgkDataSourceURIKey,i)));
97     }
98
99     fRecentSources->Resize(100,20);
100     
101     TGButton* createRecentButton = new TGTextButton(fRecentSourceSelector,"Create data source");
102     createRecentButton->Connect("Clicked()",
103                                 "AliMUONPainterDataSourceFrame",
104                                 this,
105                                 "OpenRecentSource()");
106     
107     fRecentSourceSelector->AddFrame(fRecentSources,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
108     fRecentSourceSelector->AddFrame(createRecentButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
109                                     
110     /// Raw file selection
111     
112     TGButton* openButton = new TGPictureButton(fRawSelector,
113                                            gClient->GetPicture("fileopen.xpm"));
114     openButton->SetToolTipText("Click to open file dialog");
115                                         
116     TGButton* createRawButton = new TGTextButton(fRawSelector,"Create data source");
117     
118     fRawSelector->AddFrame(fFilePath, new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
119     fRawSelector->AddFrame(fRawOCDBPath, new TGLayoutHints(kLHintsTop,5,5,5,5));
120     fRawSelector->AddFrame(openButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
121     fRawSelector->AddFrame(createRawButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
122     
123     openButton->Connect("Clicked()",
124                         "AliMUONPainterDataSourceFrame",
125                         this,
126                         "OpenFileDialog()");
127
128     createRawButton->Connect("Clicked()",
129                         "AliMUONPainterDataSourceFrame",
130                         this,
131                         "CreateRawDataSource()");
132     
133     /// OCDB selection
134     
135     fOCDBTypes->AddEntry("Pedestals",0);
136     fOCDBTypes->AddEntry("Gains",1);
137     fOCDBTypes->AddEntry("Capacitances",2);
138     fOCDBTypes->Select(0);
139     fOCDBTypes->Resize(100,20);
140     
141     TGButton* createOCDBButton = new TGTextButton(fOCDBSelector,"Create data source");
142     createOCDBButton->Connect("Clicked()",
143                              "AliMUONPainterDataSourceFrame",
144                              this,
145                              "CreateOCDBDataSource()");
146     
147     
148     fOCDBSelector->AddFrame(fOCDBPath,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));    
149     fOCDBSelector->AddFrame(fRunSelector,new TGLayoutHints(kLHintsTop,5,5,5,5));
150     fOCDBSelector->AddFrame(fOCDBTypes,new TGLayoutHints(kLHintsExpandX | kLHintsTop,5,5,5,5));
151     fOCDBSelector->AddFrame(createOCDBButton,new TGLayoutHints(kLHintsTop,5,5,5,5));
152
153     AddFrame(fRecentSourceSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
154
155     AddFrame(fRawSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
156
157     AddFrame(fOCDBSelector,new TGLayoutHints(kLHintsExpandX,10,10,10,10));
158
159     AddFrame(fDataReaders, new TGLayoutHints(kLHintsExpandX,10,10,10,10));
160     
161 }
162
163 //_____________________________________________________________________________
164 AliMUONPainterDataSourceFrame::~AliMUONPainterDataSourceFrame()
165 {
166   /// dtor
167   
168   delete fItems;
169 }
170
171 //_____________________________________________________________________________
172 void
173 AliMUONPainterDataSourceFrame::AddRecentSource(const char* name)
174 {  
175   /// Add a source to the list of recently used sources
176   
177   TGListBox* lb = fRecentSources->GetListBox();
178   
179   for ( Int_t i = 0; i < lb->GetNumberOfEntries(); ++i ) 
180   {
181     TGTextLBEntry* t = (TGTextLBEntry*)lb->GetEntry(i);
182     TString s(t->GetText()->GetString());
183     if ( s == name ) 
184     {
185       return;
186     }
187   }
188   
189   fRecentSources->AddEntry(name,lb->GetNumberOfEntries());
190   fRecentSources->MapSubwindows();
191   fRecentSources->Layout();
192 }
193
194 //_____________________________________________________________________________
195 void
196 AliMUONPainterDataSourceFrame::CreateOCDBDataSource()
197 {
198   /// Create an OCDB data source (using information from the widgets)
199   
200   TString cdbPath = fOCDBPath->GetText();
201   Int_t runNumber = fRunSelector->GetIntNumber();
202   TGTextLBEntry* t = static_cast<TGTextLBEntry*>(fOCDBTypes->GetSelectedEntry());
203   TString type = t->GetText()->GetString();
204   
205   CreateOCDBDataSource(cdbPath,runNumber,type);
206   
207   fOCDBPath->SetText("");
208   fRunSelector->SetNumber(0);  
209 }
210
211 //_____________________________________________________________________________
212 void
213 AliMUONPainterDataSourceFrame::CreateOCDBDataSource(const TString& uri)
214 {
215   /// Create an OCDB data source, given it's URI
216   
217   TObjArray* a = uri.Tokenize(";");
218   TString cdbPath = static_cast<TObjString*>(a->At(1))->String();
219   TString srun = static_cast<TObjString*>(a->At(2))->String();
220   TString type = static_cast<TObjString*>(a->At(3))->String();
221   
222   CreateOCDBDataSource(cdbPath,atoi(srun.Data()),type);
223   
224   delete a;
225 }
226
227 //_____________________________________________________________________________
228 void
229 AliMUONPainterDataSourceFrame::CreateOCDBDataSource(const TString& cdbPath,
230                                                     Int_t runNumber,
231                                                     const TString& type)
232 {
233   /// Create an OCDB data source for a given (path,runnumber,type) triplet
234   
235   AliMUONVTrackerDataMaker* reader = new AliMUONTrackerOCDBDataMaker(cdbPath.Data(),
236                                                                        runNumber,
237                                                                        type.Data());
238   
239   if ( reader->IsValid() ) 
240   {
241     AliMUONPainterRegistry::Instance()->Register(reader);
242     
243     AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
244     
245     Int_t n = env->Integer(fgkNumberOfDataSourcesKey);
246     
247     env->Set(fgkNumberOfDataSourcesKey,n+1);
248     
249     TString ds(Form("OCDB;%s;%d;%s",cdbPath.Data(),runNumber,type.Data()));
250     
251     env->Set(Form(fgkDataSourceURIKey,n),ds.Data());
252     
253     env->Save();
254     
255     AddRecentSource(ds.Data());
256   }
257 }
258
259 //_____________________________________________________________________________
260 void 
261 AliMUONPainterDataSourceFrame::CreateRawDataSource()
262 {
263   /// Create a new raw data source (using info from the widgets)
264   
265   TString uri(gSystem->ExpandPathName(fFilePath->GetText()));
266   
267   if ( gSystem->AccessPathName(uri.Data()) )
268   {
269     AliError(Form("File %s does not exist",uri.Data()));
270     fFilePath->SetText("");
271     return;
272   }
273
274   uri = Form("RAW;%s;%s",uri.Data(),fRawOCDBPath->GetText());
275   
276   if ( CreateRawDataSource(uri) )
277   {
278     fFilePath->SetText("");
279     fRawOCDBPath->SetText("");
280   }
281 }
282
283 //_____________________________________________________________________________
284 Bool_t 
285 AliMUONPainterDataSourceFrame::CreateRawDataSource(const TString& uri)
286 {
287   /// Create a new raw data source, given its URI
288   
289   TString filename;
290   TString ocdbPath;
291   
292   TObjArray* a = uri.Tokenize(";");
293   
294   filename = static_cast<TObjString*>(a->At(1))->String();
295   
296   if ( a->GetLast() > 1 ) 
297   {
298     ocdbPath = static_cast<TObjString*>(a->At(2))->String();
299   }
300   
301   AliRawReader* rawReader = 0x0;
302
303   if ( filename.Contains(TRegexp(".root$")) ) 
304   {
305     AliDebug(1,"Using RawReaderRoot");
306     if ( filename.Contains(TRegexp("^alien")) )
307     {
308       // insure we've initialized the grid...
309       if (!gGrid)
310       {
311         TGrid::Connect("alien://");
312       }
313     }
314          
315     rawReader = new AliRawReaderRoot(filename.Data());
316   }
317   else if ( uri.Contains(TRegexp(".raw")) )
318   {
319     AliDebug(1,"Using RawReaderDate");
320     rawReader = new AliRawReaderDate(filename.Data());
321   }
322   else
323   {
324     AliError(Form("Don't know how to open that file : %s\nURI=%s",filename.Data(),uri.Data()));
325     return kFALSE;
326   }
327   
328   AliMUONTrackerRawDataMaker* reader = new AliMUONTrackerRawDataMaker(rawReader,ocdbPath.Data());
329   
330   reader->SetSource(filename.Data());
331   
332   AliMUONPainterRegistry::Instance()->Register(reader);
333   
334   AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
335   
336   Int_t n = env->Integer(fgkNumberOfDataSourcesKey);
337   
338   env->Set(fgkNumberOfDataSourcesKey,n+1);
339   
340   TString ds(Form("RAW;%s;%s",filename.Data(),ocdbPath.Data()));
341   
342   env->Set(Form(fgkDataSourceURIKey,n),ds.Data());
343   
344   AddRecentSource(ds.Data());
345   
346   env->Save();
347
348   return kTRUE;
349 }
350
351 //_____________________________________________________________________________
352 void 
353 AliMUONPainterDataSourceFrame::DataReaderWasRegistered(AliMUONVTrackerDataMaker* reader)
354 {
355   /// Update ourselves as a new data reader was created
356   
357   AliInfo(Form("%s",reader->GetName()));
358   
359   AliMUONPainterDataSourceItem* item = new AliMUONPainterDataSourceItem(fDataReaders,100,20,reader);
360       
361   fDataReaders->AddFrame(item);
362   
363   fItems->Add(item);
364
365   fDataReaders->MapSubwindows();
366   fDataReaders->Resize();
367 }
368
369 //_____________________________________________________________________________
370 void 
371 AliMUONPainterDataSourceFrame::DataReaderWasUnregistered(AliMUONVTrackerDataMaker* reader)
372 {
373   /// Update ourselves as a new data reader was deleted
374   
375   AliInfo(Form("%s",reader->GetName()));
376           
377 }
378
379 //_____________________________________________________________________________
380 void
381 AliMUONPainterDataSourceFrame::OpenFileDialog()
382 {
383   /// Open a file dialog to select a file to be read
384   
385   TGFileInfo fileInfo;
386   
387   new TGFileDialog(gClient->GetRoot(),gClient->GetRoot(),
388                    kFDOpen,&fileInfo);
389   
390   
391   fFilePath->SetText(gSystem->ExpandPathName(Form("%s",fileInfo.fFilename)));
392 }
393
394
395 //_____________________________________________________________________________
396 void
397 AliMUONPainterDataSourceFrame::OpenRecentSource()
398 {
399   /// Open one source from the recently used ones
400   
401   TGTextLBEntry* t = (TGTextLBEntry*)fRecentSources->GetSelectedEntry();
402
403   TString uri(t->GetText()->GetString());
404   
405   if ( uri.Contains(TRegexp("^RAW")) )
406   {
407     CreateRawDataSource(uri);
408   }
409   else if ( uri.Contains(TRegexp("^OCDB")) )
410   {
411     CreateOCDBDataSource(uri);
412   }
413   
414   fRecentSources->Select(-1);
415 }
416