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 // AliTagAnalysis class
18 // This is the class to deal with the tag analysis
19 // Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
26 #include <TEventList.h>
27 #include <TEntryList.h>
28 #include <TTreeFormula.h>
31 #include <TGridResult.h>
35 #include "AliRunTag.h"
36 #include "AliEventTag.h"
37 #include "AliTagAnalysis.h"
38 #include "AliEventTagCuts.h"
39 #include "AliDetectorTagCuts.h"
40 #include "AliLHCTagCuts.h"
41 #include "AliRunTagCuts.h"
42 #include "AliXMLCollection.h"
46 ClassImp(AliTagAnalysis)
48 //___________________________________________________________________________
49 AliTagAnalysis::AliTagAnalysis():
55 //Default constructor for a AliTagAnalysis
58 //___________________________________________________________________________
59 AliTagAnalysis::~AliTagAnalysis() {
60 //Default destructor for a AliTagAnalysis
63 //___________________________________________________________________________
64 Bool_t AliTagAnalysis::AddTagsFile(const char *alienUrl) {
66 // Add a single tags file to the chain
70 if (! fChain) fChain = new TChain("T");
72 TFile *f = TFile::Open(alienUrl,"READ");
73 fChain->Add(alienUrl);
74 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
77 if (fChain->GetEntries() == 0 )
83 //___________________________________________________________________________
84 void AliTagAnalysis::ChainLocalTags(const char *dirname) {
85 //Searches the entries of the provided direcory
86 //Chains the tags that are stored locally
87 fTagDirName = dirname;
90 if (! fChain) fChain = new TChain("T");
92 const char * tagPattern = "tag.root";
93 // Open the working directory
94 void * dirp = gSystem->OpenDirectory(fTagDirName);
95 const char * name = 0x0;
96 // Add all files matching *pattern* to the chain
97 while((name = gSystem->GetDirEntry(dirp))) {
98 if (strstr(name,tagPattern)) {
99 fTagFilename = fTagDirName;
101 fTagFilename += name;
103 fChain->Add(fTagFilename);
106 AliInfo(Form("Chained tag files: %d ",fChain->GetEntries()));
110 //___________________________________________________________________________
111 void AliTagAnalysis::ChainGridTags(TGridResult *res) {
112 //Loops overs the entries of the TGridResult
113 //Chains the tags that are stored in the GRID
115 Int_t nEntries = ftagresult->GetEntries();
117 if (! fChain) fChain = new TChain("T");
119 TString gridname = "alien://";
122 for(Int_t i = 0; i < nEntries; i++) {
123 alienUrl = ftagresult->GetKey(i,"turl");
124 fChain->Add(alienUrl);
129 //___________________________________________________________________________
130 TChain *AliTagAnalysis::QueryTags(AliRunTagCuts *runTagCuts, AliLHCTagCuts *lhcTagCuts, AliDetectorTagCuts *detTagCuts, AliEventTagCuts *evTagCuts) {
131 //Queries the tag chain using the defined
132 //event tag cuts from the AliEventTagCuts object
133 //and returns a TChain along with the associated TEventList
134 AliInfo(Form("Querying the tags........"));
136 TString fAliceFile = "esdTree";
139 TChain *fESDchain = new TChain(fAliceFile.Data());
141 TEntryList *fGlobalList = new TEntryList();
143 //Defining tag objects
144 AliRunTag *tag = new AliRunTag;
145 AliEventTag *evTag = new AliEventTag;
146 fChain->SetBranchAddress("AliTAG",&tag);
153 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
154 fChain->GetEntry(iTagFiles);
155 if(runTagCuts->IsAccepted(tag)) {
156 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
157 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
158 TEntryList *fLocalList = new TEntryList();
159 Int_t iEvents = tag->GetNEvents();
160 const TClonesArray *tagList = tag->GetEventTags();
161 for(Int_t i = 0; i < iEvents; i++) {
162 evTag = (AliEventTag *) tagList->At(i);
163 guid = evTag->GetGUID();
164 turl = evTag->GetTURL();
165 path = evTag->GetPath();
166 fLocalList->SetTreeName(fAliceFile.Data());
167 if(turl!="") fLocalList->SetFileName(turl.Data());
168 else fLocalList->SetFileName(path.Data());
169 if(evTagCuts->IsAccepted(evTag)) fLocalList->Enter(i);
172 if(path != "") fESDchain->AddFile(path);
173 else if(turl != "") fESDchain->AddFile(turl);
174 fGlobalList->Add(fLocalList);
175 iAccepted += fLocalList->GetN();
180 AliInfo(Form("Accepted events: %d",iAccepted));
181 fESDchain->SetEntryList(fGlobalList,"ne");
186 //___________________________________________________________________________
187 TChain *AliTagAnalysis::QueryTags(const char *fRunCut, const char *fLHCCut, const char *fDetectorCut, const char *fEventCut) {
188 //Queries the tag chain using the defined
189 //event tag cuts from the AliEventTagCuts object
190 //and returns a TChain along with the associated TEventList
191 AliInfo(Form("Querying the tags........"));
193 TString fAliceFile = "esdTree";
196 TChain *fESDchain = new TChain(fAliceFile.Data());
198 TEntryList *fGlobalList = new TEntryList();
200 //Defining tag objects
201 AliRunTag *tag = new AliRunTag;
202 AliEventTag *evTag = new AliEventTag;
203 fChain->SetBranchAddress("AliTAG",&tag);
209 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
210 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
211 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
212 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
216 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
217 fChain->GetEntry(iTagFiles);
218 if (current != fChain->GetTreeNumber()) {
219 fRunFormula->UpdateFormulaLeaves();
220 fLHCFormula->UpdateFormulaLeaves();
221 fDetectorFormula->UpdateFormulaLeaves();
222 fEventFormula->UpdateFormulaLeaves();
223 current = fChain->GetTreeNumber();
225 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
226 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
227 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
228 TEntryList *fLocalList = new TEntryList();
229 Int_t iEvents = fEventFormula->GetNdata();
230 const TClonesArray *tagList = tag->GetEventTags();
231 for(Int_t i = 0; i < iEvents; i++) {
232 evTag = (AliEventTag *) tagList->At(i);
233 guid = evTag->GetGUID();
234 turl = evTag->GetTURL();
235 path = evTag->GetPath();
236 fLocalList->SetTreeName(fAliceFile.Data());
237 fLocalList->SetFileName(turl.Data());
238 if(fEventFormula->EvalInstance(i) == 1) fLocalList->Enter(i);
240 iAccepted += fLocalList->GetN();
242 if(path != "") fESDchain->AddFile(path);
243 else if(turl != "") fESDchain->AddFile(turl);
244 fGlobalList->Add(fLocalList);
245 iAccepted += fLocalList->GetN();
250 AliInfo(Form("Accepted events: %d",iAccepted));
251 fESDchain->SetEntryList(fGlobalList,"ne");
256 //___________________________________________________________________________
257 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, AliRunTagCuts *runTagCuts, AliLHCTagCuts *lhcTagCuts, AliDetectorTagCuts *detTagCuts, AliEventTagCuts *evTagCuts) {
258 //Queries the tag chain using the defined
259 //event tag cuts from the AliEventTagCuts object
260 //and returns a XML collection
261 AliInfo(Form("Creating the collection........"));
263 AliXMLCollection *collection = new AliXMLCollection();
264 collection->SetCollectionName(name);
265 collection->WriteHeader();
271 //Defining tag objects
272 AliRunTag *tag = new AliRunTag;
273 AliEventTag *evTag = new AliEventTag;
274 fChain->SetBranchAddress("AliTAG",&tag);
276 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
278 TEntryList *fList = new TEntryList();
279 fChain->GetEntry(iTagFiles);
280 if(runTagCuts->IsAccepted(tag)) {
281 if(lhcTagCuts->IsAccepted(tag->GetLHCTag())) {
282 if(detTagCuts->IsAccepted(tag->GetDetectorTags())) {
283 Int_t iEvents = tag->GetNEvents();
284 const TClonesArray *tagList = tag->GetEventTags();
285 for(Int_t i = 0; i < iEvents; i++) {
286 evTag = (AliEventTag *) tagList->At(i);
287 guid = evTag->GetGUID();
288 turl = evTag->GetTURL();
289 lfn = turl(8,turl.Length());
290 if(evTagCuts->IsAccepted(evTag)) fList->Enter(i);
292 collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
297 collection->Export();
302 //___________________________________________________________________________
303 Bool_t AliTagAnalysis::CreateXMLCollection(const char* name, const char *fRunCut, const char *fLHCCut, const char *fDetectorCut, const char *fEventCut) {
304 //Queries the tag chain using the defined
305 //event tag cuts from the AliEventTagCuts object
306 //and returns a XML collection
307 AliInfo(Form("Creating the collection........"));
309 AliXMLCollection *collection = new AliXMLCollection();
310 collection->SetCollectionName(name);
311 collection->WriteHeader();
317 //Defining tag objects
318 AliRunTag *tag = new AliRunTag;
319 AliEventTag *evTag = new AliEventTag;
320 fChain->SetBranchAddress("AliTAG",&tag);
322 TTreeFormula *fRunFormula = new TTreeFormula("fRun",fRunCut,fChain);
323 TTreeFormula *fLHCFormula = new TTreeFormula("fLHC",fLHCCut,fChain);
324 TTreeFormula *fDetectorFormula = new TTreeFormula("fDetector",fDetectorCut,fChain);
325 TTreeFormula *fEventFormula = new TTreeFormula("fEvent",fEventCut,fChain);
328 for(Int_t iTagFiles = 0; iTagFiles < fChain->GetEntries(); iTagFiles++) {
330 TEntryList *fList = new TEntryList();
331 fChain->GetEntry(iTagFiles);
332 if (current != fChain->GetTreeNumber()) {
333 fRunFormula->UpdateFormulaLeaves();
334 fLHCFormula->UpdateFormulaLeaves();
335 fDetectorFormula->UpdateFormulaLeaves();
336 fEventFormula->UpdateFormulaLeaves();
337 current = fChain->GetTreeNumber();
339 if(fRunFormula->EvalInstance(iTagFiles) == 1) {
340 if(fLHCFormula->EvalInstance(iTagFiles) == 1) {
341 if(fDetectorFormula->EvalInstance(iTagFiles) == 1) {
342 Int_t iEvents = fEventFormula->GetNdata();
343 const TClonesArray *tagList = tag->GetEventTags();
344 for(Int_t i = 0; i < iEvents; i++) {
345 evTag = (AliEventTag *) tagList->At(i);
346 guid = evTag->GetGUID();
347 turl = evTag->GetTURL();
348 lfn = turl(8,turl.Length());
349 if(fEventFormula->EvalInstance(i) == 1) fList->Enter(i);
351 collection->WriteBody(iTagFiles+1,guid,lfn,turl,fList);
356 collection->Export();
361 //___________________________________________________________________________
362 TChain *AliTagAnalysis::GetInputChain(const char* system, const char *wn) {
363 //returns the chain+event list - used in batch sessions
364 // this function will be removed once the new root
365 // improvements are committed
366 TString fsystem = system;
369 TChain *fESDchain = new TChain("esdTree");
371 TEventList *fEventList = new TEventList();
372 AliXMLCollection *collection = AliXMLCollection::Open(wn);
375 while (collection->Next()) {
376 AliInfo(Form("Adding: %s",collection->GetTURL("")));
377 fESDchain->Add(collection->GetTURL(""));
378 TEntryList *list = (TEntryList *)collection->GetEventList("");
379 for(Int_t i = 0; i < list->GetN(); i++) fEventList->Enter(iAccepted+list->GetEntry(i));
381 if(fsystem == "pp") iAccepted += 100;
382 else if(fsystem == "PbPb") iAccepted += 1;
385 fESDchain->SetEventList(fEventList);
387 AliInfo(Form("Number of selected events: %d",fEventList->GetN()));
392 //___________________________________________________________________________
393 TChain *AliTagAnalysis::GetChainFromCollection(const char* collectionname, const char* treename) {
394 //returns the TChain+TEntryList object- used in batch sessions
395 TString fAliceFile = treename;
397 TChain *fAnalysisChain = new TChain();
398 if(fAliceFile == "esdTree") {
400 fAnalysisChain->SetName("esdTree");
401 } else if(fAliceFile == "aodTree") {
403 fAnalysisChain->SetName("aodTree");
404 AliFatal("AOD case not yet implemented!!!");
406 else AliFatal("Inconsistent tree name - use esdTree or aodTree!");
408 TEntryList *fGlobalList = new TEntryList();
409 AliXMLCollection *collection = AliXMLCollection::Open(collectionname);
412 while (collection->Next()) {
413 AliInfo(Form("Adding: %s",collection->GetTURL("")));
414 fAnalysisChain->Add(collection->GetTURL(""));
415 TEntryList *list = (TEntryList *)collection->GetEventList("");
416 list->SetTreeName(fAliceFile.Data());
417 list->SetFileName(collection->GetTURL(""));
418 fGlobalList->Add(list);
419 iAccepted += list->GetN();
422 fAnalysisChain->SetEntryList(fGlobalList,"ne");
424 AliInfo(Form("Number of selected events: %d",iAccepted));
426 return fAnalysisChain;