]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderChain.cxx
Fix for possible use of AliCFManager without containers, just cuts.
[u/mrichter/AliRoot.git] / RAW / AliRawReaderChain.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 ///////////////////////////////////////////////////////////////////////////////
17 ///
18 /// This is a class for reading raw data from a root chain.
19 /// There are two constructors available - one from a text file containing the
20 /// list of root raw-data files to be processed and one directly from
21 /// TFileCollection.
22 ///
23 /// cvetan.cheshkov@cern.ch 29/07/2008
24 ///
25 ///////////////////////////////////////////////////////////////////////////////
26
27 #include <TChain.h>
28 #include <TFileCollection.h>
29 #include <TEntryList.h>
30 #include "TGridCollection.h"
31 #include <TPluginManager.h>
32 #include <TROOT.h>
33 #include <TSystem.h>
34 #include <TFile.h>
35 #include <TKey.h>
36
37 #include "AliRawReaderChain.h"
38 #include "AliRawEvent.h"
39
40 ClassImp(AliRawReaderChain)
41
42 AliRawReaderChain::AliRawReaderChain() :
43   AliRawReaderRoot(),
44   fChain(NULL)
45 {
46   // default constructor
47 }
48
49 AliRawReaderChain::AliRawReaderChain(const char* fileName) :
50   AliRawReaderRoot(),
51   fChain(NULL)
52 {
53 // create raw-reader objects which takes as an input a root chain
54 // either from the file list found in 'fileName' (IsCollection = true)
55 // or from entry list found in 'filename' (IsCollection = false)
56 // The entry-list syntax follows root convetion: filename.root/listname
57
58   fChain = new TChain("RAW");
59
60   TString fileNameStr = fileName;
61   if (fileNameStr.EndsWith(".xml")) {
62
63     TGridCollection *collection = NULL;
64     TPluginManager* pluginManager = gROOT->GetPluginManager();
65     TPluginHandler* pluginHandler = pluginManager->FindHandler("TGridCollection", "alice");
66     if (!pluginHandler) {
67       pluginManager->AddHandler("TGridCollection", "alice", 
68                                 "AliXMLCollection", "ANALYSISalice", "AliXMLCollection(const char*)");
69       pluginHandler = pluginManager->FindHandler("TGridCollection", "alice");
70     }
71     gSystem->Load("libANALYSIS");
72     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
73       collection = (TGridCollection*)pluginHandler->ExecPlugin(1,fileNameStr.Data());
74     }
75     else {
76       fIsValid = kFALSE;
77       return;
78     }
79     collection->Reset();
80     Bool_t elistsExist = kFALSE;
81     TEntryList *elist = new TEntryList();
82     while (collection->Next()) {
83       fChain->Add(collection->GetTURL(""));
84       TEntryList *list = (TEntryList *)collection->GetEntryList("");
85       if (list) {
86         list->SetTreeName("RAW");
87         list->SetFileName(collection->GetTURL(""));
88         elist->Add(list);
89         elistsExist = kTRUE;
90       }
91     }
92     if (elistsExist) {
93       fChain->SetEntryList(elist,"ne");
94     }
95     else {
96       Info("AliRawReaderChain", "no entry lists found in %s. Using all entries", fileNameStr.Data());
97       delete elist;
98     }
99   }
100   else if (fileNameStr.EndsWith(".root")) {
101
102     TDirectory* dir = gDirectory;
103     TFile *listFile = TFile::Open(fileNameStr.Data());
104     dir->cd();
105     if (!listFile || !listFile->IsOpen()) {
106       Error("AliRawReaderChain", "could not open file %s", fileNameStr.Data());
107       fIsValid = kFALSE;
108       return;
109     }
110
111     TEntryList *elist = NULL;
112     TKey *key = NULL;
113     TIter nextkey(listFile->GetListOfKeys());
114     while ((key=(TKey*)nextkey())){
115       if (strcmp("TEntryList", key->GetClassName())==0){
116         elist = (TEntryList*)key->ReadObj();
117       }
118     }
119     if (!elist) {
120       Error("AliRawReaderChain", "no TEntryList found in %s", fileNameStr.Data());
121       fIsValid = kFALSE;
122       return;
123     }
124
125     TEntryList *templist = NULL;
126     TList *elists = elist->GetLists();
127     TIter next(elists);
128     while((templist = (TEntryList*)next())){
129       Info("AliRawReaderChain", "%s added to the chain", templist->GetFileName());
130       fChain->Add(templist->GetFileName());
131     }
132     fChain->SetEntryList(elist,"ne");
133   }
134   else {
135
136     TFileCollection collection("RAW",
137                                "Collection with raw-data files",
138                                fileNameStr.Data());
139
140     if (!fChain->AddFileInfoList((TCollection*)(collection.GetList()))) {
141       Error("AliRawReaderChain","Bad file list in collection, the chain is empty");
142       fIsValid = kFALSE;
143       return;
144     }
145   }
146
147   fChain->SetBranchStatus("*",1);
148   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
149 }
150
151 AliRawReaderChain::AliRawReaderChain(TFileCollection *collection) :
152   AliRawReaderRoot(),
153   fChain(NULL)
154 {
155 // create raw-reader objects which takes as an input a root chain
156 // from a root file collection
157
158   fChain = new TChain("RAW");
159   if (!fChain->AddFileInfoList((TCollection*)(collection->GetList()))) {
160     Error("AliRawReaderChain","Bad file list in collection, the chain is empty");
161     fIsValid = kFALSE;
162     return;
163   }
164
165   fChain->SetBranchStatus("*",1);
166   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
167 }
168
169 AliRawReaderChain::AliRawReaderChain(TChain *chain) :
170   AliRawReaderRoot(),
171   fChain(chain)
172 {
173 // create raw-reader objects which takes as an input a root chain
174 // from a root file collection
175
176   if (!fChain) fIsValid = kFALSE;
177
178   fChain->SetBranchStatus("*",1);
179   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
180 }
181
182 AliRawReaderChain::AliRawReaderChain(TEntryList *elist) :
183   AliRawReaderRoot(),
184   fChain(NULL)
185 {
186 // create raw-reader objects which takes as an input a root chain
187 // from a root file collection
188
189   if (!elist) fIsValid = kFALSE;
190
191   fChain = new TChain("RAW");
192
193   TEntryList *templist = NULL;
194   TList *elists = elist->GetLists();
195   TIter next(elists);
196   while((templist = (TEntryList*)next())){
197     fChain->Add(templist->GetFileName());
198   }
199   fChain->SetEntryList(elist,"ne");
200
201   fChain->SetBranchStatus("*",1);
202   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
203 }
204
205 AliRawReaderChain::AliRawReaderChain(const AliRawReaderChain& rawReader) :
206   AliRawReaderRoot(rawReader),
207   fChain(rawReader.fChain)
208 {
209 // copy constructor
210 }
211
212 AliRawReaderChain& AliRawReaderChain::operator = (const AliRawReaderChain& 
213                                                   rawReader)
214 {
215 // assignment operator
216
217   this->~AliRawReaderChain();
218   new(this) AliRawReaderChain(rawReader);
219   return *this;
220 }
221
222 AliRawReaderChain::~AliRawReaderChain()
223 {
224 // delete objects and close root file
225
226   if (fChain) {
227     delete fChain;
228     fChain = NULL;
229   }
230 }
231
232 Bool_t AliRawReaderChain::NextEvent()
233 {
234 // go to the next event in the root file
235
236   if (!fChain || !fChain->GetListOfFiles()->GetEntriesFast()) return kFALSE;
237
238   do {
239     delete fEvent;
240     fEvent = new AliRawEvent;
241     Long64_t treeEntry = fChain->LoadTree(fEventIndex+1);
242     if (!fBranch)
243       return kFALSE;
244     if (fBranch->GetEntry(treeEntry) <= 0)
245       return kFALSE;
246     fEventIndex++;
247   } while (!IsEventSelected());
248   fEventNumber++;
249   return Reset();
250 }
251
252 Bool_t AliRawReaderChain::RewindEvents()
253 {
254 // go back to the beginning of the root file
255
256   fEventIndex = -1;
257   delete fEvent;
258   fEvent = new AliRawEvent;
259   fEventNumber = -1;
260   return Reset();
261 }
262
263 Bool_t  AliRawReaderChain::GotoEvent(Int_t event)
264 {
265   // go to a particular event
266   // Uses the absolute event index inside the
267   // chain with raw data
268
269   if (!fChain || !fChain->GetListOfFiles()->GetEntriesFast()) return kFALSE;
270
271   delete fEvent;
272   fEvent = new AliRawEvent;
273   Long64_t treeEntry = fChain->LoadTree(event);
274    if (!fBranch)
275     return kFALSE;
276   if (fBranch->GetEntry(treeEntry) <= 0)
277     return kFALSE;
278   fEventIndex = event;
279   fEventNumber++;
280   return Reset();
281 }
282
283 Int_t AliRawReaderChain::GetNumberOfEvents() const
284 {
285   // Get the total number of events in the chain
286   // of raw-data files
287
288   if (!fChain) return -1;
289
290   return fChain->GetEntries();
291 }