- added new task for pi0 reconstruction using purely calorimeter clusters
[u/mrichter/AliRoot.git] / MONITOR / AliEventServerWindow.cxx
1 // Author:  Mihai Niculesu 2013
2
3 /**************************************************************************
4  * Copyright(c) 1998-2013, ALICE Experiment at CERN, all rights reserved. *)
5  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
6  * full copyright notice.                                                 *
7  **************************************************************************/
8
9 #include <TEnv.h>
10 #include <TSystem.h>
11
12 #include <TSQLServer.h>
13 #include <TSQLResult.h>
14 #include <TSQLRow.h>
15
16 #include <TTimeStamp.h>
17 #include <TTimer.h>
18
19 #include <TGButton.h>
20 #include <TGListBox.h>
21 #include <TGTab.h>
22 #include <TGTextEntry.h>
23 #include <TGToolBar.h>
24 #include <TG3DLine.h>
25
26 #include <AliLog.h>
27 #include <AliReconstruction.h>
28
29 #include "AliEventServerUtil.h"
30 #include "AliEventServerWindow.h"
31 #include "AliEventServerPreferencesWindow.h"
32 #include "AliDimIntNotifier.h"
33 #include "AliRecoServer.h"
34
35 //______________________________________________________________________________
36 // Full description of AliEventServerWindow
37 //
38
39 ClassImp(AliEventServerWindow)
40
41 AliEventServerWindow::AliEventServerWindow() :
42   TGMainFrame(gClient->GetRoot(), 400, 400),
43   fRunList(0), 
44   fStartServButt(0), 
45   fStopServButt(0), 
46   fExitButt(0),
47   fRunRunning(0), 
48   fRecoServer(0)
49 {
50         SetCleanup(kDeepCleanup);
51
52         SetupToolbar();
53   
54         fRunList = new TGListBox(this);
55   
56         AddFrame(fRunList,  new TGLayoutHints(kLHintsNormal | kLHintsExpandX | kLHintsExpandY));
57         
58   for(Int_t i=0; i<5; ++i)
59   {
60         fDimSORListener[i] = 0;
61         fDimEORListener[i] = 0;
62   }
63   
64   Connect("CloseWindow()", "AliEventServerWindow", this, "onExit()");
65   SetWindowName("ALICE Event Server");
66  
67   MapSubwindows();
68   Resize(250,300);
69   MapWindow();
70   
71   FillRunsFromDatabase();
72   InitDIMListeners();
73 }
74
75 AliEventServerWindow::~AliEventServerWindow()
76 {
77   // Destructor.
78         
79         for (Int_t i = 0; i < 5; ++i)
80         {
81                 if(fDimSORListener[i]) delete fDimSORListener[i];
82                 if(fDimEORListener[i]) delete fDimEORListener[i];
83                 
84                 fDimSORListener[i] = 0;
85                 fDimEORListener[i] = 0;
86         }
87  
88 }
89
90 void AliEventServerWindow::InitDIMListeners()
91 {
92         // DIM interface.  
93    for (Int_t i = 0; i < 5; ++i)
94   {
95         if (i == 0)
96     {
97       fDimSORListener[i] = new AliDimIntNotifier("/LOGBOOK/SUBSCRIBE/DAQ_SOR_PHYSICS");
98       fDimEORListener[i] = new AliDimIntNotifier("/LOGBOOK/SUBSCRIBE/DAQ_EOR_PHYSICS");
99     }
100     else
101     {
102       fDimSORListener[i] = new AliDimIntNotifier(Form("/LOGBOOK/SUBSCRIBE/DAQ_SOR_PHYSICS_%d", i));
103       fDimEORListener[i] = new AliDimIntNotifier(Form("/LOGBOOK/SUBSCRIBE/DAQ_EOR_PHYSICS_%d", i));
104     }
105     
106     fDimSORListener[i]->Connect("DimMessage(Int_t)", "AliEventServerWindow", this, "StartOfRun(Int_t)");
107     fDimEORListener[i]->Connect("DimMessage(Int_t)", "AliEventServerWindow", this, "EndOfRun(Int_t)");
108   }
109
110 }
111
112 void AliEventServerWindow::FillRunsFromDatabase()
113 {
114         TEnv settings;
115         settings.ReadFile(AliEventServerUtil::GetPathToServerConf(), kEnvUser);
116         
117         TString dbHost = settings.GetValue("logbook.host", DEFAULT_LOGBOOK_HOST);
118         TString dbPort =  Form("%d", settings.GetValue("logbook.port", DEFAULT_LOGBOOK_PORT));
119         TString dbName =  settings.GetValue("logbook.db", DEFAULT_LOGBOOK_DB);
120         TString user =  settings.GetValue("logbook.user", DEFAULT_LOGBOOK_USER);
121         TString password = settings.GetValue("logbook.pass", DEFAULT_LOGBOOK_PASS);
122
123         TString connStr = Form("mysql://%s:%s/%s", dbHost.Data(), dbPort.Data(), dbName.Data()) ;
124
125         AliInfo(Form("connecting to %s", connStr.Data()) );
126
127   TSQLServer* server = TSQLServer::Connect(connStr.Data(), user.Data(), password.Data());
128   if (!server) {
129     AliWarning("ERROR: Could not connect to DAQ Logbook");
130     return;
131   }
132   TString sqlQuery;
133   TTimeStamp ts;
134   sqlQuery.Form("SELECT run FROM logbook WHERE DAQ_time_start > %u AND DAQ_time_end IS NULL AND `partition` REGEXP 'PHYSICS.*'",
135     (UInt_t)ts.GetSec()-86400);
136   TSQLResult* result = server->Query(sqlQuery);
137   if (!result)
138   {
139     AliWarning( Form("ERROR: Can't execute query <%s>!", sqlQuery.Data()) );
140     return;
141   }
142   if (result->GetRowCount() != 0)
143   {
144     for (Int_t iRow = 0; iRow < result->GetRowCount(); iRow++)
145     {
146       TSQLRow* row = result->Next();
147       TString runStr = row->GetField(0);
148       if (runStr.IsDigit())
149         StartOfRun(runStr.Atoi());
150       delete row;
151     }
152   }
153   delete result;
154
155 }
156
157 void AliEventServerWindow::SetupToolbar()
158 {
159         TGToolBar* mToolBar = new TGToolBar(this);
160         mToolBar->AddButton(this, new TGPictureButton(mToolBar, Form("%s/MONITOR/icons/start.png", gSystem->Getenv("ALICE_ROOT")), TOOLBUTTON_START ) );
161         mToolBar->AddButton(this, new TGPictureButton(mToolBar, Form("%s/MONITOR/icons/stop.png", gSystem->Getenv("ALICE_ROOT")), TOOLBUTTON_STOP) );
162         mToolBar->AddButton(this, new TGPictureButton(mToolBar, Form("%s/MONITOR/icons/preferences.png", gSystem->Getenv("ALICE_ROOT")), TOOLBUTTON_PREFERENCES) );
163         mToolBar->AddButton(this, new TGPictureButton(mToolBar, Form("%s/MONITOR/icons/exit.png", gSystem->Getenv("ALICE_ROOT")), TOOLBUTTON_EXIT) );
164         
165         mToolBar->Connect("Clicked(Int_t)", "AliEventServerWindow", this, "HandleToolBarAction(Int_t)");
166         
167         AddFrame(mToolBar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
168         AddFrame(new TGHorizontal3DLine(this), new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
169 }
170
171 void AliEventServerWindow::HandleToolBarAction(Int_t id)
172 {
173         if(id==-1) return;
174         
175         switch(id){
176         case TOOLBUTTON_START:{
177                 onStartServer();
178                 break;
179         }
180         case TOOLBUTTON_STOP:{
181                 onStopServer();
182                 break;
183         }
184         case TOOLBUTTON_PREFERENCES:{
185                 new AliEventServerPreferencesWindow(this, "Settings");
186                 break;
187         }
188         case TOOLBUTTON_EXIT:{
189                 onExit();
190                 break;
191         }
192         
193         }
194         
195 }
196
197 /*
198 void AliEventServerWindow::FinishedReconstruction(Int_t status)
199 {
200 // Slot called on termination of child process.
201         Int_t run = fServer->GetRunId();
202         
203   Info("FinishedReconstruction", "Reconstruction Thread finished \tRunId:%d \tstatus=%d", run, status);
204
205   mIntInt_i i =fRun2PidMap.find(run);
206   if (i != fRun2PidMap.end())
207   {
208     fRunList->RemoveEntry(run);
209     
210     // clean (remove) run's reconstructed directory
211     //gSystem->Exec(Form("rm -rf %s/reco/run%d_%d",gSystem->Getenv("ONLINERECO_BASE_DIR"),run,pid));
212       
213     if (status == 0)
214     {
215       fRunList->AddEntrySort(TString::Format("%-20d -- PROCESSED", run), run);
216     }
217     else
218     {
219       fRunList->AddEntrySort(TString::Format("%-20d -- PROCESSED [%d]", run, status), run);
220     }
221     fRunList->Layout();
222     
223   }
224   else
225   {
226     Warning("FinishedReconstruction", "Run number %d not registered.", run);
227   }
228
229 }
230 */
231 //------------------------------------------------------------------------------
232 // Private methods
233 //------------------------------------------------------------------------------
234
235 void AliEventServerWindow::StartReco(Int_t run)
236 {
237   AliInfo(Form("Starting Reco for run %d", run));
238   
239   TString eventSource = Form("mem://%s/run%d", gSystem->Getenv("ONLINERECO_RAWFILES_DIR"), run);
240   
241   if(!fRecoServer) LaunchRecoServer();
242     
243   fRecoServer->StartReconstruction(run, eventSource.Data());
244   
245   if(fRecoServer->IsListenning()){
246     fRunList->RemoveEntry(run);
247     fRunList->AddEntrySort(TString::Format("%-20d -- RUNNING", run), run);
248     fRunList->Layout();
249   }
250 }
251
252
253 //------------------------------------------------------------------------------
254 // Handlers of DIM signals.
255 //------------------------------------------------------------------------------
256
257 void AliEventServerWindow::StartOfRun(Int_t run)
258 {
259         if(run<=0) return;
260
261   // Slot called from DIM handler on start of run.
262         AliInfo(Form("called for Run %d ", run));
263
264         fRunList->AddEntrySort(TString::Format("%d", run), run);
265         fRunList->Layout();
266         gClient->NeedRedraw(fRunList);
267         
268         StartReco(run);
269 }
270
271 void AliEventServerWindow::EndOfRun(Int_t run)
272 {
273         if(run<=0) return;
274         
275    // Slot called from DIM handler on stop of run.
276         AliInfo(Form("called for Run %d", run) );
277         if(fRecoServer) fRecoServer->StopReconstruction();
278         
279   fRunList->RemoveEntry(run);
280   fRunList->Layout();
281   gClient->NeedRedraw(fRunList);
282 }
283
284 ///------------------------------------------------------------------------------
285 // Handlers of button signals.
286 //------------------------------------------------------------------------------
287
288 void AliEventServerWindow::onStartServer()
289 {
290   // Slot called from Start button.
291   AliInfo("Starting server...");
292         if(fRecoServer!=0) StopRecoServer();
293         
294         LaunchRecoServer();
295 }
296
297 void AliEventServerWindow::onStopServer()
298 {
299   // Slot called from Stop button.
300         AliInfo("Closing server...");
301         
302         StopRecoServer();
303 }
304
305 void AliEventServerWindow::onExit()
306 {
307         AliInfo("Closing server & Exiting...");
308         
309         StopRecoServer();
310         CloseWindow();
311         
312         gSystem->ExitLoop();
313 }
314
315 void AliEventServerWindow::LaunchRecoServer()
316 {
317         fRecoServer = new AliRecoServer;                
318 }
319
320 bool AliEventServerWindow::StopRecoServer()
321 {
322         if(fRecoServer==0) return true;
323
324         AliInfo("Closing server and stoping process...");
325         
326         delete fRecoServer;
327         fRecoServer=0;  
328
329         return true;
330 }