]> git.uio.no Git - u/mrichter/AliRoot.git/blob - SHUTTLE/TestShuttle/AliTestShuttle.cxx
Adding functionality to the Shuttle/TestShuttle:
[u/mrichter/AliRoot.git] / SHUTTLE / TestShuttle / AliTestShuttle.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 /*
17 $Log$
18 Revision 1.12  2007/04/27 07:06:48  jgrosseo
19 GetFileSources returns empty list in case of no files, but successful query
20 No mails sent in testmode
21
22 Revision 1.11  2007/04/04 10:33:36  jgrosseo
23 1) Storing of files to the Grid is now done _after_ your preprocessors succeeded. This is transparent, which means that you can still use the same functions (Store, StoreReferenceData) to store files to the Grid. However, the Shuttle first stores them locally and transfers them after the preprocessor finished. The return code of these two functions has changed from UInt_t to Bool_t which gives you the success of the storing.
24 In case of an error with the Grid, the Shuttle will retry the storing later, the preprocessor does not need to be run again.
25
26 2) The meaning of the return code of the preprocessor has changed. 0 is now success and any other value means failure. This value is stored in the log and you can use it to keep details about the error condition.
27
28 3) New function StoreReferenceFile to _directly_ store a file (without opening it) to the reference storage.
29
30 4) The memory usage of the preprocessor is monitored. If it exceeds 2 GB it is terminated.
31
32 5) New function AliPreprocessor::ProcessDCS(). If you do not need to have DCS data in all cases, you can skip the processing by implemting this function and returning kFALSE under certain conditions. E.g. if there is a certain run type.
33 If you always need DCS data (like before), you do not need to implement it.
34
35 6) The run type has been added to the monitoring page
36
37 Revision 1.10  2007/02/28 10:41:01  acolla
38 Run type field added in SHUTTLE framework. Run type is read from "run type" logbook and retrieved by
39 AliPreprocessor::GetRunType() function.
40 Added some ldap definition files.
41
42 Revision 1.8  2007/02/13 11:22:25  acolla
43 Shuttle getters and setters of main/local OCDB/Reference storages, temp and log
44 folders moved to AliShuttleInterface
45
46 Revision 1.6  2006/11/06 14:22:47  jgrosseo
47 major update (Alberto)
48 o) reading of run parameters from the logbook
49 o) online offline naming conversion
50 o) standalone DCSclient package
51
52 Revision 1.5  2006/10/02 12:58:52  jgrosseo
53 Small interface change in StoreReferenceData
54
55 Revision 1.4  2006/08/08 14:19:07  jgrosseo
56 Update to shuttle classes (Alberto)
57
58 - Possibility to set the full object's path in the Preprocessor's and
59 Shuttle's  Store functions
60 - Possibility to extend the object's run validity in the same classes
61 ("startValidity" and "validityInfinite" parameters)
62 - Implementation of the StoreReferenceData function to store reference
63 data in a dedicated CDB storage.
64
65 Revision 1.3  2006/07/11 12:44:32  jgrosseo
66 adding parameters for extended validity range of data produced by preprocessor
67
68 Revision 1.2  2006/06/06 14:20:05  jgrosseo
69 o) updated test preprocessor (alberto)
70 o) added comments to example macro
71 o) test shuttle implements new interface
72
73 Revision 1.2  2006/03/07 07:52:34  hristov
74 New version (B.Yordanov)
75
76 Revision 1.3  2005/11/17 17:47:34  byordano
77 TList changed to TObjArray
78
79 Revision 1.2  2005/11/17 14:43:22  byordano
80 import to local CVS
81
82 Revision 1.1.1.1  2005/10/28 07:33:58  hristov
83 Initial import as subdirectory in AliRoot
84
85 Revision 1.1.1.1  2005/09/12 22:11:40  byordano
86 SHUTTLE package
87
88 Revision 1.2  2005/08/29 21:15:47  byordano
89 some docs added
90
91 */
92
93 //
94 // test implementation of the AliShuttleInterface, to be used for local tests of preprocessors
95 //
96 // reads files from the local disk
97 // stores to local CDB
98 // logs to the screen
99 //
100
101 #include "AliTestShuttle.h"
102 #include "AliLog.h"
103
104 #include "AliCDBManager.h"
105 #include "AliCDBStorage.h"
106 #include "AliCDBMetaData.h"
107 #include "AliCDBPath.h"
108 #include "AliCDBId.h"
109 #include "AliPreprocessor.h"
110
111 #include <TMap.h>
112 #include <TList.h>
113 #include <TObjString.h>
114 #include <TSystem.h>
115
116 ClassImp(AliTestShuttle)
117
118 //______________________________________________________________________________________________
119 AliTestShuttle::AliTestShuttle(Int_t run, UInt_t startTime, UInt_t endTime) :
120   fRun(run),
121   fStartTime(startTime),
122   fEndTime(endTime),
123   fInputFiles(0),
124   fRunParameters(0),
125   fRunType(),
126   fPreprocessors(0),
127   fDcsAliasMap(0)
128 {
129   // constructor
130
131   fInputFiles = new TMap;
132   fRunParameters = new TMap;
133   fPreprocessors = new TObjArray;
134
135   fInputFiles->SetOwner(1);
136   fRunParameters->SetOwner(1);
137   fPreprocessors->SetOwner(1);
138 }
139
140 //______________________________________________________________________________________________
141 AliTestShuttle::~AliTestShuttle()
142 {
143   // destructor
144
145   delete fInputFiles;
146   fInputFiles = 0;
147
148   delete fRunParameters;
149   fRunParameters = 0;
150
151   delete fPreprocessors;
152   fPreprocessors = 0;
153
154   delete fDcsAliasMap;
155   fDcsAliasMap = 0;
156 }
157
158 //______________________________________________________________________________________________
159 Bool_t AliTestShuttle::Store(const AliCDBPath& path, TObject* object, AliCDBMetaData* metaData,
160                                 Int_t validityStart, Bool_t validityInfinite)
161 {
162   // Stores the CDB object
163   // This function should be called at the end of the preprocessor cycle
164   //
165   // This implementation just stores it on the local disk, the full AliShuttle
166   // puts it to the Grid FileCatalog
167
168   Int_t startRun = fRun - validityStart;
169   if(startRun < 0) {
170         AliError("First valid run happens to be less than 0! Setting it to 0...");
171         startRun=0;
172   }
173
174   Int_t endRun = -1;
175   if(validityInfinite) {
176         endRun = AliCDBRunRange::Infinity();
177   } else {
178         endRun = fRun;
179   }
180
181   AliCDBId id(path, startRun, endRun);
182
183   return AliCDBManager::Instance()->GetStorage(fgkMainCDB)->Put(object, id, metaData);
184 }
185
186 //______________________________________________________________________________________________
187 Bool_t AliTestShuttle::StoreReferenceData(const AliCDBPath& path, TObject* object, AliCDBMetaData* metaData)
188 {
189   // Stores the object as reference data
190   // This function should be called at the end of the preprocessor cycle
191   //
192   // This implementation just stores it on the local disk, the full AliShuttle
193   // puts it to the Grid FileCatalog
194
195   AliCDBId id(path, fRun, fRun);
196
197   return AliCDBManager::Instance()->GetStorage(fgkMainRefStorage)->Put(object, id, metaData);
198 }
199
200 //______________________________________________________________________________________________
201 Bool_t AliTestShuttle::StoreReferenceFile(const char* detector, const char* localFile, const char* gridFileName)
202 {
203         //
204         // Stores reference file directly (without opening it). 
205         //
206         // This implementation just stores it on the local disk, the full AliShuttle 
207         // puts it to the Grid FileCatalog
208         
209         AliCDBManager* man = AliCDBManager::Instance();
210         AliCDBStorage* sto = man->GetStorage(fgkMainRefStorage);
211         
212         TString localBaseFolder = sto->GetBaseFolder();
213         
214         TString targetDir;
215         targetDir.Form("%s/%s", localBaseFolder.Data(), detector);
216         
217         TString target;
218         target.Form("%s/%d_%s", targetDir.Data(), fRun, gridFileName);
219         
220         Int_t result = gSystem->GetPathInfo(targetDir, 0, (Long64_t*) 0, 0, 0);
221         if (result)
222         {
223                 result = gSystem->mkdir(targetDir, kTRUE);
224                 if (result != 0)
225                 {
226                         Log("SHUTTLE", Form("StoreReferenceFile - Error creating base directory %s", targetDir.Data()));
227                         return kFALSE;
228                 }
229         }
230                 
231         result = gSystem->CopyFile(localFile, target);
232
233         if (result == 0)
234         {
235                 Log("SHUTTLE", Form("StoreReferenceFile - Stored file %s locally to %s", localFile, target.Data()));
236                 return kTRUE;
237         }
238         else
239         {
240                 Log("SHUTTLE", Form("StoreReferenceFile - Storing file %s locally to %s failed with %d", localFile, target.Data(), result));
241                 return kFALSE;
242         }               
243 }
244
245 //______________________________________________________________________________________________
246 const char* AliTestShuttle::GetFile(Int_t system, const char* detector, const char* id, const char* source)
247 {
248   // This function retrieves a file from the given system (kDAQ, kDCS, kHLT) with the given file id
249   // and from the given source in the system.
250   // The function returnes the path to the local file.
251   //
252   // test implementation of GetFile
253   // takes files from the local disks, files are passen in a TMap in the constructor
254
255   TString key;
256   key.Form("%s-%s-%s", fkSystemNames[system], detector, id);
257   TPair* sourceListPair = dynamic_cast<TPair*> (fInputFiles->FindObject(key.Data()));
258   TMap* sourceList = 0;
259   if (sourceListPair)
260     sourceList = dynamic_cast<TMap*> (sourceListPair->Value());
261   if (!sourceList)
262   {
263     AliError(Form("Could not find any file in %s with id %s (%s)", fkSystemNames[system], id, key.Data()));
264     return 0;
265   }
266
267   TObjString* fileName = 0;
268   TPair* fileNamePair = dynamic_cast<TPair*> (sourceList->FindObject(source));
269   if (fileNamePair)
270         fileName = dynamic_cast<TObjString*> (fileNamePair->Value());
271   if (!fileName)
272   {
273     AliError(Form("Could not find files from source %s in %s with id %s",
274                         source, fkSystemNames[system], id));
275     return 0;
276   }
277
278   return fileName->GetString().Data();
279 }
280
281 //______________________________________________________________________________________________
282 TList* AliTestShuttle::GetFileSources(Int_t system, const char* detector, const char* id)
283 {
284   // Returns a list of sources in a given system that saved a file with the given id
285   //
286   // test implementation of GetFileSources
287   // takes files from the local disks, files are passen in a TMap in the constructor
288
289   TString key;
290   if (id)
291     key.Form("%s-%s-%s", fkSystemNames[system], detector, id);
292   else
293     key.Form("%s-%s", fkSystemNames[system], detector);
294   
295   TList* list = new TList;
296   
297   TIterator* iter = fInputFiles->MakeIterator();
298   TObject* obj = 0;
299   while ((obj = iter->Next()))
300   {
301         TObjString* objStr = dynamic_cast<TObjString*> (obj);
302         if (objStr)
303         {
304                 Bool_t found = kFALSE;
305                 if (id)
306                 {
307                         found = (objStr->String().CompareTo(key) == 0);
308                 }
309                 else
310                         found = objStr->String().BeginsWith(key);
311                 
312                 if (found)
313                 {
314                         TPair* sourceListPair = dynamic_cast<TPair*> (fInputFiles->FindObject(objStr->String().Data()));
315                         TMap* sourceList = dynamic_cast<TMap*> (sourceListPair->Value());
316         
317                         TIterator* iter2 = sourceList->GetTable()->MakeIterator();
318                         TObject* obj2 = 0;
319                         while ((obj2 = iter2->Next()))
320                         {
321                                 TPair* pair = dynamic_cast<TPair*> (obj2);
322                                 if (pair)
323                                 {
324                                         if (!list->FindObject(pair->Key()))
325                                                 list->Add(new TObjString(pair->Key()->GetName()));
326                                 }
327                         }
328                         
329                         delete iter2;
330                 }
331         }
332   }
333   
334   if (list->GetEntries() == 0)
335     AliInfo(Form("Could not find any file in %s with id %s (%s)", fkSystemNames[system], id, key.Data()));
336   
337   return list;
338 }
339
340 //______________________________________________________________________________________________
341 TList* AliTestShuttle::GetFileIDs(Int_t system, const char* detector, const char* source)
342 {
343   // Returns a list of ids in a given system that saved a file with the given source
344   //
345   // test implementation of GetFileSources
346   // takes files from the local disks, files are passen in a TMap in the constructor
347
348
349   TString key;
350   key.Form("%s-%s", fkSystemNames[system], detector);
351   
352   TList* list = new TList;
353   
354   TIterator* iter = fInputFiles->MakeIterator();
355   TObject* obj = 0;
356   while ((obj = iter->Next()))
357   {
358         TObjString* objStr = dynamic_cast<TObjString*> (obj);
359         if (objStr)
360         {
361                 if (objStr->String().BeginsWith(key))
362                 {
363                         Bool_t found = kFALSE;
364                 
365                         TPair* sourceListPair = dynamic_cast<TPair*> (fInputFiles->FindObject(objStr->String().Data()));
366                         TMap* sourceList = dynamic_cast<TMap*> (sourceListPair->Value());
367         
368                         TIterator* iter2 = sourceList->GetTable()->MakeIterator();
369                         TObject* obj2 = 0;
370                         while ((obj2 = iter2->Next()))
371                         {
372                                 TPair* pair = dynamic_cast<TPair*> (obj2);
373                                 if (pair)
374                                 {
375                                         if (strcmp(pair->Key()->GetName(), source) == 0)
376                                                 found = kTRUE;
377                                 }
378                         }
379                         
380                         delete iter2;
381                         
382                         if (found)
383                         {
384                                 TObjArray* tokens = objStr->String().Tokenize("-");
385                                 if (tokens->GetEntries() == 3)
386                                 {
387                                         TObjString* id = dynamic_cast<TObjString*> (tokens->At(2));
388                                         if (id && !list->FindObject(id->String()))
389                                                 list->Add(new TObjString(id->String()));
390                                 }
391                                 
392                                 delete tokens;
393         
394                         }
395                 }
396         }
397   }
398   
399   if (list->GetEntries() == 0)
400     AliInfo(Form("Could not find any file in %s with source %s (%s)", fkSystemNames[system], source, key.Data()));
401   
402   return list;
403 }
404
405 //______________________________________________________________________________________________
406 void AliTestShuttle::Log(const char* detector, const char* message)
407 {
408   // test implementation of Log
409   // just prints to the screen
410
411   AliInfo(Form("%s: %s", detector, message));
412 }
413
414 //______________________________________________________________________________________________
415 void AliTestShuttle::AddInputFile(Int_t system, const char* detector, const char* id, const char* source, const char* fileName)
416 {
417   //
418   // This function adds a file to the list of input files
419   // the list is stored in fInputFiles 
420   // fInputFiles: TMap (key -> value)
421   //    <system>-<detector>-<id> -> TMap (key -> value)
422   //                                <source> -> <filename>
423   //  
424   
425   TString key;
426   key.Form("%s-%s-%s", fkSystemNames[system], detector, id);
427   TPair* sourceListPair = dynamic_cast<TPair*> (fInputFiles->FindObject(key.Data()));
428   TMap* sourceList = 0;
429   if (sourceListPair)
430     sourceList = dynamic_cast<TMap*> (sourceListPair->Value());
431   if (!sourceList)
432   {
433     sourceList = new TMap;
434     fInputFiles->Add(new TObjString(key), sourceList);
435   }
436
437   sourceList->Add(new TObjString(source), new TObjString(fileName));
438 }
439
440 //______________________________________________________________________________________________
441 Bool_t AliTestShuttle::AddInputCDBEntry(AliCDBEntry* entry)
442 {
443   // This function adds an object in the OCDB to be later retrieved with GetFromOCDB
444
445         AliCDBStorage *sto = AliCDBManager::Instance()->GetStorage(fgkMainCDB);
446         if (!sto)
447         {
448                 Log("SHUTTLE", "GetFromOCDB - Cannot activate main OCDB for query!");
449                 return 0;
450         }
451
452         return sto->Put(entry);
453 }
454
455 //______________________________________________________________________________________________
456 AliCDBEntry* AliTestShuttle::GetFromOCDB(const char* detector, const AliCDBPath& path)
457 {
458 // returns obiect from OCDB valid for current run
459
460         AliCDBStorage *sto = AliCDBManager::Instance()->GetStorage(fgkMainCDB);
461         if (!sto)
462         {
463                 Log("SHUTTLE", "GetFromOCDB - Cannot activate main OCDB for query!");
464                 return 0;
465         }
466
467         return (AliCDBEntry*) sto->Get(path, fRun);
468 }
469
470 //______________________________________________________________________________________________
471 void AliTestShuttle::Process()
472 {
473   // This function tests all preprocessors that are registered to it
474   // All preprocessors get the same dcs alias map and have access to the same list of files.
475
476   for (Int_t i=0; i<fPreprocessors->GetEntries(); ++i)
477   {
478     AliPreprocessor* preprocessor = dynamic_cast<AliPreprocessor*> (fPreprocessors->At(i));
479     if (preprocessor)
480     {
481       preprocessor->Initialize(fRun, fStartTime, fEndTime);
482       preprocessor->Process(fDcsAliasMap);
483     }
484   }
485 }
486
487 //______________________________________________________________________________________________
488 void AliTestShuttle::RegisterPreprocessor(AliPreprocessor* preprocessor)
489 {
490   // registers a preprocessor
491
492         const char* detName = preprocessor->GetName();
493         if(strcmp("DET", detName) != 0) {
494                 if(GetDetPos(detName) < 0)
495                         AliFatal(Form("********** !!!!! Invalid detector name: %s !!!!! **********", detName));
496         }
497
498         fPreprocessors->Add(preprocessor);
499 }
500
501 //______________________________________________________________________________________________
502 void AliTestShuttle::AddInputRunParameter(const char* key, const char* value){
503 // set a run parameter (in reality it will be read from the DAQ logbook)
504
505         TObjString* keyObj = new TObjString(key);
506         if (fRunParameters->Contains(key)) {
507                 AliWarning(Form("Parameter %s already existing and it will be replaced.", key));
508                 delete fRunParameters->Remove(keyObj);
509
510         }
511         fRunParameters->Add(keyObj, new TObjString(value));
512         AliDebug(2, Form("Number of parameters: %d", fRunParameters->
513         GetEntries()));
514 }
515
516 //______________________________________________________________________________________________
517 const char* AliTestShuttle::GetRunType()
518 {
519         //
520         // get a run parameter
521         //
522
523         return fRunType;
524 }
525
526 //______________________________________________________________________________________________
527 const char* AliTestShuttle::GetRunParameter(const char* key){
528 // get a run parameter
529
530         TObjString* value = dynamic_cast<TObjString*> (fRunParameters->GetValue(key));
531         if(!value) {
532                 AliError(Form("No such parameter: %s", key));
533                 return 0;
534         }
535         return value->GetName();
536 }
537
538 //______________________________________________________________________________________________
539 void AliTestShuttle::SetShuttleTempDir(const char* tmpDir)
540 {
541 // sets Shuttle temp directory
542
543         fgkShuttleTempDir = gSystem->ExpandPathName(tmpDir);
544 }
545
546 //______________________________________________________________________________________________
547 void AliTestShuttle::SetShuttleLogDir(const char* logDir)
548 {
549 // sets Shuttle log directory
550
551         fgkShuttleLogDir = gSystem->ExpandPathName(logDir);
552 }
553