]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MONITOR/alieventserver/AliEventServer.cxx
repairing server's action after EOR signal
[u/mrichter/AliRoot.git] / MONITOR / alieventserver / AliEventServer.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 #include <iostream>
19
20 #include <AliLog.h>
21 #include <AliReconstruction.h>
22 #include <AliDimIntNotifier.h>
23
24 #include "AliEventServerUtil.h"
25 #include "AliEventServer.h"
26 #include "AliEventServerReconstruction.h"
27
28 ClassImp(AliEventServer)
29
30 using namespace std;
31
32 AliEventServer::AliEventServer() :
33         fRecoServer(0)
34 {
35         fRecoServer = new AliEventServerReconstruction();
36         for(Int_t i=0; i<5; ++i)
37         {
38                 fDimSORListener[i] = 0;
39                 fDimEORListener[i] = 0;
40         }
41         FillRunsFromDatabase();
42         InitDIMListeners();
43 }
44
45 AliEventServer::~AliEventServer()
46 {
47         for (Int_t i = 0; i < 5; ++i)
48         {
49                 if(fDimSORListener[i]) delete fDimSORListener[i];
50                 if(fDimEORListener[i]) delete fDimEORListener[i];
51                 
52                 fDimSORListener[i] = 0;
53                 fDimEORListener[i] = 0;
54         }
55         if(fRecoServer){delete fRecoServer;fRecoServer=0;}
56 }
57
58 void AliEventServer::InitDIMListeners()
59 {
60         // DIM interface.  
61         for (Int_t i = 0; i < 5; ++i)
62         {
63                 if (i == 0)
64                 {
65                         fDimSORListener[i] = new AliDimIntNotifier("/LOGBOOK/SUBSCRIBE/DAQ_SOR_PHYSICS");
66                         fDimEORListener[i] = new AliDimIntNotifier("/LOGBOOK/SUBSCRIBE/DAQ_EOR_PHYSICS");
67                 }
68                 else
69                 {
70                         fDimSORListener[i] = new AliDimIntNotifier(Form("/LOGBOOK/SUBSCRIBE/DAQ_SOR_PHYSICS_%d", i));
71                         fDimEORListener[i] = new AliDimIntNotifier(Form("/LOGBOOK/SUBSCRIBE/DAQ_EOR_PHYSICS_%d", i));
72                 }
73     
74                 fDimSORListener[i]->Connect("DimMessage(Int_t)", "AliEventServer", this, "StartOfRun(Int_t)");
75                 fDimEORListener[i]->Connect("DimMessage(Int_t)", "AliEventServer", this, "EndOfRun(Int_t)");
76         }
77
78 }
79
80 void AliEventServer::StartOfRun(Int_t run)
81 {
82   cout<<"SOR signal received for run:"<<run<<endl;
83   if(run<=0) return;
84   /*
85   while(!fRecoServer->StopReconstruction())
86     {
87       cout<<"Waiting for previous reco to be fully initialized"<<endl;
88       sleep(10);
89       }*/
90   fRecoServer->StopReconstruction();
91
92   TEnv settings;
93   settings.ReadFile(AliEventServerUtil::GetPathToServerConf(), kEnvUser);
94     
95   TString dataSource = settings.GetValue("data.source", DEFAULT_DATA_SOURCE);
96   TString eventSource;
97     
98   if(dataSource=="local")
99     {
100       cout<<"Starting Reco for run "<<run<<endl;
101       eventSource = Form("mem://%s/run%d", gSystem->Getenv("ONLINERECO_RAWFILES_DIR"), run);
102     }
103   else if(dataSource=="run")
104     {
105       cout<<"Starting Reco for GDCs active in current run:"<<run<<endl;
106       eventSource = "mem://@*:";
107     }
108      
109   fRecoServer->StartReconstruction(run, eventSource.Data());
110 }
111
112 void AliEventServer::EndOfRun(Int_t run)
113 {
114   cout<<"EOR signal received for run:"<<run<<endl;
115   if(run<=0) return;
116   fRecoServer->StopReconstruction();
117 }
118
119 void AliEventServer::FillRunsFromDatabase()
120 {
121         TEnv settings;
122         settings.ReadFile(AliEventServerUtil::GetPathToServerConf(), kEnvUser);
123         
124         TString dbHost = settings.GetValue("logbook.host", DEFAULT_LOGBOOK_HOST);
125         TString dbPort =  Form("%d", settings.GetValue("logbook.port", DEFAULT_LOGBOOK_PORT));
126         TString dbName =  settings.GetValue("logbook.db", DEFAULT_LOGBOOK_DB);
127         TString user =  settings.GetValue("logbook.user", DEFAULT_LOGBOOK_USER);
128         TString password = settings.GetValue("logbook.pass", DEFAULT_LOGBOOK_PASS);
129
130         TString connStr = Form("mysql://%s:%s/%s", dbHost.Data(), dbPort.Data(), dbName.Data()) ;
131
132         cout<<"connecting to:"<<connStr<<endl;
133
134         TSQLServer* server = TSQLServer::Connect(connStr.Data(), user.Data(), password.Data());
135         if (!server)
136         {
137           cout<<"ERROR: Could not connect to DAQ Logbook"<<endl;
138                 return;
139         }
140         TString sqlQuery;
141         TTimeStamp ts;
142         sqlQuery.Form("SELECT run FROM logbook WHERE DAQ_time_start > %u AND DAQ_time_end IS NULL AND `partition` REGEXP 'PHYSICS.*'",
143                       (UInt_t)ts.GetSec()-86400);
144         TSQLResult* result = server->Query(sqlQuery);
145         if (!result)
146         {
147           cout<<"ERROR: Can't execute query:"<< sqlQuery<<endl;
148                 return;
149         }
150         if (result->GetRowCount() != 0)
151         {
152                 for (Int_t iRow = 0; iRow < result->GetRowCount(); iRow++)
153                 {
154                         TSQLRow* row = result->Next();
155                         TString runStr = row->GetField(0);
156                         if (runStr.IsDigit())
157                                 StartOfRun(runStr.Atoi());
158                         delete row;
159                 }
160         }
161         delete result;
162
163 }
164
165
166 /*
167 void AliEventServer::FinishedReconstruction(Int_t status)
168 {
169   // Slot called on termination of child process.
170   Int_t run = fServer->GetRunId();
171         
172   Info("FinishedReconstruction", "Reconstruction Thread finished \tRunId:%d \tstatus=%d", run, status);
173
174   mIntInt_i i =fRun2PidMap.find(run);
175   if (i != fRun2PidMap.end())
176     {
177       fRunList->RemoveEntry(run);
178     
179       // clean (remove) run's reconstructed directory
180       //gSystem->Exec(Form("rm -rf %s/reco/run%d_%d",gSystem->Getenv("ONLINERECO_BASE_DIR"),run,pid));
181       
182       if (status == 0)
183         {
184           fRunList->AddEntrySort(TString::Format("%-20d -- PROCESSED", run), run);
185         }
186       else
187         {
188           fRunList->AddEntrySort(TString::Format("%-20d -- PROCESSED [%d]", run, status), run);
189         }
190       fRunList->Layout();
191     
192     }
193   else
194     {
195       Warning("FinishedReconstruction", "Run number %d not registered.", run);
196     }
197
198     }*/