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