]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEERBase/AliTagCreator.cxx
speed up with binary search
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliTagCreator.cxx
1 /**************************************************************************
2  * Author: Panos Christakoglou.                                           *
3  * Contributors are mentioned in the code where appropriate.              *
4  *                                                                        *
5  * Permission to use, copy, modify and distribute this software and its   *
6  * documentation strictly for non-commercial purposes is hereby granted   *
7  * without fee, provided that the above copyright notice appears in all   *
8  * copies and that both the copyright notice and this permission notice   *
9  * appear in the supporting documentation. The authors make no claims     *
10  * about the suitability of this software for any purpose. It is          *
11  * provided "as is" without express or implied warranty.                  *
12  **************************************************************************/
13
14 /* $Id$ */
15
16 //-----------------------------------------------------------------
17 //           AliTagCreator class
18 //   This is the class to deal with the tag creation (post process)
19 //   Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
21
22 //ROOT
23 #include <Riostream.h>
24 #include <TFile.h>
25 #include <TString.h>
26 #include <TTree.h>
27 #include <TSystem.h>
28 #include <TChain.h>
29 #include <TLorentzVector.h>
30
31 //ROOT-AliEn
32 #include <TGrid.h>
33 #include <TGridResult.h>
34
35 //AliRoot
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
38 #include "AliLog.h"
39
40 #include "AliTagCreator.h"
41
42
43 using std::ifstream;
44 ClassImp(AliTagCreator)
45
46
47 //______________________________________________________________________________
48   AliTagCreator::AliTagCreator() :
49     TObject(),
50     fSE("ALICE::CERN::se"),
51     fgridpath(""),
52     fStorage(0)
53 {
54   //==============Default constructor for a AliTagCreator==================
55 }
56
57 //______________________________________________________________________________
58 AliTagCreator::~AliTagCreator() {
59 //================Default destructor for a AliTagCreator=======================
60 }
61
62 //______________________________________________________________________________
63 void AliTagCreator::SetStorage(Int_t storage) {
64   // Sets correctly the storage: 0 for local, 1 for GRID
65   fStorage = storage;
66   if(fStorage == 0)
67     AliInfo(Form("Tags will be stored locally...."));
68   if(fStorage == 1)
69     AliInfo(Form("Tags will be stored in the grid...."));
70   if((fStorage != 0)&&(fStorage != 1))
71     {
72       AliInfo(Form("Storage was not properly set!!!"));
73       abort();
74     }  
75 }
76
77 //__________________________________________________________________________
78 Bool_t AliTagCreator::MergeTags(const char *type) {
79   //Merges the tags and stores the merged tag file 
80   //locally if fStorage=0 or in the grid if fStorage=1
81   AliInfo(Form("Merging tags....."));
82   TChain *fgChain = new TChain("T");
83   TString tagPattern = type; tagPattern += ".tag.root"; 
84
85   if(fStorage == 0) {
86     // Open the working directory
87     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
88     const char * name = 0x0;
89     // Add all files matching *pattern* to the chain
90     while((name = gSystem->GetDirEntry(dirp))) {
91       if (strstr(name,tagPattern)) fgChain->Add(name);  
92     }//directory loop
93     AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
94   }//local mode
95
96   else if(fStorage == 1) {
97     TString alienLocation = gGrid->Pwd();
98     alienLocation += fgridpath.Data();
99     alienLocation += "/";
100     TString queryPattern = "*."; queryPattern += tagPattern;
101     TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
102     Int_t nEntries = tagresult->GetEntries();
103     for(Int_t i = 0; i < nEntries; i++) {
104       TString alienUrl = tagresult->GetKey(i,"turl");
105       fgChain->Add(alienUrl);
106     }//grid result loop      
107     AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
108   }//grid mode
109  
110   AliRunTag *tag = new AliRunTag;
111   fgChain->SetBranchAddress("AliTAG",&tag);
112   fgChain->GetEntry(0);
113   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
114   localFileName += ".Merged."; localFileName += tagPattern.Data();
115      
116   TString filename;
117   
118   if(fStorage == 0) {
119     filename = localFileName.Data();      
120     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
121   } 
122   else if(fStorage == 1) {
123     TString alienFileName = "/alien";
124     alienFileName += gGrid->Pwd();
125     alienFileName += fgridpath.Data();
126     alienFileName += "/";
127     alienFileName +=  localFileName;
128     alienFileName += "?se=";
129     alienFileName += fSE.Data();
130     filename = alienFileName.Data();
131     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
132   }
133
134   fgChain->Merge(filename);
135   gSystem->Exec("rm Run*.Event*");
136
137   return kTRUE;
138 }
139
140
141 //__________________________________________________________________________
142 Bool_t AliTagCreator::MergeTags(const char *type, const char *inflist) {
143   //Merges the tags and stores the merged tag file 
144   //locally if fStorage=0 or in the grid if fStorage=1
145   AliInfo(Form("Merging tags....."));
146   TChain *fgChain = new TChain("T");
147   TString tagPattern = type; tagPattern += ".tag.root"; 
148
149 //   if(fStorage == 0) {
150 //     // Open the working directory
151 //     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
152 //     const char * name = 0x0;
153 //     // Add all files matching *pattern* to the chain
154 //     while((name = gSystem->GetDirEntry(dirp))) {
155 //       if (strstr(name,tagPattern)) fgChain->Add(name);  
156 //     }//directory loop
157 //     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
158 //   }//local mode
159
160 //   else if(fStorage == 1) {
161 //     TString alienLocation = gGrid->Pwd();
162 //     alienLocation += fgridpath.Data();
163 //     alienLocation += "/";
164 //     TString queryPattern = "*."; queryPattern += tagPattern;
165 //     TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
166 //     Int_t nEntries = tagresult->GetEntries();
167 //     for(Int_t i = 0; i < nEntries; i++) {
168 //       TString alienUrl = tagresult->GetKey(i,"turl");
169 //       fgChain->Add(alienUrl);
170 //     }//grid result loop      
171 //     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
172 //   }//grid mode
173
174   if (fStorage == 0) {
175     ifstream *istr = new ifstream(inflist);
176     char fname[200];
177     while (!(istr->eof())) {
178       (*istr) >> fname;
179       if (strstr(fname, tagPattern)) fgChain->Add(fname);
180     }
181     AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
182   }
183  
184   AliRunTag *tag = new AliRunTag;
185   fgChain->SetBranchAddress("AliTAG",&tag);
186   fgChain->GetEntry(0);
187   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
188   localFileName += ".Merged."; localFileName += tagPattern.Data();
189      
190   TString filename;
191   
192   if(fStorage == 0) {
193     filename = localFileName.Data();      
194     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
195   } 
196   else if(fStorage == 1) {
197     TString alienFileName = "/alien";
198     alienFileName += gGrid->Pwd();
199     alienFileName += fgridpath.Data();
200     alienFileName += "/";
201     alienFileName +=  localFileName;
202     alienFileName += "?se=";
203     alienFileName += fSE.Data();
204     filename = alienFileName.Data();
205     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
206   }
207
208   fgChain->Merge(filename);
209   gSystem->Exec("rm Run*.Event*");
210
211   return kTRUE;
212 }
213
214 //__________________________________________________________________________
215 Bool_t AliTagCreator::MergeTags(const char *type, TGridResult *result) {
216   //Merges the tags that are listed in the TGridResult 
217   AliInfo(Form("Merging tags....."));
218   TChain *fgChain = new TChain("T");
219   TString tagPattern = "."; tagPattern += type; tagPattern += ".tag.root";
220
221   Int_t nEntries = result->GetEntries();
222
223   TString alienUrl;
224   for(Int_t i = 0; i < nEntries; i++) {
225     alienUrl = result->GetKey(i,"turl");
226     fgChain->Add(alienUrl);  
227   }
228   AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
229   AliRunTag *tag = new AliRunTag;
230   fgChain->SetBranchAddress("AliTAG",&tag);
231   fgChain->GetEntry(0);
232     
233   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
234   localFileName += ".Merged"; localFileName += tagPattern.Data();
235      
236   TString filename;
237   
238   if(fStorage == 0) {
239     filename = localFileName.Data();      
240     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
241   } 
242   else if(fStorage == 1) {
243     TString alienFileName = "/alien";
244     alienFileName += gGrid->Pwd();
245     alienFileName += fgridpath.Data();
246     alienFileName += "/";
247     alienFileName +=  localFileName;
248     alienFileName += "?se=";
249     alienFileName += fSE.Data();
250     filename = alienFileName.Data();
251     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
252   }
253   
254   fgChain->Merge(filename);
255
256   return kTRUE;
257 }
258
259 Bool_t AliTagCreator::MergeTagsForRun(const char* type) {
260   //Merges the tags and stores the merged tag file 
261   //locally if fStorage=0 or in the grid if fStorage=1
262   AliInfo(Form("Merging tags....."));
263   TChain *fgChain = new TChain("T");
264   TString tagPattern = type; tagPattern += ".tag.root"; 
265
266   if(fStorage == 0) {
267     // Open the working directory
268     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
269     const char * name = 0x0;
270     // Add all files matching *pattern* to the chain
271     while((name = gSystem->GetDirEntry(dirp))) {
272       if (strstr(name,tagPattern)) fgChain->Add(name);  
273     }//directory loop
274     AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
275   }//local mode
276
277   else if(fStorage == 1) {
278     TString alienLocation = gGrid->Pwd();
279     alienLocation += fgridpath.Data();
280     alienLocation += "/";
281     TString queryPattern = "*."; queryPattern += tagPattern;
282     TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
283     Int_t nEntries = tagresult->GetEntries();
284     for(Int_t i = 0; i < nEntries; i++) {
285       TString alienUrl = tagresult->GetKey(i,"turl");
286       fgChain->Add(alienUrl);
287     }//grid result loop      
288     AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
289   }//grid mode
290  
291   AliRunTag *tag = new AliRunTag;
292   fgChain->SetBranchAddress("AliTAG",&tag);
293   fgChain->GetEntry(0);
294   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
295   localFileName += ".Merged."; localFileName += tagPattern.Data();
296      
297   TString filename;
298   
299   if(fStorage == 0) {
300     filename = localFileName.Data();      
301     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
302   } 
303   else if(fStorage == 1) {
304     TString alienFileName = "/alien";
305     alienFileName += gGrid->Pwd();
306     alienFileName += fgridpath.Data();
307     alienFileName += "/";
308     alienFileName +=  localFileName;
309     alienFileName += "?se=";
310     alienFileName += fSE.Data();
311     filename = alienFileName.Data();
312     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
313   }
314
315   //  fgChain->Merge(filename);
316   MergeToSingleRunTag(fgChain, filename);
317
318   gSystem->Exec("rm Run*.Event*");
319
320   return kTRUE;
321 }
322
323 Bool_t AliTagCreator::MergeTagsForRun(const char* type, TGridResult *result) {
324   //Merges the tags that are listed in the TGridResult 
325   AliInfo(Form("Merging tags....."));
326   TChain *fgChain = new TChain("T");
327   TString tagPattern = "."; tagPattern += type; tagPattern += ".tag.root";
328
329   Int_t nEntries = result->GetEntries();
330
331   TString alienUrl;
332   for(Int_t i = 0; i < nEntries; i++) {
333     alienUrl = result->GetKey(i,"turl");
334     fgChain->Add(alienUrl);  
335   }
336   AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
337   AliRunTag *tag = new AliRunTag;
338   fgChain->SetBranchAddress("AliTAG",&tag);
339   fgChain->GetEntry(0);
340     
341   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
342   localFileName += ".Merged"; localFileName += tagPattern.Data();
343      
344   TString filename;
345   
346   if(fStorage == 0) {
347     filename = localFileName.Data();      
348     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
349   } 
350   else if(fStorage == 1) {
351     TString alienFileName = "/alien";
352     alienFileName += gGrid->Pwd();
353     alienFileName += fgridpath.Data();
354     alienFileName += "/";
355     alienFileName +=  localFileName;
356     alienFileName += "?se=";
357     alienFileName += fSE.Data();
358     filename = alienFileName.Data();
359     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
360   }
361   
362   //  fgChain->Merge(filename);
363   MergeToSingleRunTag(fgChain, filename);
364
365   return kTRUE;
366 }
367
368 Bool_t AliTagCreator::MergeTagsForRun(const char* type, const char *inflist) {
369   //Merges the tags and stores the merged tag file 
370   //locally if fStorage=0 or in the grid if fStorage=1
371   AliInfo(Form("Merging tags....."));
372   TChain *fgChain = new TChain("T");
373   TString tagPattern = type; tagPattern += ".tag.root"; 
374
375 //   if(fStorage == 0) {
376 //     // Open the working directory
377 //     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
378 //     const char * name = 0x0;
379 //     // Add all files matching *pattern* to the chain
380 //     while((name = gSystem->GetDirEntry(dirp))) {
381 //       if (strstr(name,tagPattern)) fgChain->Add(name);  
382 //     }//directory loop
383 //     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
384 //   }//local mode
385
386 //   else if(fStorage == 1) {
387 //     TString alienLocation = gGrid->Pwd();
388 //     alienLocation += fgridpath.Data();
389 //     alienLocation += "/";
390 //     TString queryPattern = "*."; queryPattern += tagPattern;
391 //     TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
392 //     Int_t nEntries = tagresult->GetEntries();
393 //     for(Int_t i = 0; i < nEntries; i++) {
394 //       TString alienUrl = tagresult->GetKey(i,"turl");
395 //       fgChain->Add(alienUrl);
396 //     }//grid result loop      
397 //     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
398 //   }//grid mode
399
400   if (fStorage == 0) {
401     ifstream *istr = new ifstream(inflist);
402     char fname[200];
403     while (!(istr->eof())) {
404       (*istr) >> fname;
405       if (strstr(fname, tagPattern)) fgChain->Add(fname);
406     }
407     AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
408   }
409  
410   AliRunTag *tag = new AliRunTag;
411   fgChain->SetBranchAddress("AliTAG",&tag);
412   fgChain->GetEntry(0);
413   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
414   localFileName += ".Merged."; localFileName += tagPattern.Data();
415      
416   TString filename;
417   
418   if(fStorage == 0) {
419     filename = localFileName.Data();      
420     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
421   } 
422   else if(fStorage == 1) {
423     TString alienFileName = "/alien";
424     alienFileName += gGrid->Pwd();
425     alienFileName += fgridpath.Data();
426     alienFileName += "/";
427     alienFileName +=  localFileName;
428     alienFileName += "?se=";
429     alienFileName += fSE.Data();
430     filename = alienFileName.Data();
431     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
432   }
433
434   //  fgChain->Merge(filename);
435   MergeToSingleRunTag(fgChain, filename);
436
437   gSystem->Exec("rm Run*.Event*");
438
439   return kTRUE;
440 }
441
442
443 Bool_t AliTagCreator::MergeToSingleRunTag(TChain *chain, const char *filename)
444 {
445   // Merge all tags for a given run into a single RunTag
446   TFile* ftag = TFile::Open(filename, "recreate");
447
448   AliRunTag *tag = new AliRunTag;
449   TTree * ttag = new TTree("T","A Tree with event tags");
450   TBranch * btag = ttag->Branch("AliTAG", &tag, 1000000);
451   btag->SetCompressionLevel(9);
452   ttag->AutoSave("10000");
453
454   AliRunTag *rtag = new AliRunTag();
455   chain->SetBranchAddress("AliTAG", &rtag);
456
457   AliFileTag *evt;
458
459   if (chain->GetEntries()) {
460     chain->GetEntry(0);
461     tag->CopyStandardContent(rtag);
462     tag->Clear();
463
464     int runno = rtag->GetRunId();
465
466     for (int iter=0; iter<chain->GetEntries(); iter++) {
467       chain->GetEntry(iter);
468       if (runno != rtag->GetRunId()) {
469         AliInfo(Form("Run tag ID %i is different from the Run ID for the merged run: %i\n", rtag->GetRunId(), runno));
470         continue;
471       }
472
473       for (int iev=0; iev<rtag->GetNFiles(); iev++) {
474         evt = (AliFileTag *) rtag->GetFileTag(iev);
475         tag->AddFileTag(new AliFileTag(*evt));
476       }
477     }
478   }
479   else {
480     AliInfo("Found no tag files to merge.");
481     return kFALSE;
482   }
483
484   ttag->Fill();
485
486   ftag->cd();
487   tag->Clear();
488   ttag->Write();
489   ftag->Close();
490   
491   return kTRUE;
492 }
493
494