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"
43 ClassImp(AliTagCreator)
46 //______________________________________________________________________________
47 AliTagCreator::AliTagCreator() :
49 fSE("ALICE::CERN::se"),
53 //==============Default constructor for a AliTagCreator==================
56 //______________________________________________________________________________
57 AliTagCreator::~AliTagCreator() {
58 //================Default destructor for a AliTagCreator=======================
61 //______________________________________________________________________________
62 void AliTagCreator::SetStorage(Int_t storage) {
63 // Sets correctly the storage: 0 for local, 1 for GRID
66 AliInfo(Form("Tags will be stored locally...."));
68 AliInfo(Form("Tags will be stored in the grid...."));
69 if((fStorage != 0)&&(fStorage != 1))
71 AliInfo(Form("Storage was not properly set!!!"));
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";
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);
92 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
95 else if(fStorage == 1) {
96 TString alienLocation = gGrid->Pwd();
97 alienLocation += fgridpath.Data();
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);
106 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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();
118 filename = localFileName.Data();
119 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
133 fgChain->Merge(filename);
134 gSystem->Exec("rm Run*.Event*");
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";
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);
156 // AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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()));
174 ifstream *istr = new ifstream(inflist);
176 while (!(istr->eof())) {
178 if (strstr(fname, tagPattern)) fgChain->Add(fname);
180 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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();
192 filename = localFileName.Data();
193 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
207 fgChain->Merge(filename);
208 gSystem->Exec("rm Run*.Event*");
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";
220 Int_t nEntries = result->GetEntries();
223 for(Int_t i = 0; i < nEntries; i++) {
224 alienUrl = result->GetKey(i,"turl");
225 fgChain->Add(alienUrl);
227 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
228 AliRunTag *tag = new AliRunTag;
229 fgChain->SetBranchAddress("AliTAG",&tag);
230 fgChain->GetEntry(0);
232 TString localFileName = "Run"; localFileName += tag->GetRunId();
233 localFileName += ".Merged"; localFileName += tagPattern.Data();
238 filename = localFileName.Data();
239 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
253 fgChain->Merge(filename);
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";
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);
273 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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);
287 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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();
299 filename = localFileName.Data();
300 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
314 // fgChain->Merge(filename);
315 MergeToSingleRunTag(fgChain, filename);
317 gSystem->Exec("rm Run*.Event*");
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";
328 Int_t nEntries = result->GetEntries();
331 for(Int_t i = 0; i < nEntries; i++) {
332 alienUrl = result->GetKey(i,"turl");
333 fgChain->Add(alienUrl);
335 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
336 AliRunTag *tag = new AliRunTag;
337 fgChain->SetBranchAddress("AliTAG",&tag);
338 fgChain->GetEntry(0);
340 TString localFileName = "Run"; localFileName += tag->GetRunId();
341 localFileName += ".Merged"; localFileName += tagPattern.Data();
346 filename = localFileName.Data();
347 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
361 // fgChain->Merge(filename);
362 MergeToSingleRunTag(fgChain, filename);
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";
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);
382 // AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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()));
400 ifstream *istr = new ifstream(inflist);
402 while (!(istr->eof())) {
404 if (strstr(fname, tagPattern)) fgChain->Add(fname);
406 AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
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();
418 filename = localFileName.Data();
419 AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
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()));
433 // fgChain->Merge(filename);
434 MergeToSingleRunTag(fgChain, filename);
436 gSystem->Exec("rm Run*.Event*");
442 Bool_t AliTagCreator::MergeToSingleRunTag(TChain *chain, const char *filename)
444 // Merge all tags for a given run into a single RunTag
445 TFile* ftag = TFile::Open(filename, "recreate");
447 AliRunTag *tag = new AliRunTag;
448 TTree * ttag = new TTree("T","A Tree with event tags");
449 TBranch * btag = ttag->Branch("AliTAG", &tag);
450 btag->SetCompressionLevel(9);
452 AliRunTag *rtag = new AliRunTag();
453 chain->SetBranchAddress("AliTAG", &rtag);
455 if (chain->GetEntries()) {
457 tag->CopyStandardContent(rtag);
459 int runno = rtag->GetRunId();
461 for (int iter=0; iter<chain->GetEntries(); iter++) {
462 chain->GetEntry(iter);
463 if (runno != rtag->GetRunId()) {
464 AliInfo(Form("Run tag ID %i is different from the Run ID for the merged run: %i\n", rtag->GetRunId(), runno));
468 for (int iev=0; iev<rtag->GetEventTags()->GetEntries(); iev++) {
469 tag->AddEventTag(*((AliEventTag *) rtag->GetEventTags()->At(iev)));
474 AliInfo("Found no tag files to merge.");