source: JEM-EUSO/esaf_cc_at_lal/packages/common/root/src/EReader.cc @ 114

Last change on this file since 114 was 114, checked in by moretto, 11 years ago

actual version of ESAF at CCin2p3

File size: 7.7 KB
Line 
1// $Id: EReader.cc 2922 2011-06-12 14:21:23Z mabl $
2// Author: ale   2005/06/22
3
4/*****************************************************************************
5 * ESAF: Euso Simulation and Analysis Framework                              *
6 *                                                                           *
7 *  Id: EReader                                                           *
8 *  Package: <packagename>                                                   *
9 *  Coordinator: <coordinator>                                               *
10 *                                                                           *
11 *****************************************************************************/
12
13//_____________________________________________________________________________
14//
15// EReader
16//
17// <extensive class description>
18//
19//   Config file parameters
20//   ======================
21//
22//   <parameter name>: <parameter description>
23//   -Valid options: <available options>
24//
25
26#include "EReader.hh"
27#include "EEvent.hh"
28#include <TList.h>
29#include <TMap.h>
30#include <TFile.h>
31#include <TObjString.h>
32#include <TChain.h>
33#include <TSystem.h>
34#include <TRegexp.h>
35
36ClassImp(EReader)
37
38//_____________________________________________________________________________
39EReader::EReader() : fEvTree(0), fEvent(0) {
40    //
41    // Constructor
42    //
43
44    fFiles = new TList;
45    fFiles->SetOwner();
46    fBaseNames = new TList;
47    fBaseNames->SetOwner();
48    fFriendsExt = new TMap;
49    fFriendsExt->SetOwner();
50}
51
52//_____________________________________________________________________________
53EReader::~EReader() {
54    //
55    // Destructor
56    //
57
58    Clear();
59    delete fFiles;
60    delete fBaseNames;
61    delete fFriendsExt;
62}
63
64//______________________________________________________________________________
65void EReader::Clear(Option_t* opt) {
66    //
67    //
68    //
69
70    SafeDelete(fEvent);
71
72    if ( fEvTree && fEvTree->TestBit(kCanDelete) )
73        delete fEvTree;
74           
75
76    fFiles->Delete();
77    fBaseNames->Delete(); 
78    fFriendsExt->Delete();
79}
80
81//______________________________________________________________________________
82TList* EReader::DirFileList(const char *name) {
83    //
84    // Taken from TChain::Add
85    //
86   
87    TList* l = new TList;
88    l->SetOwner();
89
90    // case with one single file
91    if (!TString(name).MaybeWildcard()) {
92        l->Add(new TObjString(name));
93        return l;
94    }
95
96    // wildcarding used in name
97    TString basename(name);
98
99    Int_t slashpos = basename.Last('/');
100    TString directory;
101    if (slashpos>=0) {
102        directory = basename(0,slashpos); // Copy the directory name
103        basename.Remove(0,slashpos+1);      // and remove it from basename
104    } else {
105        directory = gSystem->WorkingDirectory();
106    }
107
108    const char *file;
109    gSystem->ExpandPathName(directory);
110    void *dir = gSystem->OpenDirectory(directory);
111
112    if (dir) {
113        //create a TList to store the file names (not yet sorted)
114        TRegexp re(basename,kTRUE);
115        while ((file = gSystem->GetDirEntry(dir))) {
116            if (!strcmp(file,".") || !strcmp(file,"..")) continue;
117            TString s = file;
118            if ( (basename!=file) && s.Index(re) == kNPOS) continue;
119            l->Add(new TObjString(directory+"/"+file));
120        }
121        gSystem->FreeDirectory(dir);
122        //sort the files in alphanumeric order
123        l->Sort();
124    }
125    return l;
126}
127
128//______________________________________________________________________________
129Int_t EReader::Open( const char* files ) {
130    //
131    // Open files and creates the chain. If possible creates also the img chain
132    // and links it to the tree
133    //
134   
135    TList* l = DirFileList(files);
136
137    TString ext = ".root";
138
139    TIter next(l);
140    // parse the file list
141    while(TObjString* str = (TObjString*)next()) {
142
143        TString s = str->GetString();
144        gSystem->ExpandPathName(s);
145       
146        // skip all not std rootfiles
147        if ( !s.EndsWith(ext) ) continue;
148       
149        TString basename = s(0,s.Sizeof()-ext.Sizeof());
150       
151        TIter nextExt(fFriendsExt);
152        Bool_t skip = kFALSE;
153        while(TObjString* extName = (TObjString*)nextExt()) {
154            TString dummy = "_"+extName->GetString();
155            if ( basename.EndsWith(dummy) ) {
156                skip = kTRUE;
157                break;
158            }
159        }
160           
161        if ( skip ) continue;
162
163        fBaseNames->Add(new TObjString(basename));
164
165    }
166   
167    delete l;
168   
169    TIter nextSimu(fBaseNames);
170
171    if ( fBaseNames->GetEntries() > 1) {
172
173        // build the chain out of the list of collected files
174        TChain* chain = new TChain("etree");
175        chain->SetBit(kCanDelete);
176
177        while( TObjString* str = (TObjString*)nextSimu() )
178            chain->Add(str->GetString()+ext);
179
180        fEvTree = chain;
181    } else if ( fBaseNames->GetEntries() == 1 ) {
182        TObjString* str = (TObjString*)nextSimu();
183        TFile *f = new TFile(str->GetString()+ext);
184        fEvTree = (TTree*)f->Get("etree");
185
186        if ( !fEvTree ) return 0;
187
188        fFiles->Add(f);
189       
190    } else return 0;
191
192
193    fEvent = new EEvent();
194    fEvent->SetBranches(fEvTree);
195   
196    return fBaseNames->GetEntries();
197}
198
199//______________________________________________________________________________
200void EReader::AddExt( const char* ext, const char* tname ) {
201    fFriendsExt->Add(new TObjString(ext), new TObjString(tname));
202}
203
204//______________________________________________________________________________
205Int_t EReader::ConnectFriends() {
206    //
207    // Looks for the friends tree stored in rootfiles whose name is defined by
208    // fFriendsExt. If the number of friend files is equal to the number of
209    // main files, a chain is assembled and connected to the main tree
210    //
211
212    // no base files, stop here
213    if ( !fBaseNames->GetEntries() ) return 0;
214
215    TString ext = ".root";
216   
217    TList fileNames;
218    fileNames.SetOwner();
219
220    TIter nextExt(fFriendsExt);
221    Int_t nFriends(0);
222   
223    // loop on the extensions
224    while(TObjString* strExt = (TObjString*)nextExt()) {
225        TIter next(fBaseNames);
226
227        while(TObjString* str = (TObjString*)next()) {
228            // check existence of the friend files
229
230            TString name = str->GetString()+"_"
231                +strExt->GetString()+ext;
232            if ( gSystem->AccessPathName(name) ) {
233                Info("ConnectFriends","%s not found", (const char*) name);
234                break;
235            }
236
237            fileNames.Add(new TObjString(name));
238
239        }
240
241        if ( fileNames.GetEntries() == fBaseNames->GetEntries() ) {
242            TIter nextFriend(&fileNames);
243            TString tname = ((TObjString*)fFriendsExt->GetValue(strExt))->GetString();
244            if ( fileNames.GetEntries() == 1 ) {
245                // 1 file, create only a tree
246                TObjString* str = (TObjString*)nextFriend();
247                TFile *f = new TFile(str->GetString());
248                TTree *t = (TTree*)f->Get(tname);
249                if ( t ) {
250                    fEvTree->AddFriend(t);
251                    nFriends++;
252                } else 
253                    Printf("File or Tree not found: file %s, tree %s",f->GetName(), tname.Data());
254            } else {
255                // build the chain
256                TChain* chain = new TChain(tname);
257                // CHECK:                    chain->SetBit(kCanDelete);
258
259                while(TObjString* str = (TObjString*)nextFriend()){
260                    chain->Add(str->GetName());
261                }
262
263                chain->GetEntries();
264                fEvTree->AddFriend(chain);
265                nFriends++;
266            }
267        }
268
269    }
270
271    // force the chain (if one) to connect the trees
272    fEvTree->GetEntries();
273    return nFriends;
274}
Note: See TracBrowser for help on using the repository browser.