Added V0A23 (V0 rings 2-3), V0C01 (V0 rings 0-1) and V0S = V0A23+V0C01
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliTagCreator.cxx
CommitLineData
b45e5084 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
c336835b 23#include <Riostream.h>
b45e5084 24#include <TFile.h>
25#include <TString.h>
cb1645b7 26#include <TTree.h>
4fd84456 27#include <TSystem.h>
28#include <TChain.h>
2bdb9d38 29#include <TLorentzVector.h>
b45e5084 30
31//ROOT-AliEn
32#include <TGrid.h>
b45e5084 33#include <TGridResult.h>
b45e5084 34
35//AliRoot
36#include "AliRunTag.h"
37#include "AliEventTag.h"
b45e5084 38#include "AliLog.h"
39
b45e5084 40#include "AliTagCreator.h"
41
42
66b0310c 43using std::ifstream;
b45e5084 44ClassImp(AliTagCreator)
45
46
47//______________________________________________________________________________
442646f2 48 AliTagCreator::AliTagCreator() :
49 TObject(),
b2dc099f 50 fSE("ALICE::CERN::se"),
442646f2 51 fgridpath(""),
52 fStorage(0)
53{
b45e5084 54 //==============Default constructor for a AliTagCreator==================
b45e5084 55}
56
57//______________________________________________________________________________
c336835b 58AliTagCreator::~AliTagCreator() {
b45e5084 59//================Default destructor for a AliTagCreator=======================
60}
61
62//______________________________________________________________________________
c336835b 63void AliTagCreator::SetStorage(Int_t storage) {
cb1645b7 64 // Sets correctly the storage: 0 for local, 1 for GRID
28afeb2e 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
4fd84456 77//__________________________________________________________________________
5b8d5d69 78Bool_t AliTagCreator::MergeTags(const char *type) {
4fd84456 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");
5b8d5d69 83 TString tagPattern = type; tagPattern += ".tag.root";
4fd84456 84
85 if(fStorage == 0) {
4fd84456 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))) {
22c08f55 91 if (strstr(name,tagPattern)) fgChain->Add(name);
4fd84456 92 }//directory loop
73bbf779 93 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
4fd84456 94 }//local mode
95
96 else if(fStorage == 1) {
97 TString alienLocation = gGrid->Pwd();
98 alienLocation += fgridpath.Data();
99 alienLocation += "/";
5b8d5d69 100 TString queryPattern = "*."; queryPattern += tagPattern;
101 TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
4fd84456 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
73bbf779 107 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
4fd84456 108 }//grid mode
109
110 AliRunTag *tag = new AliRunTag;
4fd84456 111 fgChain->SetBranchAddress("AliTAG",&tag);
b2dc099f 112 fgChain->GetEntry(0);
442646f2 113 TString localFileName = "Run"; localFileName += tag->GetRunId();
5b8d5d69 114 localFileName += ".Merged."; localFileName += tagPattern.Data();
442646f2 115
9b4aee57 116 TString filename;
442646f2 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 }
442646f2 133
b2dc099f 134 fgChain->Merge(filename);
0e7ce776 135 gSystem->Exec("rm Run*.Event*");
442646f2 136
137 return kTRUE;
138}
139
850d5792 140
141//__________________________________________________________________________
142Bool_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 }
73bbf779 181 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
850d5792 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
442646f2 214//__________________________________________________________________________
5b8d5d69 215Bool_t AliTagCreator::MergeTags(const char *type, TGridResult *result) {
442646f2 216 //Merges the tags that are listed in the TGridResult
217 AliInfo(Form("Merging tags....."));
218 TChain *fgChain = new TChain("T");
5b8d5d69 219 TString tagPattern = "."; tagPattern += type; tagPattern += ".tag.root";
442646f2 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 }
73bbf779 228 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
442646f2 229 AliRunTag *tag = new AliRunTag;
442646f2 230 fgChain->SetBranchAddress("AliTAG",&tag);
b2dc099f 231 fgChain->GetEntry(0);
232
4fd84456 233 TString localFileName = "Run"; localFileName += tag->GetRunId();
5b8d5d69 234 localFileName += ".Merged"; localFileName += tagPattern.Data();
4fd84456 235
9b4aee57 236 TString filename;
4fd84456 237
238 if(fStorage == 0) {
239 filename = localFileName.Data();
240 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
241 }
c336835b 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();
4fd84456 250 filename = alienFileName.Data();
251 AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));
252 }
253
b2dc099f 254 fgChain->Merge(filename);
4fd84456 255
256 return kTRUE;
257}
850d5792 258
259Bool_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
73bbf779 274 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
850d5792 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
73bbf779 288 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
850d5792 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
323Bool_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 }
73bbf779 336 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
850d5792 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
368Bool_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 }
73bbf779 407 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
850d5792 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
443Bool_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");
04cb11d4 450 TBranch * btag = ttag->Branch("AliTAG", &tag, 1000000);
850d5792 451 btag->SetCompressionLevel(9);
04cb11d4 452 ttag->AutoSave("10000");
850d5792 453
454 AliRunTag *rtag = new AliRunTag();
455 chain->SetBranchAddress("AliTAG", &rtag);
456
04cb11d4 457 AliFileTag *evt;
458
850d5792 459 if (chain->GetEntries()) {
460 chain->GetEntry(0);
461 tag->CopyStandardContent(rtag);
04cb11d4 462 tag->Clear();
850d5792 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
04cb11d4 473 for (int iev=0; iev<rtag->GetNFiles(); iev++) {
474 evt = (AliFileTag *) rtag->GetFileTag(iev);
475 tag->AddFileTag(new AliFileTag(*evt));
850d5792 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