Parameterization of reconstruction algorithm.
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttleTrigger.cxx
CommitLineData
d477ad88 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/*
17 $Log$
45a493ce 18 Revision 1.3 2006/06/12 09:11:16 jgrosseo
19 coding conventions (Alberto)
20
58bc3020 21 Revision 1.2 2006/06/06 14:26:40 jgrosseo
22 o) removed files that were moved to STEER
23 o) shuttle updated to follow the new interface (Alberto)
24
b948db8d 25 Revision 1.1 2006/03/07 07:52:34 hristov
26 New version (B.Yordanov)
27
d477ad88 28 Revision 1.5 2005/11/21 09:03:48 byordano
29 one more print added
30
31 Revision 1.4 2005/11/20 10:12:37 byordano
32 comments added to AliShuttleTrigger
33
34 */
35
36
37//
38// This class is to deal with DAQ LogBook and DAQ "end of run" notification.
39// It has severeal two modes:
40// 1) syncrhnized - Collect(), CollectNew() and CollectAll methods
b948db8d 41// 2) asynchronized - Run() - starts listening for DAQ "end of run"
d477ad88 42// notification by DIM service.
43//
44
45#include "AliShuttleTrigger.h"
46
47#include <TSQLServer.h>
48#include <TSQLResult.h>
49#include <TSQLRow.h>
50#include <TObjArray.h>
51#include <TSystem.h>
52
53#include "AliLog.h"
b948db8d 54#include "AliCDBManager.h"
d477ad88 55#include "AliCDBStorage.h"
56#include "AliCDBEntry.h"
57
45a493ce 58#include "AliDCSValue.h"
d477ad88 59#include "AliShuttleConfig.h"
60#include "AliShuttle.h"
61#include "DATENotifier.h"
62
63ClassImp(TerminateSignalHandler)
b948db8d 64
58bc3020 65//______________________________________________________________________
66TerminateSignalHandler::TerminateSignalHandler(const TerminateSignalHandler& /*other*/):
67TSignalHandler()
68{
69// copy constructor (not implemented)
70
71}
72
73//______________________________________________________________________
74TerminateSignalHandler &TerminateSignalHandler::operator=(const TerminateSignalHandler& /*other*/)
75{
76// assignment operator (not implemented)
77
78return *this;
79}
80
b948db8d 81//______________________________________________________________________________________________
58bc3020 82Bool_t TerminateSignalHandler::Notify()
83{
84// Sentd terminate command to the Shuttle trigger
d477ad88 85
86 AliInfo("Terminate signal received ...");
87 fTrigger->Terminate();
88
89 return kTRUE;
90}
91
58bc3020 92//______________________________________________________________________________________________
93//______________________________________________________________________________________________
94
d477ad88 95ClassImp(AliShuttleTrigger)
b948db8d 96
97//______________________________________________________________________________________________
d477ad88 98AliShuttleTrigger::AliShuttleTrigger(const AliShuttleConfig* config,
b948db8d 99 UInt_t timeout, Int_t retries):
100 fConfig(config), fShuttle(NULL),
d477ad88 101 fNotified(kFALSE), fTerminate(kFALSE), fCondition(&fMutex),
102 fQuitSignalHandler(this, kSigQuit),
103 fInterruptSignalHandler(this, kSigInterrupt)
104{
105 //
106 // config - pointer to the AliShuttleConfig object which represents
107 // the configuration
b948db8d 108 // mainStorage - pointer to AliCDBStorage for the undelying CDBStorage
109 // localStorage (local) CDB storage to be used if mainStorage is unavailable
d477ad88 110 //
111
b948db8d 112 fShuttle = new AliShuttle(config, timeout, retries);
d477ad88 113
114 gSystem->AddSignalHandler(&fQuitSignalHandler);
115 gSystem->AddSignalHandler(&fInterruptSignalHandler);
116}
117
58bc3020 118
119
120//______________________________________________________________________
121AliShuttleTrigger::AliShuttleTrigger(const AliShuttleTrigger& /*other*/):
122TObject()
123{
124// copy constructor (not implemented)
125
126}
127
128//______________________________________________________________________
129AliShuttleTrigger &AliShuttleTrigger::operator=(const AliShuttleTrigger& /*other*/)
130{
131// assignment operator (not implemented)
132
133return *this;
134}
135
136
137
138
139
b948db8d 140//______________________________________________________________________________________________
58bc3020 141AliShuttleTrigger::~AliShuttleTrigger()
142{
143// destructor
d477ad88 144
145 gSystem->RemoveSignalHandler(&fQuitSignalHandler);
146 gSystem->RemoveSignalHandler(&fInterruptSignalHandler);
147
148 delete fShuttle;
149}
150
b948db8d 151//______________________________________________________________________________________________
d477ad88 152Bool_t AliShuttleTrigger::Notify() {
153 //
154 // Trigger CollectNew() methods in asynchronized (listen) mode.
155 // Usually called automaticly by DATENotifier on "end of run"
156 // notification event.
157 //
158
159 fMutex.Lock();
160
161 fNotified = kTRUE;
162 fCondition.Signal();
163
164 fMutex.UnLock();
165
166 return kTRUE;
167}
168
b948db8d 169//______________________________________________________________________________________________
d477ad88 170void AliShuttleTrigger::Terminate() {
171 //
172 // Stop triggers listen mode and exist from Run()
173 // Usually called automaticly by TerminateSignalHandler.
174 //
175
176 fTerminate = kTRUE;
177 fCondition.Signal();
178}
179
b948db8d 180//______________________________________________________________________________________________
d477ad88 181void AliShuttleTrigger::Run() {
182 //
183 // AliShuttleTrigger main loop for asynchronized (listen) mode.
184 // It spawns DIM service listener and waits for DAQ "end of run"
185 // notification. Calls CollectNew() on notification.
186 //
187
188 fTerminate = kFALSE;
189
190 DATENotifier* notifier = new DATENotifier(this, "/DATE/LOGBOOK/UPDATE");
191
192 while (1) {
193
194 fMutex.Lock();
195
196 while (!(fNotified || fTerminate)) {
197 fCondition.Wait();
198 }
199
200 fNotified = kFALSE;
201
202 fMutex.UnLock();
203
204 if (fTerminate) {
205 AliInfo("Terminated.");
206 break;
207 }
208
209 CollectNew();
210 }
211
212 delete notifier;
213}
214
b948db8d 215//______________________________________________________________________________________________
d477ad88 216Bool_t AliShuttleTrigger::RetrieveDATEEntries(const char* whereClause,
58bc3020 217 TObjArray& entries, Int_t& lastRun)
218{
219// Retrieve start time and end time for all runs in the DAQ logbook
220// that aren't processed yet
d477ad88 221
222 TString sqlQuery;
223 sqlQuery += "select run, time_start, time_end from logbook ";
224 sqlQuery += whereClause;
225 sqlQuery += " order by run";
226
227 TSQLServer* aServer;
b948db8d 228 TString logbookHost="mysql://";
229 logbookHost+=fConfig->GetDAQLogBookHost();
230
231 aServer = TSQLServer::Connect(logbookHost,
232 fConfig->GetDAQLogBookUser(),
233 fConfig->GetDAQLogBookPassword());
d477ad88 234 if (!aServer) {
235 AliError("Can't establish connection to DAQ log book DB!");
236 return kFALSE;
237 }
b948db8d 238
239 aServer->GetTables("REFSYSLOG");
d477ad88 240
241 TSQLResult* aResult;
242 aResult = aServer->Query(sqlQuery);
243 if (!aResult) {
244 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
245 delete aServer;
246 return kFALSE;
247 }
248
249 if (aResult->GetFieldCount() != 3) {
250 AliError("Invalid SQL result field number!");
251 delete aResult;
252 delete aServer;
253 return kFALSE;
254 }
255
256 lastRun = 0;
257
258 TSQLRow* aRow;
259 while ((aRow = aResult->Next())) {
260 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
261 Int_t run = runString.Atoi();
262
263 TString startTimeString(aRow->GetField(1),
264 aRow->GetFieldLength(1));
265 Int_t startTime = startTimeString.Atoi();
266 if (!startTime) {
267 AliWarning(Form("Zero StartTime for run <%d>!", run));
268 AliWarning("Going to skip this run!");
269 continue;
270 }
271
272 TString endTimeString(aRow->GetField(2),
273 aRow->GetFieldLength(2));
274 Int_t endTime = endTimeString.Atoi();
275 if (!endTime) {
276 AliWarning(Form("Zero EndTime for run <%d>!", run));
277 AliWarning("Going to skip this run!");
278 continue;
279 }
280
281 if (startTime > endTime) {
282 AliWarning(Form("StartTime bigger than EndTime for run <%d>", run));
283 AliWarning("Going to skip this run!");
284 continue;
285 }
286
58bc3020 287 entries.AddLast(new AliShuttleTriggerDATEEntry(run, startTime, endTime));
d477ad88 288 if (lastRun < run) {
289 lastRun = run;
290 }
291 delete aRow;
292 }
293
294 delete aResult;
295
296 aServer->Shutdown();
297 delete aServer;
298
299 entries.SetOwner(1);
300
301 return kTRUE;
302}
303
b948db8d 304//______________________________________________________________________________________________
45a493ce 305Bool_t AliShuttleTrigger::RetrieveConditionsData(const TObjArray& dateEntries)
58bc3020 306{
307// Retrieve conditions data for all runs that aren't processed yet
d477ad88 308
309 Bool_t hasError = kFALSE;
310
311 TIter iter(&dateEntries);
58bc3020 312 AliShuttleTriggerDATEEntry* anEntry;
313 while ((anEntry = (AliShuttleTriggerDATEEntry*) iter.Next())) {
b948db8d 314 if(!fShuttle->Process(anEntry->GetRun(),
d477ad88 315 anEntry->GetStartTime(),
316 anEntry->GetEndTime())) {
317 hasError = kTRUE;
b948db8d 318 }
d477ad88 319 }
320
321 return !hasError;
322}
323
b948db8d 324//______________________________________________________________________________________________
58bc3020 325Bool_t AliShuttleTrigger::Collect(Int_t run)
326{
d477ad88 327 //
328 // Collects conditions date for the given run.
329 //
330
331 AliInfo(Form("Collecting conditions data for run <%d> ...", run));
332
333 TString whereClause("where run = ");
334 whereClause += run;
335
336 Int_t lastRun;
337 TObjArray dateEntries;
338 if (!RetrieveDATEEntries(whereClause, dateEntries, lastRun)) {
339 AliError("Can't retrieve entries from DAQ log book.");
340 return kFALSE;
341 }
342
343 if (!dateEntries.GetEntriesFast()) {
344 AliError(Form("There isn't entry for run <%d> in DAQ log book!",
345 run));
346 return kFALSE;
347 }
348
349 if (dateEntries.GetEntriesFast() > 1) {
350 AliError(Form("There is more than one entry for run <%d> in DAQ log book", run));
351 return kFALSE;
352 }
353
354 if (!RetrieveConditionsData(dateEntries)) {
355 AliError("An error occured during conditions data retrieval!");
356 return kFALSE;
357 }
358
359 return kTRUE;
360}
361
b948db8d 362//______________________________________________________________________________________________
58bc3020 363Bool_t AliShuttleTrigger::CollectNew()
364{
d477ad88 365 //
366 // Collects conditions data for all new run written to DAQ LogBook.
367 //
368
45a493ce 369 // TODO revise this! last run number is ONLY allowed to be written when run was processed successfully!!!
370
d477ad88 371 AliInfo("Collecting conditions data for new runs ...");
372
373 Int_t lastRun;
374
b948db8d 375 AliCDBEntry* cdbEntry = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
376 ->Get("/SHUTTLE/SYSTEM/LASTRUN", 0);
d477ad88 377 if (cdbEntry) {
378 TObject* anObject = cdbEntry->GetObject();
b948db8d 379 if (anObject == NULL ||
45a493ce 380 anObject->IsA() != AliDCSValue::Class()) {
d477ad88 381 AliError("Invalid last run object stored to CDB!");
382 return kFALSE;
383 }
45a493ce 384 AliDCSValue* simpleValue = (AliDCSValue*) anObject;
d477ad88 385 lastRun = simpleValue->GetInt();
386 delete cdbEntry;
387 } else {
b948db8d 388 AliWarning("There isn't last run stored! Starting from run 21200");
45a493ce 389 lastRun = 21200; // TODO maybe exit here
d477ad88 390 }
391
392 AliInfo(Form("Last run number <%d>", lastRun));
393
394 TString whereClause("where run > ");
395 whereClause += lastRun;
396
397 Int_t newLastRun;
398 TObjArray dateEntries;
399 if (!RetrieveDATEEntries(whereClause, dateEntries, newLastRun)) {
400 AliError("Can't retrieve entries from DAQ log book.");
401 return kFALSE;
402 }
403
404 if (newLastRun > lastRun) {
45a493ce 405 AliDCSValue lastRunObj(newLastRun, 0);
d477ad88 406 AliCDBMetaData metaData;
407 AliCDBId cdbID(AliCDBPath("SHUTTLE", "SYSTEM", "LASTRUN"), 0, 0);
408
b948db8d 409 UInt_t result = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
410 ->Put(&lastRunObj, cdbID, &metaData);
411
412 if (!result) {
d477ad88 413 AliError("Can't store last run to CDB!");
414 return kFALSE;
415 }
416 }
417
418 if (!RetrieveConditionsData(dateEntries)) {
419 AliError("An error occured during conditions data retrieval!");
420 return kFALSE;
421 }
422
423 return kTRUE;
424}
425
b948db8d 426//______________________________________________________________________________________________
58bc3020 427Bool_t AliShuttleTrigger::CollectAll()
428{
d477ad88 429 //
430 // Collects conditions data for all run written in DAQ LogBook.
431 //
432
433 AliInfo("Collecting conditions data for all runs ...");
434
435 Int_t lastRun;
436 TObjArray dateEntries;
437 if (!RetrieveDATEEntries("", dateEntries, lastRun)) {
438 AliError("Can't retrieve entries from DAQ log book.");
439 return kFALSE;
440 }
441
442 if (!RetrieveConditionsData(dateEntries)) {
443 AliError("An error occured during conditions data retrieval!");
444 return kFALSE;
445 }
446
447 return kTRUE;
448}
449