]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTagAnalysis.cxx
Changes required by Effective C++
[u/mrichter/AliRoot.git] / STEER / AliTagAnalysis.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 //           AliTagAnalysis class
18 //   This is the class to deal with the tag analysis
19 //   Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
21
22 //ROOT
23 #include <TSystem.h>
24 #include <TChain.h>
25 #include <TFile.h>
26 #include <TEventList.h>
27
28 //ROOT-AliEn
29 #include <TGridResult.h>
30
31 #include "AliLog.h"
32
33 #include "AliRunTag.h"
34 #include "AliEventTag.h"
35 #include "AliTagAnalysis.h"
36 #include "AliEventTagCuts.h"
37 #include "AliXMLCollection.h"
38
39 class TTree;
40
41 ClassImp(AliTagAnalysis)
42
43 TChain *AliTagAnalysis::fgChain = 0;
44
45 //___________________________________________________________________________
46 AliTagAnalysis::AliTagAnalysis(): 
47   TObject(),
48   ftagresult(0x0),
49   fTagDirName(),
50   fChain(0x0)
51 {
52   //Default constructor for a AliTagAnalysis
53 }
54
55 //___________________________________________________________________________
56 AliTagAnalysis::~AliTagAnalysis() {
57 //Default destructor for a AliTagAnalysis
58 }
59
60 //___________________________________________________________________________
61 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
62   //Searches the entries of the provided direcory
63   //Chains the tags that are stored locally
64   fTagDirName = dirname;
65   TString fTagFilename;
66   
67   TChain *fgChain = new TChain("T");
68   fChain = fgChain;
69   
70   const char * tagPattern = "tag.root";
71   // Open the working directory
72   void * dirp = gSystem->OpenDirectory(fTagDirName);
73   const char * name = 0x0;
74   // Add all files matching *pattern* to the chain
75   while((name = gSystem->GetDirEntry(dirp))) {
76     if (strstr(name,tagPattern)) { 
77       fTagFilename = fTagDirName;
78       fTagFilename += "/";
79       fTagFilename += name;
80                 
81       TFile * fTag = TFile::Open(fTagFilename);
82       if((!fTag) || (!fTag->IsOpen())) {
83         AliError(Form("Tag file not opened!!!"));
84         continue;
85       } 
86       fChain->Add(fTagFilename);  
87       fTag->Close();
88       delete fTag;
89     }//pattern check
90   }//directory loop
91   AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
92 }
93
94
95 //___________________________________________________________________________
96 void AliTagAnalysis::ChainGridTags(TGridResult *res) {
97   //Loops overs the entries of the TGridResult
98   //Chains the tags that are stored in the GRID
99   ftagresult = res;
100   Int_t nEntries = ftagresult->GetEntries();
101  
102   TChain *fgChain = new TChain("T");
103   fChain = fgChain;
104
105   TString gridname = "alien://";
106   TString alienUrl;
107  
108   for(Int_t i = 0; i < nEntries; i++) {
109     alienUrl = ftagresult->GetKey(i,"turl");
110     TFile *f = TFile::Open(alienUrl,"READ");
111     fChain->Add(alienUrl);
112     delete f;    
113   }//grid result loop  
114 }
115
116
117 //___________________________________________________________________________
118 TChain *AliTagAnalysis::QueryTags(AliEventTagCuts *EvTagCuts) {
119   //Queries the tag chain using the defined 
120   //event tag cuts from the AliEventTagCuts object
121   //and returns a TChain along with the associated TEventList
122   AliInfo(Form("Querying the tags........"));
123   
124   //ESD file chain
125   TChain *fESDchain = new TChain("esdTree");
126   //Event list
127   TEventList *fEventList = new TEventList();
128   
129   //Defining tag objects
130   AliRunTag *tag = new AliRunTag;
131   AliEventTag *evTag = new AliEventTag;
132   fChain->SetBranchAddress("AliTAG",&tag);
133
134   TString guid = 0;
135   TString turl = 0;
136   TString path = 0;
137
138   Int_t iAccepted = 0;
139   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
140     fChain->GetEntry(iTagFiles);
141     Int_t iEvents = tag->GetNEvents();
142     const TClonesArray *tagList = tag->GetEventTags();
143     for(Int_t i = 0; i < iEvents; i++) {
144       evTag = (AliEventTag *) tagList->At(i);
145       guid = evTag->GetGUID(); 
146       turl = evTag->GetTURL(); 
147       path = evTag->GetPath();
148       if(EvTagCuts->IsAccepted(evTag)) fEventList->Enter(iAccepted+i);
149     }//event loop
150     iAccepted += iEvents;
151     
152     if(path != "") fESDchain->AddFile(path);
153     else if(turl != "") fESDchain->AddFile(turl);
154   }//tag file loop
155   AliInfo(Form("Accepted events: %d",fEventList->GetN()));
156   fESDchain->SetEventList(fEventList);
157    
158   return fESDchain;
159 }
160
161 //___________________________________________________________________________
162 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, AliEventTagCuts *EvTagCuts) {
163   //Queries the tag chain using the defined 
164   //event tag cuts from the AliEventTagCuts object
165   //and returns a XML collection
166   AliInfo(Form("Creating the collection........"));
167
168   AliXMLCollection *collection = new AliXMLCollection();
169   collection->SetCollectionName(name);
170   collection->WriteHeader();
171
172   //Event list
173   TEventList *fEventList = new TEventList();
174   TString guid = 0x0;
175   
176   //Defining tag objects
177   AliRunTag *tag = new AliRunTag;
178   AliEventTag *evTag = new AliEventTag;
179   fChain->SetBranchAddress("AliTAG",&tag);
180
181   for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
182     fChain->GetEntry(iTagFiles);
183     Int_t iEvents = tag->GetNEvents();
184     const TClonesArray *tagList = tag->GetEventTags();
185     for(Int_t i = 0; i < iEvents; i++) {
186       evTag = (AliEventTag *) tagList->At(i);
187       guid = evTag->GetGUID(); 
188       if(EvTagCuts->IsAccepted(evTag)) fEventList->Enter(i);
189     }//event loop
190     collection->WriteBody(iTagFiles+1,guid,fEventList);
191     fEventList->Clear();
192   }//tag file loop
193   collection->Export();
194
195   return kTRUE;
196 }
197