]>
Commit | Line | Data |
---|---|---|
f553869e | 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> | |
4d172770 | 25 | #include <TSystemFile.h> |
f553869e | 26 | #include <TFile.h> |
27 | #include <TList.h> | |
28 | #include <TLeaf.h> | |
29 | #include <TMath.h> | |
30 | #include <TNtuple.h> | |
afc49a4b | 31 | #include <TString.h> |
f553869e | 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), | |
f553869e | 43 | fEvent(NULL) |
44 | { | |
45 | //ctor | |
46 | } | |
47 | //______________________________________________________________________________ | |
48 | AliStarEventReader::AliStarEventReader( const char* inputFileDirectory ): | |
49 | TObject(), | |
50 | fFileList(NULL), | |
f553869e | 51 | fEvent(new AliStarEvent(1024)) |
52 | { | |
53 | //ctor | |
54 | MakeFileList ( inputFileDirectory ) ; | |
55 | } | |
56 | ||
57 | //______________________________________________________________________________ | |
58 | AliStarEventReader::~AliStarEventReader() | |
59 | { | |
60 | //dtor | |
f553869e | 61 | delete fFileList; |
f553869e | 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 ; | |
cc0afcfc | 71 | static Int_t doOnce = 0 ; |
72 | static Long64_t nextEntry = 0 ; | |
f553869e | 73 | static Long64_t entries = 0 ; |
cc0afcfc | 74 | static Long64_t fileCounter = 0 ; |
f553869e | 75 | |
cc0afcfc | 76 | if ( doOnce == 0 ) |
f553869e | 77 | { |
cc0afcfc | 78 | doOnce = 1 ; |
f553869e | 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 | } | |
cc0afcfc | 89 | fileCounter++ ; |
90 | cout << "Start New File " << fileCounter << endl ; | |
f553869e | 91 | } |
92 | ||
93 | while ( nextFile ) | |
94 | { | |
cc0afcfc | 95 | while ( nextEntry < entries ) |
f553869e | 96 | { |
c855d803 | 97 | Float_t* header = NULL; |
f553869e | 98 | Int_t numberOfParticles = 0 ; // Number of particle tracks in the next event |
cc0afcfc | 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 | |
f553869e | 101 | |
f553869e | 102 | fEvent->Reset(); //reset the event |
f553869e | 103 | |
104 | // Search for the first "Event" record | |
105 | ||
cc0afcfc | 106 | for ( Long64_t j = nextEntry ; j < entries ; j++ ) |
f553869e | 107 | { |
108 | Long64_t BytesRead = ntData->GetEntry(j) ; | |
109 | if ( BytesRead < 60 ) | |
110 | { | |
111 | cout << "Warning: error in file or EOF " << endl ; | |
cc0afcfc | 112 | headerEntry = -1 ; |
f553869e | 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 | { | |
f553869e | 119 | fEvent->SetParams(header); //set the event params |
f553869e | 120 | |
121 | numberOfParticles = (int) header[9] ; // # of particles passing track cuts, thus in ntuple | |
cc0afcfc | 122 | headerEntry = j ; |
f553869e | 123 | break ; |
124 | } | |
125 | cout << "Warning: no header entries found in this file" << endl ; | |
cc0afcfc | 126 | headerEntry = -1 ; |
f553869e | 127 | } |
128 | ||
cc0afcfc | 129 | if ( headerEntry == -1 ) break ; // Break out of main loop if I/O error |
f553869e | 130 | |
131 | // Get subsequent "track" data | |
cc0afcfc | 132 | for ( Long64_t j = headerEntry + 1 ; j < headerEntry + 1 + numberOfParticles ; j++ ) |
f553869e | 133 | { |
134 | Long64_t BytesRead = ntData->GetEntry(j) ; | |
135 | if ( BytesRead < 60 ) | |
136 | { | |
137 | cout << "Warning: error in file sequence or EOF" << endl ; | |
cc0afcfc | 138 | nextEntry = -1 ; |
f553869e | 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 ; | |
cc0afcfc | 161 | skipEvent = 1 ; |
162 | nextEntry = j ; // Skip event and freeze nextEntry counter | |
f553869e | 163 | break ; |
164 | } | |
165 | ||
f553869e | 166 | fEvent->AddTrack( new AliStarTrack(header) ); //add the new track |
f553869e | 167 | |
cc0afcfc | 168 | nextEntry = j+1 ; |
f553869e | 169 | } |
cc0afcfc | 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 | |
f553869e | 172 | return true ; // Success: Event read OK, note unusual location for a successful return |
173 | } | |
174 | ||
cc0afcfc | 175 | nextEntry = 0 ; // this entry goes before nextFile |
f553869e | 176 | nextFile = (TFile*) fFileList->After(nextFile) ; |
177 | if ( nextFile == 0 ) break ; | |
d3aa0e80 | 178 | if (ntData) delete ntData; |
f553869e | 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 | } | |
cc0afcfc | 187 | fileCounter++ ; |
188 | cout << "Start New File " << fileCounter << endl ; | |
f553869e | 189 | } |
190 | ||
191 | return false ; // Failure: Error or EOF | |
192 | } | |
193 | ||
f553869e | 194 | //______________________________________________________________________________ |
afc49a4b | 195 | Bool_t AliStarEventReader::MakeFileList ( const char* input ) |
196 | { | |
197 | //get the files to process | |
4d172770 | 198 | TString inputstring(input); |
199 | inputstring = inputstring.Strip(TString::kBoth); | |
200 | TSystemFile inputfile(inputstring.Data(),""); | |
201 | if (inputfile.IsDirectory()) | |
202 | return MakeFileListFromDir(inputstring.Data()); | |
afc49a4b | 203 | else |
4d172770 | 204 | return MakeFileListFromFile(inputstring.Data()); |
afc49a4b | 205 | } |
206 | ||
207 | //______________________________________________________________________________ | |
208 | Bool_t AliStarEventReader::MakeFileListFromDir ( const char* inputFileDirectory ) | |
f553869e | 209 | { |
210 | //get the files to process | |
211 | Int_t Count = 0 ; | |
cc0afcfc | 212 | static Int_t doOnce = 0 ; |
f553869e | 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) ) ; | |
cc0afcfc | 234 | if ( doOnce == 0 ) |
f553869e | 235 | { |
236 | cout << "Add: " << fileName << endl ; | |
cc0afcfc | 237 | doOnce = 1 ; |
f553869e | 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 | ||
afc49a4b | 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 |