]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderChain.cxx
Coverty fixes (M.Nicassio)
[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 #include <TGrid.h>
37 #include <TGridResult.h>
38
39 #include "AliRawReaderChain.h"
40 #include "AliRawVEvent.h"
41
42 ClassImp(AliRawReaderChain)
43
44 AliRawReaderChain::AliRawReaderChain() :
45   AliRawReaderRoot(),
46   fChain(NULL)
47 {
48   // default constructor
49 }
50
51 AliRawReaderChain::AliRawReaderChain(const char* fileName) :
52   AliRawReaderRoot(),
53   fChain(NULL)
54 {
55 // create raw-reader objects which takes as an input a root chain
56 // either from the file list found in 'fileName' (IsCollection = true)
57 // or from entry list found in 'filename' (IsCollection = false)
58 // The entry-list syntax follows root convetion: filename.root/listname
59
60   fChain = new TChain("RAW");
61
62   TString fileNameStr = fileName;
63   if (fileNameStr.EndsWith(".xml")) {
64
65     TGridCollection *collection = NULL;
66     TPluginManager* pluginManager = gROOT->GetPluginManager();
67     TPluginHandler* pluginHandler = pluginManager->FindHandler("TGridCollection", "alice");
68     if (!pluginHandler) {
69       pluginManager->AddHandler("TGridCollection", "alice", 
70                                 "AliXMLCollection", "ANALYSISalice", "AliXMLCollection(const char*)");
71       pluginHandler = pluginManager->FindHandler("TGridCollection", "alice");
72     }
73     gSystem->Load("libANALYSIS");
74     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
75       collection = (TGridCollection*)pluginHandler->ExecPlugin(1,fileNameStr.Data());
76     }
77     else {
78       fIsValid = kFALSE;
79       return;
80     }
81     collection->Reset();
82     Bool_t elistsExist = kFALSE;
83     TEntryList *elist = new TEntryList();
84     while (collection->Next()) {
85       fChain->Add(collection->GetTURL(""));
86       TEntryList *list = (TEntryList *)collection->GetEntryList("");
87       if (list) {
88         list->SetTreeName("RAW");
89         list->SetFileName(collection->GetTURL(""));
90         elist->Add(list);
91         elistsExist = kTRUE;
92       }
93     }
94     if (elistsExist) {
95       fChain->SetEntryList(elist,"ne");
96     }
97     else {
98       Info("AliRawReaderChain", "no entry lists found in %s. Using all entries", fileNameStr.Data());
99       delete elist;
100     }
101   }
102   else if (fileNameStr.EndsWith(".root")) {
103
104     TDirectory* dir = gDirectory;
105     TFile *listFile = TFile::Open(fileNameStr.Data());
106     dir->cd();
107     if (!listFile || !listFile->IsOpen()) {
108       Error("AliRawReaderChain", "could not open file %s", fileNameStr.Data());
109       fIsValid = kFALSE;
110       return;
111     }
112
113     TEntryList *elist = NULL;
114     TKey *key = NULL;
115     TIter nextkey(listFile->GetListOfKeys());
116     while ((key=(TKey*)nextkey())){
117       if (strcmp("TEntryList", key->GetClassName())==0){
118         elist = (TEntryList*)key->ReadObj();
119       }
120     }
121     if (!elist) {
122       Error("AliRawReaderChain", "no TEntryList found in %s", fileNameStr.Data());
123       fIsValid = kFALSE;
124       return;
125     }
126
127     TEntryList *templist = NULL;
128     TList *elists = elist->GetLists();
129     TIter next(elists);
130     while((templist = (TEntryList*)next())){
131       Info("AliRawReaderChain", "%s added to the chain", templist->GetFileName());
132       fChain->Add(templist->GetFileName());
133     }
134     fChain->SetEntryList(elist,"ne");
135   }
136   else {
137
138     TFileCollection collection("RAW",
139                                "Collection with raw-data files",
140                                fileNameStr.Data());
141
142     if (!fChain->AddFileInfoList((TCollection*)(collection.GetList()))) {
143       Error("AliRawReaderChain","Bad file list in collection, the chain is empty");
144       fIsValid = kFALSE;
145       return;
146     }
147   }
148
149   fChain->SetBranchStatus("*",1);
150   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
151 }
152
153 AliRawReaderChain::AliRawReaderChain(TFileCollection *collection) :
154   AliRawReaderRoot(),
155   fChain(NULL)
156 {
157 // create raw-reader objects which takes as an input a root chain
158 // from a root file collection
159
160   fChain = new TChain("RAW");
161   if (!fChain->AddFileInfoList((TCollection*)(collection->GetList()))) {
162     Error("AliRawReaderChain","Bad file list in collection, the chain is empty");
163     fIsValid = kFALSE;
164     return;
165   }
166
167   fChain->SetBranchStatus("*",1);
168   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
169 }
170
171 AliRawReaderChain::AliRawReaderChain(TChain *chain) :
172   AliRawReaderRoot(),
173   fChain(chain)
174 {
175 // create raw-reader objects which takes as an input a root chain
176 // from a root file collection
177
178   if (!fChain) {
179     fIsValid = kFALSE;
180     return;
181   }
182
183   fChain->SetBranchStatus("*",1);
184   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
185 }
186
187 AliRawReaderChain::AliRawReaderChain(TEntryList *elist) :
188   AliRawReaderRoot(),
189   fChain(NULL)
190 {
191 // create raw-reader objects which takes as an input a root chain
192 // from a root file collection
193
194   if (!elist) {
195     fIsValid = kFALSE;
196     return;
197   }
198
199   fChain = new TChain("RAW");
200
201   TEntryList *templist = NULL;
202   TList *elists = elist->GetLists();
203   TIter next(elists);
204   while((templist = (TEntryList*)next())){
205     fChain->Add(templist->GetFileName());
206   }
207   fChain->SetEntryList(elist,"ne");
208
209   fChain->SetBranchStatus("*",1);
210   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
211 }
212
213 AliRawReaderChain::AliRawReaderChain(Int_t runNumber) :
214   AliRawReaderRoot(),
215   fChain(NULL)
216 {
217 // create raw-reader objects which takes as an input a root chain
218 // with the raw-data files for a given run
219 // It queries alien FC in order to do that and therefore
220 // it needs alien API to be enabled
221
222   if (runNumber <= 0) {
223     Error("AliRawReaderChain","Bad run number:%d",runNumber);
224     fIsValid = kFALSE;
225   }
226
227   if (!gGrid) TGrid::Connect("alien://");
228   if (!gGrid) {
229     fIsValid = kFALSE;
230     return;
231   }
232
233   TGridResult *res = gGrid->Query("/alice/data",Form("%09d/raw/*%09d*0.root",runNumber,runNumber));
234   Int_t nFiles = res->GetEntries();
235   if (!nFiles) {
236     Error("AliRawReaderChain","No raw-data files found for run %d",runNumber);
237     fIsValid = kFALSE;
238     delete res;
239     return;
240   }
241
242   fChain = new TChain("RAW");
243   for (Int_t i = 0; i < nFiles; i++) {
244     TString filename = res->GetKey(i, "turl");
245     if(filename == "") continue;
246     fChain->Add(filename.Data());
247   }
248   delete res;
249
250   fChain->SetBranchStatus("*",1);
251   fChain->SetBranchAddress("rawevent",&fEvent,&fBranch);
252 }
253
254
255 AliRawReaderChain::AliRawReaderChain(const AliRawReaderChain& rawReader) :
256   AliRawReaderRoot(rawReader),
257   fChain(rawReader.fChain)
258 {
259 // copy constructor
260 }
261
262 AliRawReaderChain& AliRawReaderChain::operator = (const AliRawReaderChain& 
263                                                   rawReader)
264 {
265 // assignment operator
266
267   this->~AliRawReaderChain();
268   new(this) AliRawReaderChain(rawReader);
269   return *this;
270 }
271
272 AliRawReaderChain::~AliRawReaderChain()
273 {
274 // delete objects and close root file
275
276   if (fChain) {
277     delete fChain;
278     fChain = NULL;
279   }
280 }
281
282 Bool_t AliRawReaderChain::NextEvent()
283 {
284 // go to the next event in the root file
285
286   if (!fChain || !fChain->GetListOfFiles()->GetEntriesFast()) return kFALSE;
287
288   do {
289     delete fEvent;
290     fEvent = NULL;
291     fEventHeader = NULL;
292     Long64_t treeEntry = fChain->LoadTree(fEventIndex+1);
293     if (!fBranch)
294       return kFALSE;
295     if (fBranch->GetEntry(treeEntry) <= 0)
296       return kFALSE;
297     fEventHeader = fEvent->GetHeader();
298     fEventIndex++;
299   } while (!IsEventSelected());
300   fEventNumber++;
301   return Reset();
302 }
303
304 Bool_t AliRawReaderChain::RewindEvents()
305 {
306 // go back to the beginning of the root file
307
308   fEventIndex = -1;
309   delete fEvent;
310   fEvent = NULL;
311   fEventHeader = NULL;
312   fEventNumber = -1;
313   return Reset();
314 }
315
316 Bool_t  AliRawReaderChain::GotoEvent(Int_t event)
317 {
318   // go to a particular event
319   // Uses the absolute event index inside the
320   // chain with raw data
321
322   if (!fChain || !fChain->GetListOfFiles()->GetEntriesFast()) return kFALSE;
323
324   delete fEvent;
325   fEvent = NULL;
326   fEventHeader = NULL;
327   Long64_t treeEntry = fChain->LoadTree(event);
328    if (!fBranch)
329     return kFALSE;
330   if (fBranch->GetEntry(treeEntry) <= 0)
331     return kFALSE;
332   fEventHeader = fEvent->GetHeader();
333   fEventIndex = event;
334   fEventNumber++;
335   return Reset();
336 }
337
338 Int_t AliRawReaderChain::GetNumberOfEvents() const
339 {
340   // Get the total number of events in the chain
341   // of raw-data files
342
343   if (!fChain) return -1;
344
345   return fChain->GetEntries();
346 }