]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/CalibMacros/AliOCDBToolkit.cxx
Enable sourcing of the AliOCDBtoolkit.sh.
[u/mrichter/AliRoot.git] / PWGPP / CalibMacros / AliOCDBToolkit.cxx
1 /*
2
3   Primary goal of the proposal was to provide functionality to browse and compare the content of the OCDB
4   specified by different means.
5
6   a.) galice.root               - as currently implemented by Ruben in MC (or any file with cdbMap, and cdbList)
7   b.) AliESDs.root              - for the reconstructed data
8   c.) ocdb snapshot             - as used for grid productions
9   d.) TMap(s)                   - as used internally in galice.root and AliESDs,root  
10   e.) log file (if possible)    - looks logs aways used similar syntax, tested and working
11   f.) C macro                   - custom macro
12
13   Content comparison should be done:
14   a.) on the level of symbolic links 
15   b.) on the level of content itself 
16   - by by byte comparison dif
17   - data member by data member comparison
18
19   Implementation assumption:
20   All input formats (a .. f) will  be converted to the TMap storages and TList if AliCDBIds 
21
22
23   Example usage:
24   export mcProd=$ALICE_ROOT/PWGPP/CalibMacros/
25   .L  $mcProd/AliOCDBToolkit.cxx+
26   MakeDiffExampleUseCase();
27
28
29 */
30
31 // STD
32 #include <iostream>
33 #include <algorithm>
34 #include <sstream>
35 #include <stdexcept>
36 #include <functional>
37 //
38 #include "TSystem.h"
39 #include "TObjArray.h"
40 #include "TString.h"
41 #include "TTree.h"
42 #include "TMessage.h"
43 //
44 #include "AliCDBManager.h"
45 #include "AliCDBEntry.h"
46
47 //
48 //
49 //
50
51
52 void MakeDiffExampleUseCase();    // example usage
53 //
54 //
55 Bool_t   ParseInfoFromOcdbString(TString ocdbString, TString &ocdbPath, Int_t &run0, Int_t &run1, Int_t &version, Int_t &subVersion);   
56 Bool_t   ParseInfoFromOcdbString(TString ocdbString, AliCDBId &cdbId);
57 TList  * ConvertListStringToCDBId(const TList *cdbList0); 
58
59 void LoadOCDBFromLog(const char *logName, Int_t verbose);
60 void LoadOCDBFromMap(const TMap *cdbMap, const TList *cdbList);
61 void MakeDiff(const TMap *cdbMap0, const TList *cdbList0, const TMap *cdbMap1, const TList *cdbList1, Int_t verbose);
62 void DumpOCDB(const TMap *cdbMap0, const TList *cdbList0);
63
64
65
66 void MakeDiffExampleUseCase(){
67   //
68   // Example usage for the MC 
69   // To run example case, assuming presence of following files in working directory: 
70   //    - rec.log        
71   //    - galice.root   
72   //    - AliESDs.root
73   //
74   AliCDBManager * man = AliCDBManager::Instance();
75   LoadOCDBFromLog("rec.log",0);
76   const TMap *cdbMapLog= man->GetStorageMap();        // this is map of 
77   const TList *cdbListLog=man->GetRetrievedIds();     // this is list of AliCDBId
78   //  TList *cdbListLog0=man->GetRetrievedIds();     // this is list of AliCDBId
79   //
80   TFile *fmc = TFile::Open("galice.root");
81   TMap *cdbMapMC= (TMap*)fmc->Get("cdbMap");          // 
82   TList *cdbListMC0= (TList*)fmc->Get("cdbList");     // this is list of TObjStrings
83   TList *cdbListMC = ConvertListStringToCDBId(cdbListMC0);        // convert to the TObjArray of AliCDBids
84   //
85   TFile *fesd = TFile::Open("AliESDs.root");
86   TList *listESD = ((TTree*)fesd->Get("esdTree"))->GetUserInfo();
87   TMap *cdbMapESD= (TMap*)listESD->FindObject("cdbMap");  
88   TList *cdbListESD0= (TList*)listESD->FindObject("cdbList"); // this is list of TObjStrings
89   TList *cdbListESD = ConvertListStringToCDBId(cdbListESD0);              // convert to the TObjArray  of AliCDBids
90   //
91   //
92   //
93   printf("\n\n");
94   printf("Diff log>>>ESD\n\n:");
95   MakeDiff(cdbMapLog, cdbListLog, cdbMapESD, cdbListESD,0);
96   printf("\n\n");
97   printf("Diff ESD>>>log\n\n:");
98   MakeDiff(cdbMapESD, cdbListESD,cdbMapLog, cdbListLog,0);
99   // 
100   printf("\n\n");
101   printf("Diff ESD>>>MC\n\n:");
102   MakeDiff(cdbMapMC, cdbListMC, cdbMapESD, cdbListESD,0);
103 }
104
105
106 void DumpOCDBAsTxt(const TString fInput,const TString fType){
107     TFile *file;
108     const TMap *cdbMap;
109     const TList *cdbList;
110
111
112     if(fType.EqualTo("MC",TString::kIgnoreCase)){
113         file = TFile::Open(fInput.Data());
114         cdbMap = (TMap*)file->Get("cdbMap");          // 
115         TList *cdbListMC0 = (TList*)file->Get("cdbList");     // this is list of TObjStrings
116         cdbList = ConvertListStringToCDBId(cdbListMC0);        // convert to the TObjArray of AliCDBids
117     } 
118     else if(fType.EqualTo("ESD",TString::kIgnoreCase)){
119         file = TFile::Open(fInput.Data());
120         TList *listESD = ((TTree*)file->Get("esdTree"))->GetUserInfo();
121         cdbMap = (TMap*)listESD->FindObject("cdbMap");  
122         TList *cdbListESD0= (TList*)listESD->FindObject("cdbList"); // this is list of TObjStrings
123         cdbList = ConvertListStringToCDBId(cdbListESD0);              // convert to the TObjArray  of AliCDBids
124     }
125     else if(fType.EqualTo("log",TString::kIgnoreCase)){
126         AliCDBManager * man = AliCDBManager::Instance();
127         LoadOCDBFromLog(fInput.Data(),0);
128         cdbMap = man->GetStorageMap();        // this is map of 
129         cdbList =man->GetRetrievedIds();     // this is list of AliCDBId
130     }
131     else{
132         printf("unsupported option: %s",fType.Data());
133         return;
134     }
135     cout <<"BEGINDUMP:" << endl;
136     DumpOCDB(cdbMap,cdbList);
137 }
138
139
140 Bool_t ParseInfoFromOcdbString(TString ocdbString, TString &ocdbPath, Int_t &run0, Int_t &run1, Int_t &version, Int_t &subVersion){
141   //
142   // Parse OCDB id string and provide basic ocdb information
143   //
144   //  a.) parse ocdbPath
145   Int_t indexBeginPath= ocdbString.Index("path: ")+7;
146   if (indexBeginPath<0) return kFALSE;
147   Int_t indexEndPath=ocdbString.Index(";",indexBeginPath);
148   if (indexEndPath<0) return kFALSE;
149   ocdbPath=TString(&(ocdbString.Data()[indexBeginPath]), indexEndPath-indexBeginPath-1);
150   // b.) parse runRange
151   Int_t indexRun0= ocdbString.Index(": [",indexEndPath)+3;
152   if (indexRun0<0) return kFALSE;
153   Int_t indexRun1= ocdbString.Index(",",indexRun0)+1;
154   if (indexRun1<0) return kFALSE;
155   run0=atoi(&(ocdbString.Data()[indexRun0]));
156   run1=atoi(&(ocdbString.Data()[indexRun1]));
157   AliCDBRunRange runRange(run0,run1);
158   //c.) parse version, subversion
159   Int_t indexVersion= ocdbString.Index("version: v",indexRun1)+10;
160   if (indexVersion<0) return kFALSE;
161   Int_t indexSubVersion= ocdbString.Index("_s",indexVersion)+2;
162   if (indexSubVersion<0) return kFALSE;
163   version=atoi(&(ocdbString.Data()[indexVersion]));
164   subVersion=atoi(&(ocdbString.Data()[indexSubVersion]));
165   return kTRUE;
166 }
167
168 Bool_t ParseInfoFromOcdbString(TString ocdbString, AliCDBId &cdbId){
169   //
170   // Parse OCDB id string and provide basic ocdb information and fillcdbID object
171   //
172   TString ocdbPath;
173   Int_t run0=0, run1=0;
174   Int_t version=0, subVersion=0;
175   Bool_t parseStatus = ParseInfoFromOcdbString(ocdbString, ocdbPath, run0,run1,version,subVersion); 
176   if (parseStatus) {
177     AliCDBRunRange runRange(run0,run1);
178     cdbId=AliCDBId(ocdbPath.Data(),runRange,version,subVersion);
179     AliCDBId* id = AliCDBId::MakeFromString(ocdbString);
180     cdbId=*id;
181     delete id;
182   }
183   //
184   return parseStatus;
185 }
186
187 TList  * ConvertListStringToCDBId(const TList *cdbList0){
188   //
189   // Convert input  list of the TObjString to list to AliCDBid 
190   //
191   Int_t entriesList0=cdbList0->GetEntries();
192   TList * array0 = new TList();
193   AliCDBId tmp0;
194   for (Int_t ientry0=0; ientry0<entriesList0; ientry0++){
195     if (cdbList0->At(ientry0)==0) continue;
196     Bool_t isId =  cdbList0->At(ientry0)->IsA()->InheritsFrom("AliCDBId");
197     if (isId){
198       array0->AddLast(cdbList0->At(ientry0));
199     }else{
200       Bool_t isString =  cdbList0->At(ientry0)->IsA()->InheritsFrom("TObjString");
201       if (isString){
202         TObjString* sid0 = dynamic_cast<TObjString*> (cdbList0->At(ientry0));
203         Bool_t status =  ParseInfoFromOcdbString(sid0->String(), tmp0);
204         if (!status) continue;
205         array0->AddLast(new AliCDBId(tmp0));
206       }
207     }
208   }
209   return array0;  
210 }
211
212
213
214 void LoadOCDBFromLog(const char *logName, Int_t verbose){
215   //
216   // Initilaize OCDB
217   // Load OCDB setting as specified in log
218   // Assuming fixed version of the log 
219   // AliCDBManager is initilaized - ocdbMap and ID list can be exported
220   //
221
222   // Parsing/loading sequence:
223   //    0.) SetDefault storage  *** Default Storage URI:
224   //    1.) SetSpecific storage  *** Specific storage
225   //    2.) SetRunNumber  Run number:
226   //    3.) Set used IDs
227   //
228   AliCDBManager * man = AliCDBManager::Instance();
229   //
230   // 0.) SetDefault storage  *** Default Storage URI:
231   // 
232   TString  defaultOCDB = gSystem->GetFromPipe(TString::Format("cat %s| grep \"Storage URI:\"",logName).Data());
233   TObjArray *array = defaultOCDB.Tokenize("\"");
234   man->SetDefaultStorage(array->Last()->GetName());
235   delete array;
236   //
237   // 1.) SetSpecific storage  *** Specific storage
238   //
239   TString  specificStorage  = gSystem->GetFromPipe(TString::Format("cat %s| grep \"Specific storage\"",logName).Data());
240   array = specificStorage.Tokenize("\"");
241   Int_t entries = array->GetEntries();
242   for (Int_t i=1; i<entries-2; i+=4){    
243     // add protection here line shuld be in expected format
244     if (verbose&2>0) printf("%s\t%s\n",array->At(i)->GetName(),array->At(i+2)->GetName());    
245     man->SetSpecificStorage(array->At(i)->GetName(),array->At(i+2)->GetName());
246   }
247   delete array;
248   //
249   // 2.) SetRunNumber  Run number:
250   //
251   TString  runLine  = gSystem->GetFromPipe(TString::Format("cat %s| grep \"I-AliCDBManager::Print: Run number =\"",logName).Data());
252   array = runLine.Tokenize("=");
253   Int_t run = 0;
254   if (array->GetEntries()>1) run=atoi(array->At(1)->GetName()); 
255   delete array;
256   man->SetRun(run);  
257   //
258   // 3.) Set used IDs
259   //   
260   TString  ids =   gSystem->GetFromPipe(TString::Format("cat %s| grep I-AliCDB | grep path| grep range | grep version", logName).Data());
261   array= ids.Tokenize("\n");
262   entries = array->GetEntries();
263   //
264   for (Int_t i=0; i<entries; i++){
265     //
266     TString ocdbString = array->At(i)->GetName();
267     TString ocdbEntry;
268     TString ocdbPath;
269     Int_t run0=0, run1=0;
270     Int_t version=0, subVersion=0;
271     Bool_t parseStatus = ParseInfoFromOcdbString(ocdbString, ocdbPath, run0,run1,version,subVersion); 
272     if (!parseStatus) continue;
273     AliCDBRunRange runRange(run0,run1);
274     //
275     if ((verbose&2)!=0) {
276       printf("%s/Run%d_%d_v%d_s%d.root\n",ocdbPath.Data(),run0,run1,version,subVersion); 
277     }
278     try {
279       AliCDBEntry*entry= man->Get(ocdbPath.Data(),runRange,version,subVersion);
280     } catch(const exception &e){
281       cerr << "OCDB retrieval failed!" << endl;
282       cerr << "Detailes: " << e.what() << endl;
283     }
284   }  
285   if ((verbose&1)!=0){
286     man->Print();
287     man->GetStorageMap()->Print();
288     man->GetRetrievedIds()->Print(); 
289   }
290 }
291
292
293 void LoadOCDBFromMap(TMap */*cdbMap*/, TList */*cdbList*/){
294   //
295   // Initilaize OCDB
296   // Load OCDB setting as specified in maps
297   // Or Do we have already implementation in AliCDBanager?
298 }
299
300
301
302 void MakeDiff(const TMap *cdbMap0, const TList *cdbList0, const TMap *cdbMap1, const TList *cdbList1, Int_t verbose){
303   //
304   //
305   // Print difference between the 2 ocdb maps
306   // Input:
307   //   maps and list charactireizing OCDB setup  
308   // Output:
309   //   To be decided, currently it is the teinal output
310   //
311   Int_t entriesList0=cdbList0->GetEntries();
312   Int_t entriesList1=cdbList1->GetEntries();
313   //
314   for (Int_t ientry0=0; ientry0<entriesList0; ientry0++){
315     AliCDBId *id0    = dynamic_cast<AliCDBId*> (cdbList0->At(ientry0));
316     AliCDBId *id1=0;
317     for (Int_t ientry1=0; ientry1<entriesList1; ientry1++){
318       AliCDBId *cid1    = dynamic_cast<AliCDBId*> (cdbList1->At(ientry1));
319       //id0.Print();
320       //cid1.Print();
321       if (cid1->GetPath().Contains(id0->GetPath().Data())==0) continue;
322       id1=cid1;
323     }
324     if (!id1) {
325       printf("Missing entry\t");
326       id0->Print();
327       continue;
328     }
329     Bool_t isOK=kTRUE;
330     if (id0->GetFirstRun()!= id1->GetFirstRun() ||id0->GetLastRun()!= id1->GetLastRun()){
331       printf("Differrent run range\n");
332       id0->Print();
333       id1->Print();
334     }    
335     if (id0->GetVersion()!= id1->GetVersion() ||id0->GetSubVersion()!= id1->GetSubVersion()){
336       printf("Differrent version\n");
337       id0->Print();
338       id1->Print();
339     }    
340   }
341 }
342
343 void DumpOCDB(const TMap *cdbMap0, const TList *cdbList0){
344   //
345   // Dump the OCDb configuatation as formated text file 
346   // with following collumns
347   // cdb name  prefix cdb path
348   // OCDB entries are sorted alphabetically
349   // e.g:
350     // TPC/Calib/RecoParam /hera/alice/jwagner/software/aliroot/AliRoot_TPCdev/OCDB/ TPC/Calib/RecoParam/Run0_999999999_v0_s0.root $SIZE_AliCDBEntry_Object $HASH_AliCDBEntry_Object
351
352     AliCDBManager * man = AliCDBManager::Instance();
353     TIter next(cdbList0);
354     AliCDBId *CDBId;
355     TString cdbName;
356     TString cdbPath;
357     TObjString *ostr;
358     AliCDBEntry *cdbEntry;
359     UInt_t hash;
360     TMessage * file;
361     Int_t size; 
362
363     while ((CDBId  =(AliCDBId*) next())){
364         cdbName = CDBId->GetPath();
365         ostr = (TObjString*)cdbMap0->GetValue(cdbName.Data());
366         if(!ostr) ostr = (TObjString*)cdbMap0->GetValue("default");
367         cdbPath = ostr->GetString();
368         if(cdbPath.Contains("local://"))cdbPath=cdbPath(8,cdbPath.Length()).Data();
369
370         cdbEntry = (AliCDBEntry*) man->Get(*CDBId);
371         TObject *obj = cdbEntry->GetObject();
372         file = new TMessage(TBuffer::kWrite);
373         file->WriteObject(obj);
374         size = file->Length();
375         if(!obj){
376             printf("object %s empty!\n",cdbName.Data());
377             continue;
378         }
379         hash = TString::Hash(file->Buffer(),size);
380         printf("%s\t%s\t%s/Run%d_%d_v%d_s%d.root\t%d\t%u\n",
381                 cdbName.Data(),
382                 cdbPath.Data(),
383                 cdbName.Data(),
384                 CDBId->GetFirstRun(),
385                 CDBId->GetLastRun(),
386                 CDBId->GetVersion(),
387                 CDBId->GetSubVersion(),
388                 size,
389                 hash
390               );
391         //if(!(CDBId->GetPathLevel(0)).Contains("TPC")) continue;
392         //cout << CDBId.ToString() << endl;
393
394         delete file;
395     }
396 }