]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muondep/QAMergeTemplates/QAMerge.C
General updates, including new class to compute the normalization factors using the...
[u/mrichter/AliRoot.git] / PWG / muondep / QAMergeTemplates / QAMerge.C
1 #include "TSystem.h"
2 #include "TList.h"
3 #include "TObjString.h"
4 #include "Riostream.h"
5 #include "TRegexp.h"
6 #include "AliMUONVTrackerData.h"
7 #include "TFile.h"
8 #include "TKey.h"
9 #include "TGrid.h"
10 #include "TGridResult.h"
11 #include "TUrl.h"
12 #include "TMath.h"
13 #include "TFile.h"
14 #include "TStopwatch.h"
15 #include "TGridCollection.h"
16 #include "TROOT.h"
17 #include "TError.h"
18 #include "TH1.h"
19 #include "TMethodCall.h"
20
21 void CopyFromRemote(Int_t runNumber, const char* period, const char* pass);
22 void CopyFromRemote(const TList& files);
23 TList* GetFileList(Int_t runNumber, const char* period, const char* pass);
24 void QAMerge(Int_t runNumber, const char* period, const char* pass);
25 void QAMerge(const TList& files, const char* outputfile);
26
27 ////______________________________________________________________________________
28 //AliMUONVTrackerData* GetTrackerData(TDirectory* dir, const char* trackerDataName)
29 //{
30 //  AliMUONVTrackerData* trackerData(0x0);
31 //
32 //  TIter next(dir->GetListOfKeys());
33 //  TKey* key;
34 //  
35 //  while ( ( key = static_cast<TKey*>(next()) ) && !trackerData )
36 //  {
37 //    if ( key->IsFolder() )
38 //    {
39 //      trackerData = GetTrackerData(static_cast<TDirectory*>(key->ReadObj()),trackerDataName);
40 //    }
41 //    else
42 //    {
43 //      TString name(key->GetName());
44 //      
45 //      if ( name.Contains(trackerDataName) )
46 //      {
47 //        trackerData = dynamic_cast<AliMUONVTrackerData*>(key->ReadObj());
48 //      }
49 //    }
50 //  }
51 //  
52 //  return trackerData;
53 //  
54 //}
55 //
56 ////______________________________________________________________________________
57 //AliMUONVTrackerData* GetTrackerData(const char* filename, const char* trackerDataName)
58 //{
59 //  TFile* file = TFile::Open(filename);
60 //  
61 //  cout << filename << endl;
62 //  
63 //  if (!file) return 0x0;
64 //
65 //  AliMUONVTrackerData* trackerData = GetTrackerData(file,trackerDataName);  
66 //
67 //  delete file;
68 //  
69 //  return trackerData;
70 //  
71 //}
72
73 ////______________________________________________________________________________
74 //AliMUONVTrackerData* MergeTrackerData(const TList& filelist, const char* trackerDataName)
75 //{
76 //  TIter next(&filelist);
77 //  TObjString* s;
78 //  
79 //  s = static_cast<TObjString*>(next());
80 //  
81 //  AliMUONVTrackerData* d = GetTrackerData(s->String().Data(),trackerDataName);
82 //  
83 //  if (!d)
84 //  {
85 //    cout << Form("ERROR : no tracker data found in %s",s->String().Data()) << endl;
86 //    return 0x0;
87 //  }
88 //  
89 //  AliMUONVTrackerData* merged = static_cast<AliMUONVTrackerData*>(d->Clone(Form("%s-MERGED",trackerDataName)));
90 //  
91 //  while ( ( s = static_cast<TObjString*>(next()) ) )
92 //  {
93 //    d = GetTrackerData(s->String().Data(),trackerDataName);
94 //    if (d)
95 //    {
96 //      TList list;
97 //      list.Add(d);
98 //      cout << "Merging " << s->String().Data() << " " << d->GetName() << " " << d->NumberOfEvents(-1) << endl;
99 //      
100 //      merged->Merge(&list);
101 //      delete d;
102 //    }
103 //  }
104 //  
105 //  merged->SetName(Form("%s-MERGED-%d",trackerDataName,runNumber));
106 //  
107 //  merged->SetTitle(Form("%s merged for run %d",trackerDataName,runNumber));
108 //  
109 //
110 //  return merged;
111 //}
112
113 //______________________________________________________________________________
114 void GenerateFileList(const char* runlist="philippe.runlist")
115 {
116   if (!gGrid)
117   {
118     TGrid::Connect("alien://");    
119   }
120   if (!gGrid) return;
121   
122   ifstream in(runlist);
123   Int_t run;
124   
125   Int_t n(0);
126   
127   while ( in >> run )
128   {
129     TString basedir("/alice/data/2009/LHC09d/");
130     
131     TString dir(Form("%s%09d/ESDs/pass1/*/Merged.QA.Data.root",basedir.Data(),run));
132     
133     TGridResult* r = gGrid->Ls(dir.Data());
134
135     Int_t i(0);
136     
137     while ( r->GetFileName(i) ) 
138     {
139       cout << "alien://" << r->GetFileNamePath(i) << endl;
140       ++i;      
141     }
142     
143     delete r;
144     
145     ++n;
146   }
147 }
148
149 //______________________________________________________________________________
150 void CopyFromRemote(Int_t runNumber, const char* period, const char* pass)
151 {
152   TList* list = GetFileList(runNumber,period,pass);
153   if ( list )
154   {
155     CopyFromRemote(*list);
156   }
157   delete list;
158 }
159
160 //______________________________________________________________________________
161 void CopyFromRemote(const TList& files)
162 {
163   TStopwatch timer;
164   
165   TIter next(&files);
166   TObjString* line;
167   
168   while ( ( line = static_cast<TObjString*>(next()) ) )
169   {
170     TUrl url(line->String());
171
172     if ( TString(url.GetProtocol()) == "alien" )
173     {
174       if (!gGrid)
175       {
176         TGrid::Connect("alien://");
177         if (!gGrid)
178         {
179           cout << "Cannot get alien connection" << endl;
180           return;
181         }
182       }
183     }
184
185     TString file(url.GetFile());
186
187     TString dir(gSystem->DirName(file));
188
189     gSystem->mkdir(dir.Data(),kTRUE);
190
191     if ( gSystem->AccessPathName(file.Data())==kFALSE)
192     {
193       cout << "Skipping copy of " << file.Data() << " as it already exists" << endl;
194     }
195     else
196     {
197       TFile::Cp(line->String().Data(),file.Data());
198       if ( line->String().Contains("root_archive.zip") )
199       {
200         gSystem->Exec(Form("unzip %s -d %s",file.Data(),gSystem->DirName(file.Data())));
201         gSystem->Exec(Form("rm %s",file.Data()));
202       }
203     }
204   }
205   
206   timer.Print();
207 }
208
209 //______________________________________________________________________________
210 TList* GetFileList(Int_t runNumber, const char* period, const char* pass)
211 {
212   if (!gGrid) TGrid::Connect("alien://");
213   if (!gGrid)
214   {
215     return 0x0;
216   }
217
218   Int_t year;
219   
220   TString speriod(period);
221   
222   if (!speriod.BeginsWith("LHC"))
223   {
224     std::cerr << "Period not starting with LHC !" << std::endl;
225     return 0x0;
226   }
227   
228   year = 2000 + TString(speriod(3,3)).Atoi();
229   
230   TString sbasedir(Form("/alice/data/%d/%s/%09d/ESDs/%s",year,period,runNumber,pass));
231   
232   std::cout << sbasedir.Data() << std::endl;
233   
234   TString search("Merged.QA.Data.root");
235   
236   TGridResult* res = gGrid->Query(sbasedir.Data(),search.Data());
237   
238   Int_t nFiles = res->GetEntries();
239   
240   nFiles = res->GetEntries();
241   
242   Long64_t size(0);
243   Int_t count(0);
244   TList* files(0x0);
245   
246   for (Int_t i = 0; i < nFiles; ++i)
247   {
248     Long64_t thisSize = TString(res->GetKey(i,"size")).Atoll();
249     
250 //    TUrl url(res->GetKey(i,"turl"));
251     
252     if (!files)
253     {
254       files = new TList;
255       files->SetOwner(kTRUE);
256     }
257     
258     files->Add(new TObjString(res->GetKey(i,"turl")));
259     
260     ++count;
261     
262     size += thisSize;
263   }
264   
265   std::cout << Form("%d files for a total of %d MB",count,TMath::Nint(size/1024.0/1024)) << std::endl;
266
267   delete res;
268   
269   return files;
270 }
271
272 //______________________________________________________________________________
273 void QAMerge(Int_t runNumber, const char* period, const char* pass)
274 {
275   TList* list = GetFileList(runNumber,period,pass);
276   if ( list )
277   {
278     QAMerge(*list,Form("QA.%s.%s.%d.root",period,pass,runNumber));
279   }
280   delete list;
281
282 }
283
284 //______________________________________________________________________________
285 TObject* GetObject(const char* filepath, const char* objectPath)
286 {
287   TFile* f = TFile::Open(filepath);
288   
289   if (!f)
290   {
291     std::cerr << "Could not open file " << filepath << std::endl;
292     return 0x0;    
293   }
294   
295   TObject* o = f->Get(objectPath);
296   
297   if (!o)
298   {
299     std::cout << "Cannot get object " << objectPath << " from " << filepath << std::endl;
300     return 0x0;
301   }
302   
303   if ( o->InheritsFrom("TH1") )
304   {
305     TH1* h = static_cast<TH1*>(o);
306     h->SetDirectory(0);
307   }
308   
309   delete f;
310   
311   return o;
312 }
313
314 //______________________________________________________________________________
315 TObject* Merge(const TList& files, const char* objectPath)
316 {
317   TIter next(&files);
318   TObjString* filename;
319   
320   filename = static_cast<TObjString*>(next());
321   
322   TObject* d = GetObject(filename->String().Data(),objectPath);
323   
324   if (!d)
325   {
326     cout << Form("ERROR : no object found in %s",filename->String().Data()) << endl;
327     return 0x0;
328   }
329   
330   TString oname(gSystem->BaseName(objectPath));
331   
332   TObject* merged = d->Clone();
333   
334   TMethodCall callEnv;
335
336   if ( merged->IsA() )
337   {
338     callEnv.InitWithPrototype(merged->IsA(), "Merge", "TCollection*");
339   }
340   
341   if (!callEnv.IsValid())
342   {
343     std::cout << "callEnv invalid" << std::endl;
344     delete merged;
345     return 0x0;
346   }
347
348   while ( ( filename = static_cast<TObjString*>(next()) ) )
349   {
350 //    std::cout << filename->String().Data() << " : " << objectPath << std::endl;
351     
352     d = GetObject(filename->String().Data(),objectPath);
353     
354     if (!d)
355     {
356       continue;
357     }
358     
359     TList list;
360     list.Add(d);
361     callEnv.SetParam((Long_t) (&list));
362     callEnv.Execute(merged);
363     
364     delete d;
365   }
366   
367   return merged;
368
369 }
370
371 //______________________________________________________________________________
372 void QAMerge(const TList& files, const char* outputfile)
373 {
374   const char* eventSpecie = "HighMultiplicity";
375   
376   TList objectsToMerge;
377   objectsToMerge.SetOwner(kTRUE);
378
379   objectsToMerge.Add(new TObjString(Form("MUON/Raws/%s/Expert/%s_hTrackerDDLNofEventsSeen",eventSpecie,eventSpecie)));
380
381   objectsToMerge.Add(new TObjString(Form("MUON/RecPoints/%s/Expert/%s_RecPoints",eventSpecie,eventSpecie)));
382   objectsToMerge.Add(new TObjString(Form("MUON/RecPoints/%s/Expert/%s_RecCharges",eventSpecie,eventSpecie)));
383
384   objectsToMerge.Add(new TObjString(Form("MUON/Raws/%s/Expert/%s_RawCharges",eventSpecie,eventSpecie)));
385
386   for ( Int_t i = 1; i <= 10; ++i )
387   {
388     objectsToMerge.Add(new TObjString(Form("MUON/RecPoints/%s/Expert/%s_hTrackerClusterHitMapForChamber%d",eventSpecie,eventSpecie,i)));
389     objectsToMerge.Add(new TObjString(Form("MUON/ESDs/%s/Expert/%s_hESDClusterHitMap%d",eventSpecie,eventSpecie,i)));
390   }
391
392   TObjArray output;
393   output.SetOwner(kTRUE);
394
395   TIter next(&objectsToMerge);
396   TObjString* s;
397   
398   while ( ( s = static_cast<TObjString*>(next())) )
399   {
400     output.Add(Merge(files,s->String().Data()));
401   }
402   
403   TFile* f = TFile::Open(outputfile,"recreate");
404   
405   next.Reset();
406
407   Int_t i(0);
408   while ( ( s = static_cast<TObjString*>(next())) )
409   {
410     TObject* o = output.At(i);
411     if (o)
412     {
413       TString dir = gSystem->DirName(s->String());
414       TString file = gSystem->BaseName(s->String());
415       TDirectory* tdir = static_cast<TDirectory*>(f->Get(dir.Data()));
416       if (!tdir)
417       {
418         f->mkdir(dir.Data()," ");
419         tdir = static_cast<TDirectory*>(f->Get(dir.Data()));
420       }
421       tdir->cd();
422       o->Write(file.Data());
423     }
424     ++i;
425   }
426
427   f->Write();
428   
429   f->Close();
430   
431   delete f;
432 }
433
434 //______________________________________________________________________________
435 void QAMerge(const char* inputfilelist, const char* mergedFile)
436 {
437   if (!gGrid)
438   {
439     TGrid::Connect("alien://");    
440   }
441   if (!gGrid) return;
442   
443   TString sinput(inputfilelist);
444   TList filelist;
445   filelist.SetOwner(kTRUE);
446   
447   if (sinput.Contains(".xml"))
448   {
449     TGridCollection *coll = reinterpret_cast<TGridCollection*>(gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\");", inputfilelist)));
450     if (!coll)
451     {
452       ::Error("MergeOutput", "Input XML collection empty.");
453       return;
454     }
455     // Iterate grid collection
456     while (coll->Next())
457     {
458       filelist.Add(new TObjString(coll->GetTURL()));
459     }
460   }
461   else
462   {
463     ifstream in(inputfilelist);
464     char filename[1024];
465
466     while (in.getline(filename,1024,'\n'))
467     {
468       filelist.Add(new TObjString(filename));
469     }
470   }
471     
472   if ( filelist.GetEntries()==0 )
473   {
474     std::cout << "No file to be merged !" << std::endl;
475   }
476   else
477   {
478     QAMerge(filelist,mergedFile);
479   }
480   
481   ofstream out;
482   out.open("outputs_valid_merge", ios::out);
483   out.close();
484 }
485