]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/CalibMacros/MergeCalibration/mergeCalibObjects.C
Move the calibration macros to PWG1 (Marian)
[u/mrichter/AliRoot.git] / PWG1 / CalibMacros / MergeCalibration / mergeCalibObjects.C
1 /*
2   Merge calibration entry macro:
3   
4   Example usage:
5   
6   .L $ALICE_ROOT/ANALYSIS/CalibMacros/mergeCalibObjects.C
7   mergeCalibObjects()
8   
9 */
10
11 #if !defined(__CINT__) || defined(__MAKECINT__)
12
13 #include <fstream>
14 #include "TSystem.h"
15 #include "TFile.h"
16 #include "TObjArray.h"
17 #include "AliSysInfo.h"
18 #include "AliTPCcalibBase.h"
19 #include "TH1F.h"
20 #include "TMethodCall.h"
21 #include "TGrid.h"
22 #include "TGridResult.h"
23
24 #endif
25
26 void IterTXT( const char * fileList="calib.list",Bool_t separate=kFALSE);
27 void IterAlien(const char* outputDir, const char* outputFileName="AliESDfriends_v1.root");
28 void Merge(TFile* fileIn, TObjArray * array);
29 void StoreResults(TObjArray * array);
30 void StoreSeparateResults(TObjArray * array);
31
32 void mergeCalibObjects( const char * fileList="calib.list",Bool_t separate=kFALSE){
33
34   // main function
35
36   IterTXT(fileList,separate );
37 }
38
39 void LoadLib(){
40
41   // Loading the necessary libraries
42
43   gSystem->Load("libANALYSIS");
44   gSystem->Load("libTPCcalib"); 
45   TH1::AddDirectory(0);
46 }
47
48
49 void IterXML(){ 
50
51   // iterating over the files coming from an XML collection 
52   // to be implemented
53
54   LoadLib();
55 }
56
57
58 void IterAlien(const char* outputDir, const char* outputFileName){
59
60         //
61         // Merge the files coming out of the calibration job
62         // 
63
64         LoadLib();
65         TObjArray * mergeArray= new TObjArray;
66
67         TString outputFile(outputFileName);
68         TString command;
69         // looking for files to be merged in the output directory
70         command = Form("find %s/ *%s", outputDir, outputFile.Data());
71         printf("command: %s\n", command.Data());
72         TGrid::Connect("alien://");
73         TGridResult *res = gGrid->Command(command);
74         if (!res) return;
75         TIter nextmap(res);
76         TMap *map = 0;
77         // loop over the results
78         while((map=(TMap*)nextmap())) {
79                 // getting the turl
80                 TObjString *objs = dynamic_cast<TObjString*>(map->GetValue("turl"));
81                 if (!objs || !objs->GetString().Length()) {
82                         // Nothing found - skip this output
83                         delete res;
84                         break;
85                 } 
86                 printf("looking for file %s\n",(objs->GetString()).Data());
87                 TFile* currentFile=TFile::Open((objs->GetString()).Data());
88                 Merge(currentFile, mergeArray);
89         }
90         Bool_t separate = kFALSE;
91         if (separate) 
92                 StoreSeparateResults(mergeArray);
93         else
94                 StoreResults(mergeArray);
95         delete mergeArray;
96 }
97                 
98
99
100 void IterTXT( const char * fileList, Bool_t separate){
101
102   // Merge the files indicated in the list - fileList
103   // ASCII file opition example: 
104   // find `pwd`/ | grep AliESDfriends_v1.root > calib.list
105
106   LoadLib();
107   TObjArray * mergeArray= new TObjArray;
108   
109   // Open the input stream
110   
111   ifstream in;
112   in.open(fileList);
113   // Read the input list of files 
114   TString objfile;
115   Int_t counter=0;
116   while(in.good()) {
117     in >> objfile;
118     if (!objfile.Contains("root")) continue; // protection    
119     printf("Open file:Counter\t%d\tMerging file %s\n",counter++,objfile.Data());
120     TFile currentFile(objfile.Data());
121     Merge(&currentFile, mergeArray);
122   }
123   if (separate) 
124     StoreSeparateResults(mergeArray);
125   else
126     StoreResults(mergeArray);
127   delete mergeArray;
128 }
129
130 void StoreResults(TObjArray * array){
131
132   // Storing the results in one single file
133
134   TFile *f = new TFile("CalibObjects.root","recreate");
135   for (Int_t i=0; i<array->GetEntries(); i++){
136     TObject *object0 = array->At(i);
137     if (!object0) continue;
138     object0->Write();
139   }
140   f->Close();
141   delete f;
142 }
143
144
145 void StoreSeparateResults(TObjArray * array){
146
147   // Store the results in separate files (one per object)
148
149   for (Int_t i=0; i<array->GetEntries(); i++){
150     TObject *object0 = array->At(i);
151     if (!object0) continue;
152     TFile *f = new TFile(Form("CalibObjects_%s.root",object0->GetName()),"recreate");
153     object0->Write();
154     f->Close();
155     delete f;
156   }
157 }
158
159
160
161 void Merge(TFile* fileIn, TObjArray * array){
162   
163   // Merging procedure
164   
165   TObjArray *carray = new TObjArray;   //array of the objects inside current file
166   carray->SetOwner(kTRUE);
167
168   // load all objects to  memory
169   
170   TList *farr = fileIn->GetListOfKeys();
171   if (!farr) return;
172   for (Int_t ical=0; ical<farr->GetEntries(); ical++){
173     if (!farr->At(ical)) continue;
174     TObject *obj = fileIn->Get(farr->At(ical)->GetName());
175     if (obj) carray->AddLast(obj);
176     AliSysInfo::AddStamp(farr->At(ical)->GetName(),1,ical);  
177   }
178
179   if (carray->GetEntries()==0) return;
180   TMethodCall callEnv;
181
182   for (Int_t i=0; i<carray->GetEntries(); i++){
183
184     TObjArray *templist = new TObjArray(1);
185     templist->SetOwner(kFALSE);
186     TObject *currentObject = carray->At(i);
187     if (!currentObject) continue;
188     printf("%s\n",currentObject->GetName());
189     callEnv.InitWithPrototype(currentObject->IsA(), "Merge", "TCollection*");
190     if (!callEnv.IsValid()) {continue;}
191     TString oname=currentObject->GetName();
192     if (oname.Contains("esdFriendTree")) continue;
193     TObject *mergedObject = array->FindObject(currentObject->GetName());
194     if (!mergedObject) {
195       array->AddLast(currentObject);
196       carray->RemoveAt(i);
197       continue;
198     }
199     templist->AddLast(currentObject);
200     callEnv.SetParam((Long_t) templist);
201     callEnv.Execute(mergedObject);
202     delete templist;
203   }
204   delete carray;
205 }