]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/FLOW/Base/AliStarEventReader.cxx
Initialize the path name to the geometry file in the Init and not in the InitParameters
[u/mrichter/AliRoot.git] / PWG / FLOW / Base / AliStarEventReader.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 // Title:   Class for ccessing data from STAR NTuples
17 //          produces data encapsulated in AliStarEvent and AliStarTrack classes
18 //
19 // Origin:  Jim Thomas,        jhthomas@lbl.gov
20 //          Mikolaj Krzewicki, mikolaj.krzewicki@cern.ch
21
22 #include <Riostream.h>
23
24 #include <TSystem.h>
25 #include <TSystemFile.h>
26 #include <TFile.h>
27 #include <TList.h>
28 #include <TLeaf.h>
29 #include <TMath.h>
30 #include <TNtuple.h>
31 #include <TString.h>
32
33 #include "AliStarEventReader.h"
34 #include "AliStarEvent.h"
35 #include "AliStarTrack.h"
36
37 ClassImp(AliStarEventReader)
38
39 //______________________________________________________________________________
40 AliStarEventReader::AliStarEventReader():
41   TObject(),
42   fFileList(NULL),
43   fEvent(NULL)
44 {
45   //ctor
46 }
47 //______________________________________________________________________________
48 AliStarEventReader::AliStarEventReader( const char* inputFileDirectory ):
49   TObject(),
50   fFileList(NULL),
51   fEvent(new AliStarEvent(1024))
52 {
53   //ctor
54   MakeFileList ( inputFileDirectory ) ;
55 }
56
57 //______________________________________________________________________________
58 AliStarEventReader::~AliStarEventReader()
59 {
60   //dtor
61   delete fFileList;
62   delete fEvent;
63 }
64
65 //______________________________________________________________________________
66 Bool_t AliStarEventReader::GetNextEvent( )
67 {
68   //gets next event
69   static TFile*    nextFile    = NULL ;
70   static TNtuple*  ntData      = NULL ;
71   static Int_t     doOnce      = 0 ;
72   static Long64_t  nextEntry   = 0 ;
73   static Long64_t  entries     = 0 ;
74   static Long64_t  fileCounter = 0 ;
75
76   if ( doOnce == 0 )
77   {
78     doOnce = 1     ;
79     nextFile = (TFile*)  fFileList->First() ;
80     if ( nextFile == 0 ) return false      ;
81     ntData        = (TNtuple*) ( nextFile->Get("NTtracks") ) ;
82     entries       = ntData->GetEntriesFast() ;
83     Int_t columns = ntData->GetNvar() ;
84     if ( columns != 15 )
85     {
86       cout << "Error in reading Ntuple file: no. columns != 15" << endl ;
87       return false ;
88     }
89     fileCounter++ ;
90     cout << "Start New File " << fileCounter << endl ;
91   }
92
93   while ( nextFile )
94   {
95     while ( nextEntry < entries )
96     {
97       Float_t* header = NULL;
98       Int_t numberOfParticles =  0 ;                   // Number of particle tracks in the next event
99       Long64_t headerEntry    =  0 ;                   // Store position of Header and Set Flag in case of EOF or error
100       Long64_t skipEvent      =  0 ;                   // Flag in case of wrong number of tracks in this event
101
102       fEvent->Reset();           //reset the event
103
104       // Search for the first "Event" record
105
106       for ( Long64_t j = nextEntry ; j < entries ; j++ )
107       {
108         Long64_t BytesRead = ntData->GetEntry(j) ;
109         if ( BytesRead < 60 )
110         {
111           cout << "Warning: error in file or EOF " <<  endl ;
112           headerEntry = -1 ;
113           break ;
114         }
115         header = ntData->GetArgs() ;
116         if ( (int) header[10] == -1 && (int) header[11] == -1 && (int) header[12] == -1 &&
117              (int) header[13] == -1 && (int) header[14] == -1 )
118         {
119           fEvent->SetParams(header);  //set the event params
120
121           numberOfParticles = (int) header[9]  ;   // # of particles passing track cuts, thus in ntuple
122           headerEntry = j ;
123           break ;
124         }
125         cout << "Warning: no header entries found in this file" << endl ;
126         headerEntry = -1 ;
127       }
128
129       if ( headerEntry == -1 ) break ;                 // Break out of main loop if I/O error
130
131       // Get subsequent "track" data
132       for ( Long64_t j = headerEntry + 1 ; j < headerEntry + 1 + numberOfParticles  ; j++ )
133       {
134         Long64_t BytesRead = ntData->GetEntry(j) ;
135         if ( BytesRead < 60 )
136         {
137           cout << "Warning: error in file sequence or EOF" << endl ;
138           nextEntry = -1 ;
139           break ;
140         }
141         header = ntData->GetArgs() ;
142
143         if ( TMath::IsNaN(header[10]) == 1 )
144         {
145           cout << "IsNan ... dEdx will be zeroed out" << endl ;
146           header[10] = 0 ;
147           header[11] = 999 ;
148           header[12] = 999 ;
149           header[13] = 999 ;
150           header[14] = 999 ;
151           cout << header[0]  << " " << header[1]  << " " << header[2]  << " " << header[3]  << " "
152                << header[4]  << " " << header[5]  << " " << header[6]  << " " << header[7]  << " "
153                << header[8]  << " " << header[9]  << " " << header[10] << " " << header[11] << " "
154                << header[12] << " " << header[13] << " " << header[14] << endl ;  // JT test
155         }
156
157         if ( (int) header[10] == -1 && (int) header[11] == -1 && (int) header[12] == -1 &&
158              (int) header[13] == -1 && (int) header[14] == -1 )
159         {
160           cout << "Warning: Header in the wrong place, skipping event" << endl ;
161           skipEvent = 1 ;
162           nextEntry = j ;          // Skip event and freeze nextEntry counter
163           break ;
164         }
165
166         fEvent->AddTrack( new AliStarTrack(header) );    //add the new track
167
168         nextEntry = j+1 ;
169       }
170       if ( nextEntry == -1 ) break ;      // Bad record in file, go to next file in fFileList
171       if ( skipEvent ==  1 ) continue ;   // Bad event, go to next event in this file
172       return true ;                       // Success: Event read OK, note unusual location for a successful return
173     }
174
175     nextEntry = 0 ; // this entry goes before nextFile
176     nextFile = (TFile*) fFileList->After(nextFile) ;
177     if ( nextFile == 0 ) break ;
178     if (ntData) delete ntData;
179     ntData        = (TNtuple*) ( nextFile->Get("NTtracks") ) ;
180     entries       = ntData->GetEntriesFast() ;
181     Int_t columns = ntData->GetNvar() ;
182     if ( columns != 15 )
183     {
184       cout << "Error in reading Ntuple file: no. columns != 15" << endl ;
185       break ;
186     }
187     fileCounter++ ;
188     cout << "Start New File " << fileCounter << endl ;
189   }
190
191   return false ;  // Failure: Error or EOF
192 }
193
194 //______________________________________________________________________________
195 Bool_t AliStarEventReader::MakeFileList ( const char* input )
196 {
197   //get the files to process
198   TString inputstring(input);
199   inputstring = inputstring.Strip(TString::kBoth);
200   TSystemFile inputfile(inputstring.Data(),"");
201   if (inputfile.IsDirectory())
202     return MakeFileListFromDir(inputstring.Data());
203   else
204     return MakeFileListFromFile(inputstring.Data());
205 }
206
207 //______________________________________________________________________________
208 Bool_t AliStarEventReader::MakeFileListFromDir ( const char* inputFileDirectory )
209 {
210   //get the files to process
211   Int_t  Count        = 0 ;
212   static Int_t doOnce = 0 ;
213   fFileList =  new TList() ;
214   void*   directory = gSystem->OpenDirectory(inputFileDirectory) ;
215   const char* entry = gSystem->GetDirEntry(directory) ;
216
217   if ( entry == 0 )
218   {
219     cout << endl <<  "Error: \"" << inputFileDirectory << "\" does not exist" << endl << endl ;
220     return false ;
221   }
222   else cout << endl ;
223
224   while(entry != 0)
225   {
226     int len = strlen(entry);
227     if( len >= 5 && strcmp( &entry[len - 5], ".root" ) == 0 )
228     {
229       TString fileName ;
230       fileName = inputFileDirectory ;
231       if( !fileName.EndsWith("/") ) fileName += "/" ;
232       fileName += entry;
233       fFileList->Add ( TFile::Open(fileName) ) ;
234       if ( doOnce == 0 )
235       {
236         cout << "Add: " << fileName << endl ;
237         doOnce = 1 ;
238       }
239       Count ++ ;
240     }
241     entry = gSystem->GetDirEntry(directory) ;
242   }
243
244   cout << "Add: " << Count-1 << " more file(s) from this directory for a total of " << Count << " files." << endl ;
245   cout << "Finished creating file list ... preparing to open first file." << endl << endl ;
246   return true ;
247 }
248
249 //______________________________________________________________________________
250 Bool_t AliStarEventReader::MakeFileListFromFile ( const char* inputFile )
251 {
252   //get the files to process, from a text file, one file per line
253   if (!fFileList) fFileList=new TList();
254   ifstream filein;
255   filein.open(inputFile);
256   if (!filein.good()) 
257   {
258     printf("problem reading the file list \"%s\"\n",inputFile);
259     return kFALSE;
260   }
261   TString line;
262   while (filein.good())
263   {
264     printf("opening file: ");
265     line.ReadLine(filein);
266     if (line.Length() == 0) continue;
267     TFile* file = TFile::Open(line.Data());
268     if (!file) 
269     {
270       printf("problem opening file \"%s\"\n",line.Data());
271       continue;
272     }
273     fFileList->Add(file);
274     printf("%s\n",line.Data());
275   }
276   if (fFileList->GetEntries()>0) return kTRUE;
277   return kFALSE;
278 }
279