o add Reset function to CalPad and CalROC o Add functionality to AliTPCdataQA - Reset...
[u/mrichter/AliRoot.git] / MUON / AliMUONPainterDataSourceItem.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 "AliMUONPainterDataSourceItem.h"
19
20 #include "AliMUONPainterEnv.h"
21 #include "AliMUONPainterHelper.h"
22 #include "AliMUONPainterDataRegistry.h"
23 #include "AliMUONVTrackerDataMaker.h"
24 #include "AliMUONVTrackerData.h"
25 #include "AliLog.h"
26 #include <TFile.h>
27 #include <TGFileDialog.h>
28 #include <TGLabel.h>
29 #include <TGButton.h>
30 #include <TSystem.h>
31 #include <TThread.h>
32 #include <Riostream.h>
33
34 ///\class AliMUONPainterDataSourceItem
35 ///
36 /// Widget to show one data source, and allow to run/stop/rewind/remove it
37 ///
38 /// WARNING : the thread business is not really working yet (AliRawReaders are
39 /// not really thread-safe for the moment). So please use a single raw data
40 /// source at a time, otherwise you'll get a crash !
41 ///
42 ///\author Laurent Aphecetche, Subatech
43
44 ///\cond CLASSIMP
45 ClassImp(AliMUONPainterDataSourceItem)
46 ///\endcond
47
48 namespace
49 {
50   void* RunFunction(void* args)
51   {
52     Long_t* params = (Long_t*)(args);
53     
54     AliMUONPainterDataSourceItem* calling = reinterpret_cast<AliMUONPainterDataSourceItem*>(params[0]);
55     AliMUONVTrackerDataMaker* reader = reinterpret_cast<AliMUONVTrackerDataMaker*> (params[1]);
56     
57     Bool_t ok(kTRUE);
58     
59     while ( ok ) 
60     {
61       ok = reader->NextEvent();
62       if ( reader->IsZombie() ) 
63       {
64         AliMUONPainterDataRegistry::Instance()->DeleteZombies();
65         return 0x0;
66       }
67       if ( !reader->IsRunning() ) gSystem->Sleep(1000);
68     }
69     
70     calling->Rewind();
71     
72     return 0x0;
73   }
74 }
75
76 //_____________________________________________________________________________
77 AliMUONPainterDataSourceItem::AliMUONPainterDataSourceItem(const TGWindow* p,
78                                                            UInt_t w, UInt_t h,
79                                                            AliMUONVTrackerDataMaker* maker)
80 : TGCompositeFrame(p,w,h,kHorizontalFrame),
81   fDataMaker(maker),
82   fSourceName(new TGLabel(this,maker->Data()->Name())),
83   fSource(new TGLabel(this,maker->Source().Data())),
84   fNumberOfEvents(new TGLabel(this,Form("%10d",0))),
85   fRun(0x0),
86   fStop(0x0),
87   fRewind(0x0),
88   fRemove(new TGTextButton(this,"Remove")),
89   fSave(new TGTextButton(this,"Save")),
90   fSaveAs(new TGTextButton(this,"Save As...")),
91   fThread(0x0),
92   fShouldReset(kFALSE)
93 {
94     /// ctor
95  
96     SetCleanup(kDeepCleanup);
97     
98     Update();
99     
100     AddFrame(fSourceName, new TGLayoutHints(kLHintsNormal | kLHintsCenterY,5,5,5,5));
101     AddFrame(fSource,new TGLayoutHints(kLHintsExpandX | kLHintsCenterY,5,5,5,5));
102     AddFrame(fNumberOfEvents,new TGLayoutHints(kLHintsNormal | kLHintsCenterY,5,5,5,5));
103
104     if ( fDataMaker->IsRunnable() ) 
105     {
106       fRun = new TGTextButton(this,"Run");
107       fStop = new TGTextButton(this,"Stop");
108       fRewind = new TGTextButton(this,"Rewind");
109       
110       fRun->SetEnabled(!maker->Data()->IsSingleEvent());
111       fRun->Connect("Clicked()",
112                     "AliMUONPainterDataSourceItem",
113                     this,
114                     "Run()");
115       
116       fStop->SetEnabled(kFALSE);
117       fStop->Connect("Clicked()",
118                      "AliMUONPainterDataSourceItem",
119                      this,
120                      "Stop()");
121       
122       fRewind->SetEnabled(kFALSE);
123       fRewind->Connect("Clicked()",
124                        "AliMUONPainterDataSourceItem",
125                        this,
126                        "Rewind()");
127       
128       AddFrame(fRun,new TGLayoutHints(kLHintsCenterY | kLHintsCenterY,5,5,5,5));
129       AddFrame(fStop,new TGLayoutHints(kLHintsCenterY | kLHintsCenterY,5,5,5,5));
130       AddFrame(fRewind,new TGLayoutHints(kLHintsCenterY | kLHintsCenterY,5,5,5,5));    
131     }
132
133     AddFrame(fRemove,new TGLayoutHints(kLHintsCenterY | kLHintsCenterY,5,5,5,5));    
134
135     AddFrame(fSave,new TGLayoutHints(kLHintsCenterY | kLHintsCenterY,5,5,5,5));    
136
137     AddFrame(fSaveAs,new TGLayoutHints(kLHintsCenterY | kLHintsCenterY,5,5,5,5));    
138
139     maker->Data()->Connect("NumberOfEventsChanged()",
140                             "AliMUONPainterDataSourceItem",
141                             this,
142                             "Update()");
143     
144     fRemove->Connect("Clicked()",
145                      "AliMUONPainterDataSourceItem",
146                      this,
147                      "Remove()");
148     
149     fSave->Connect("Clicked()",
150                    "AliMUONPainterDataSourceItem",
151                    this,
152                    "Save()");
153
154     fSaveAs->Connect("Clicked()",
155                    "AliMUONPainterDataSourceItem",
156                    this,
157                    "SaveWithDialog()");
158     
159     Resize();
160 }
161
162 //_____________________________________________________________________________
163 AliMUONPainterDataSourceItem::~AliMUONPainterDataSourceItem()
164 {
165   /// dtor
166   TThread::Delete(fThread);
167   delete fThread;
168 }
169
170
171 //_____________________________________________________________________________
172 void 
173 AliMUONPainterDataSourceItem::EnableRun() 
174
175   /// Enable run button
176   if ( fRun ) 
177   {
178     fRun->SetEnabled(kTRUE); 
179   }
180 }
181   
182 //_____________________________________________________________________________
183 void 
184 AliMUONPainterDataSourceItem::DisableRun() 
185
186   /// Disable run button
187   if ( fRun )
188   {
189     fRun->SetEnabled(kFALSE); 
190   }
191 }
192
193 //_____________________________________________________________________________
194 void
195 AliMUONPainterDataSourceItem::Remove()
196 {
197   /// Remove
198   
199   MakeZombie();
200   AliMUONPainterDataRegistry::Instance()->Unregister(fDataMaker);
201 }
202
203 //_____________________________________________________________________________
204 void
205 AliMUONPainterDataSourceItem::Reset()
206 {
207   /// Reset the data
208   fDataMaker->Data()->Clear();
209 }
210
211 //_____________________________________________________________________________
212 void
213 AliMUONPainterDataSourceItem::Rewind()
214 {
215   /// Rewind button was clicked
216   
217   fRewind->SetEnabled(kTRUE);
218   
219   Stop();
220   
221   TThread::Delete(fThread);
222   delete fThread;
223   fThread = 0x0;
224   
225   if ( fRun && fStop && fRewind ) 
226   {
227     fRun->SetEnabled(kTRUE);
228     fStop->SetEnabled(kFALSE);
229     fRewind->SetEnabled(kFALSE);
230   }
231   
232   fDataMaker->Rewind();
233   
234   fShouldReset = kTRUE;
235 }
236
237 //_____________________________________________________________________________
238 void
239 AliMUONPainterDataSourceItem::Run()
240 {
241   /// Run button was clicked
242   
243   StartRunning();
244   
245   if ( fShouldReset ) 
246   {
247     Reset();
248     fShouldReset = kFALSE;
249   }
250   
251   fRemove->SetEnabled(kFALSE);
252   
253   if (!fThread)
254   {
255     fParams[0] = (Long_t)(this);
256     fParams[1] = (Long_t)(fDataMaker);
257     fThread = new TThread(RunFunction,(void*)(&fParams[0]));
258     fThread->Run();
259   }
260   
261   fDataMaker->SetRunning(kTRUE);
262   
263   if ( fRun && fStop )
264   {
265     fRun->SetEnabled(kFALSE);
266     fStop->SetEnabled(kTRUE);
267   }
268 }
269
270 //_____________________________________________________________________________
271 void
272 AliMUONPainterDataSourceItem::Save(const char* filename)
273 {
274   /// Save the data maker
275   
276   TFile* f = TFile::Open(filename,"RECREATE");
277   
278   fDataMaker->Write();
279   
280   f->Write();
281   f->Close();
282   
283   delete f;
284 }
285
286 //_____________________________________________________________________________
287 void
288 AliMUONPainterDataSourceItem::Save()
289 {
290   /// Save the data maker (filename is fixed)
291   
292   TString dname(fDataMaker->Data()->GetName());
293   dname.ToLower();
294   
295   TString outputDir(AliMUONPainterHelper::Instance()->Env()->String("LastSaveDir","."));
296
297   TString filename(Form("%s/mchview.%s.root",gSystem->ExpandPathName(outputDir.Data()),dname.Data()));
298   
299   Save(filename.Data());
300 }
301
302 //_____________________________________________________________________________
303 void
304 AliMUONPainterDataSourceItem::SaveWithDialog()
305 {
306   /// Save the data maker (filename given by dialog)
307   
308   TGFileInfo fileInfo;
309   
310 //  fileInfo.fFileTypes = fgkFileTypes;
311   
312   delete[] fileInfo.fIniDir;
313   
314   AliMUONPainterEnv* env = AliMUONPainterHelper::Instance()->Env();
315   
316   fileInfo.fIniDir = StrDup(env->String("LastSaveDir","."));
317   
318   new TGFileDialog(gClient->GetRoot(),gClient->GetRoot(),
319                    kFDSave,&fileInfo);
320   
321   env->Set("LastSaveDir",fileInfo.fIniDir);
322   env->Save();  
323   
324   Save(fileInfo.fFilename);  
325 }
326
327 //_____________________________________________________________________________
328 void
329 AliMUONPainterDataSourceItem::Stop()
330 {
331   /// Stop button was clicked
332   
333   StopRunning();
334   
335   fDataMaker->SetRunning(kFALSE);
336   
337   if ( fStop && fRun ) 
338   {
339     fStop->SetEnabled(kFALSE);
340     fRun->SetEnabled(kTRUE);
341   }
342   
343   fRemove->SetEnabled(kTRUE);
344 }
345
346 //_____________________________________________________________________________
347 void
348 AliMUONPainterDataSourceItem::Update()
349 {
350   /// Update ourselves
351   
352   fNumberOfEvents->SetText(Form("%10d",fDataMaker->Data()->NumberOfEvents(-1)));
353 }
354
355 //_____________________________________________________________________________
356 void
357 AliMUONPainterDataSourceItem::StartRunning()
358 {
359   /// Signal we start to run
360   Emit("StartRunning()");
361 }  
362
363 //_____________________________________________________________________________
364 void
365 AliMUONPainterDataSourceItem::StopRunning()
366 {
367   /// Signal we stop to run
368   Emit("StopRunning()");
369 }