1 /**************************************************************************
2 * Author: Panos Christakoglou. *
3 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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 //-----------------------------------------------------------------
23 #include <Riostream.h>
29 #include <TLorentzVector.h>
33 #include <TGridResult.h>
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
40 #include "AliTagCreator.h"
44 ClassImp(AliTagCreator)
47 //______________________________________________________________________________
48 AliTagCreator::AliTagCreator() :
50 fSE("ALICE::CERN::se"),
54 //==============Default constructor for a AliTagCreator==================
57 //______________________________________________________________________________
58 AliTagCreator::~AliTagCreator() {
59 //================Default destructor for a AliTagCreator=======================
62 //______________________________________________________________________________
63 void AliTagCreator::SetStorage(Int_t storage) {
64 // Sets correctly the storage: 0 for local, 1 for GRID
67 AliInfo(Form("Tags will be stored locally...."));
69 AliInfo(Form("Tags will be stored in the grid...."));
70 if((fStorage != 0)&&(fStorage != 1))
72 AliInfo(Form("Storage was not properly set!!!"));
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";
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);
93 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
96 else if(fStorage == 1) {
97 TString alienLocation = gGrid->Pwd();
98 alienLocation += fgridpath.Data();
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);
107 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
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();
119 filename = localFileName.Data();
120 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
134 fgChain->Merge(filename);
135 gSystem->Exec("rm Run*.Event*");
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";
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);
157 // AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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()));
175 ifstream *istr = new ifstream(inflist);
177 while (!(istr->eof())) {
179 if (strstr(fname, tagPattern)) fgChain->Add(fname);
181 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
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();
193 filename = localFileName.Data();
194 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
208 fgChain->Merge(filename);
209 gSystem->Exec("rm Run*.Event*");
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";
221 Int_t nEntries = result->GetEntries();
224 for(Int_t i = 0; i < nEntries; i++) {
225 alienUrl = result->GetKey(i,"turl");
226 fgChain->Add(alienUrl);
228 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
229 AliRunTag *tag = new AliRunTag;
230 fgChain->SetBranchAddress("AliTAG",&tag);
231 fgChain->GetEntry(0);
233 TString localFileName = "Run"; localFileName += tag->GetRunId();
234 localFileName += ".Merged"; localFileName += tagPattern.Data();
239 filename = localFileName.Data();
240 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
254 fgChain->Merge(filename);
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";
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);
274 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
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);
288 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
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();
300 filename = localFileName.Data();
301 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
315 // fgChain->Merge(filename);
316 MergeToSingleRunTag(fgChain, filename);
318 gSystem->Exec("rm Run*.Event*");
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";
329 Int_t nEntries = result->GetEntries();
332 for(Int_t i = 0; i < nEntries; i++) {
333 alienUrl = result->GetKey(i,"turl");
334 fgChain->Add(alienUrl);
336 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
337 AliRunTag *tag = new AliRunTag;
338 fgChain->SetBranchAddress("AliTAG",&tag);
339 fgChain->GetEntry(0);
341 TString localFileName = "Run"; localFileName += tag->GetRunId();
342 localFileName += ".Merged"; localFileName += tagPattern.Data();
347 filename = localFileName.Data();
348 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
362 // fgChain->Merge(filename);
363 MergeToSingleRunTag(fgChain, filename);
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";
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);
383 // AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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()));
401 ifstream *istr = new ifstream(inflist);
403 while (!(istr->eof())) {
405 if (strstr(fname, tagPattern)) fgChain->Add(fname);
407 AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
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();
419 filename = localFileName.Data();
420 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
434 // fgChain->Merge(filename);
435 MergeToSingleRunTag(fgChain, filename);
437 gSystem->Exec("rm Run*.Event*");
443 Bool_t AliTagCreator::MergeToSingleRunTag(TChain *chain, const char *filename)
445 // Merge all tags for a given run into a single RunTag
446 TFile* ftag = TFile::Open(filename, "recreate");
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");
454 AliRunTag *rtag = new AliRunTag();
455 chain->SetBranchAddress("AliTAG", &rtag);
459 if (chain->GetEntries()) {
461 tag->CopyStandardContent(rtag);
464 int runno = rtag->GetRunId();
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));
473 for (int iev=0; iev<rtag->GetNFiles(); iev++) {
474 evt = (AliFileTag *) rtag->GetFileTag(iev);
475 tag->AddFileTag(new AliFileTag(*evt));
480 AliInfo("Found no tag files to merge.");