StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StIOMaker.cxx
1 // //
3 // StIOMaker class for Makers //
4 // //
6 #include <Stiostream.h>
7 #include "TClass.h"
8 #include "TROOT.h"
9 #include "StMaker.h"
10 #include "StIOMaker.h"
11 #include "StTreeMaker/StTreeMaker.h"
12 //#include "St_io_Maker/St_io_Maker.h"
13 //#include "St_xdfin_Maker/St_xdfin_Maker.h"
14 
15 enum { kStTREE=1,
16  kStXDF=2,
17  kStMDC2=3,
18  kStDAQ=4,
19  kStMuDst=5,
20  kStDAT=6
21 };
22 const char IOFMTS[] = "root xdf mdc2 daq mudst dat ";
23 const char *IOCLAS[] = {0
24  ,"StTreeMaker"
25  ,"St_xdfin_Maker"
26  ,"St_io_Maker"
27  ,"StDAQMaker"
28  ,"StMuIOMaker"
29  ,"StDAQMaker"
30 // ,"StTrgDatFileReader"
31 };
32 const char *IONAME[] = {0,
33  "Root",
34  "XDF",
35  "MDC2",
36  "DAQ",
37  "MuDst",
38  "DAT"
39 };
40 
41 ClassImp(StIOMaker)
42 
43 //_____________________________________________________________________________
44 StIOMaker::StIOMaker(const char *name, const char *iomode,
45  const char *ioFile,const char *treeName)
46 :StIOInterFace(name,iomode),fFileSet(0),fCurrMk(0)
47 {
48  Build(0,ioFile,treeName);
49 }
50 //_____________________________________________________________________________
51 StIOMaker::StIOMaker(const char *name, const char *iomode,
52  StFileI *fileSet,const char *treeName )
53 :StIOInterFace(name,iomode),fFileSet(0),fCurrMk(0)
54 {
55  Build( fileSet,0,treeName);
56 }
57 //_____________________________________________________________________________
58 void StIOMaker::Build(StFileI *fileSet,const char *ioFile,const char *treeName)
59 {
60  fCurrMk = 0;
61  fSkip=0; fFileSet=0; fCase = 0;
62  SetMaxEvent();
63  memset(fFmtMk,0,sizeof(fFmtMk));
64  SetTreeName(treeName); fCase=0;
65 
66  if (fIOMode[0]=='r') { //ReadOnly
67  fFileSet= fileSet;
68  if (ioFile && ioFile[0]) { //Make small StFile
69  fFileSet = new StFile();
70  fFileSet->AddFile(ioFile);
71 //VP AddConst(new TObjectSet("..FileSet",fFileSet,1)); //To be deleted at the end
72  }
73  } else { //Write/Update
74  fCase = 1;
75  fNextFile = ioFile;
76  }
77 }
78 //_____________________________________________________________________________
79 StIOMaker::~StIOMaker()
80 {
81  // pointer may belong to someone else - do not delete
82  if (fFileSet) delete fFileSet; fFileSet= 0;
83 }
84 //_____________________________________________________________________________
85 void StIOMaker::Rewind()
86 {
87  Close();
88  if (fFileSet) fFileSet->Rewind();
89 }
90 
91 //_____________________________________________________________________________
92 Int_t StIOMaker::Init()
93 {
94 //VP return Open();
95  return 0;
96 }
97 //_____________________________________________________________________________
98  void StIOMaker::SetFile(const char *file)
99 {
100  if (!file || !file[0]) return;
101  StIOInterFace::SetFile(file);
102  fNextFile = file;
103  if (fIOMode[0]!='r') return;
104 
105 // Add file to StFile
106  if(!fFileSet) {
107  fFileSet = new StFile();
108 // AddConst(new TObjectSet("..FileSet",fFileSet,1)); //To be deleted at the end
109  }
110  fFileSet->AddFile(file);
111 }
112 //_____________________________________________________________________________
113 Int_t StIOMaker::Skip()
114 {
115 
116  if (!fSkip) return 0;
117  if (!fCurrMk) OpenRead();
118  if (!fCurrMk) return 0;
119 
120  fSkip = fCurrMk->Skip(fSkip);
121  if (!fSkip) return 0;
122  SetNumber((fNumEvent+=fSkip));
123  Close();
124 
125  return fSkip;
126 }
127 
128 //_____________________________________________________________________________
129 Int_t StIOMaker::Open(const char *)
130 {
131  if (fIOMode[0]==0) return 0;
132  if (fIOMode[0]=='r') { Skip(); return OpenRead();}
133  return OpenWrite();
134 }
135 //_____________________________________________________________________________
136 Int_t StIOMaker::OpenRead()
137 {
138 
139  if (fCurrMk) return 0;
140  fNumEvent = 0;
141  if (!fFileSet) return kStEOF;
142 
143  if (fFileSet->GetNextBundle()) return kStEOF;
144 
145  fNextFile = fFileSet->GetFileName(0);
146  if (fNextFile.IsNull()) return kStEOF;
147  if(GetDebug()) (void) printf("<StIOMaker::Open() file %s\n",(const char*)fNextFile);
148  TString fmt = fFileSet->GetFormat(0);
149  TString bra = fFileSet->GetCompName(0);
150 
151  const char *cc = strstr(IOFMTS,(const char*)fmt);
152  if (!cc) return kStErr;
153  fCase = (cc-IOFMTS)/6+1;
154 
155  if (!fFmtMk[fCase-1]) fFmtMk[fCase-1] = Load();
156  fCurrMk = fFmtMk[fCase-1];
157 
158  if (!fCurrMk) return kStErr;
159  fCurrMk->SetBranch(bra,fNextFile.Data(),0);
160  StIOInterFace::SetFile(fNextFile.Data());
161  fCurrMk->SetFile(fNextFile.Data());
162  NotifyEm("OpenFile",fNextFile.Data());
163  return fCurrMk->Open();
164 }
165 //_____________________________________________________________________________
166 Int_t StIOMaker::OpenWrite()
167 {
168 
169  if (fCurrMk) return 0;
170  fNumEvent = 0;
171  if (fNextFile.IsNull()) return kStEOF;
172  if(GetDebug()) (void) printf("<StIOMaker::Open() file %s\n",(const char*)fNextFile);
173  fCase = 1;
174 
175  if (!fFmtMk[fCase-1]) fFmtMk[fCase-1] = Load();
176  fCurrMk = fFmtMk[fCase-1];
177  if (!fCurrMk) return kStErr;
178  fCurrMk->Close();
179  fCurrMk->SetFile(fNextFile.Data());
180  return fCurrMk->Open();
181 }
182 //_____________________________________________________________________________
184  int iret;
185  fNIO++;
186  if (fIOMode[0]=='r') { //Read mode
187 AGAIN:
188  iret = MakeRead();
189  SetNumber(++fNumEvent);
190 
191  LOG_QA << "StIOMaker: Event: " << GetIventNumber()
192  << " Run: " << GetRunNumber()
193  << " EventId: " << GetEventNumber() << endm;
194 
195 
196  if (fNumEvent > fMaxEvent) iret = kStEOF;
197  if (iret != kStEOF) return iret;
198  Close();
199  iret = Open(); if (iret) return iret;
200  goto AGAIN;
201 
202  } else { //Write mode
203  return MakeWrite();
204  }
205 }
206 //_____________________________________________________________________________
207 Int_t StIOMaker::MakeRead(){
208 
209  if (!fCurrMk) Open();
210  if (!fCurrMk) return kStEOF;
211  StUKey uk = fFileSet->GetNextEvent();
212  if (uk.EOK()) return kStEOF;
213 
214  return MakeRead(uk);
215 }
216 //_____________________________________________________________________________
217 Int_t StIOMaker::MakeRead(const StUKey &uk){
218 
219  if (!fCurrMk) Open();
220  if (!fCurrMk) return kStEOF;
221  return fCurrMk->MakeRead(uk);
222 }
223 //_____________________________________________________________________________
224 Int_t StIOMaker::MakeWrite()
225 {
226  if (!fCurrMk) Open();
227  if (!fCurrMk) return kStEOF;
228  return fCurrMk->MakeWrite();
229 }
230 //_____________________________________________________________________________
232 {
233  int n = sizeof(fFmtMk)/sizeof(fFmtMk[0]);
234  for(int i=0;i<n;i++) {
235  if (!fFmtMk[i]) continue;
236  fFmtMk[i]->Finish();
237  fFmtMk[i]=0;}
238  fCurrMk = 0;
240  delete fFileSet; fFileSet=0;
241  return 0;
242 }
243 //_____________________________________________________________________________
244 void StIOMaker::Close(Option_t *)
245 {
246  if (!fCurrMk) return;
247  fCurrMk->Close();
248  NotifyEm("CloseFile",fFile.Data());
249  fCurrMk = 0;
250 }
251 //_____________________________________________________________________________
252 void StIOMaker::Clear(Option_t *opt)
253 {
254  if(!fCurrMk) return;
255  fCurrMk->Clear();
256 }
257 //_____________________________________________________________________________
258 void StIOMaker::NotifyMe(const char *about,const void *info)
259 {
260  if (strcmp("CloseFile",about)==0) { Close(); return;}
261  if (strcmp("OpenFile" ,about)==0) { fNextFile =((char*)info);return;}
262 }
263 
264 //_____________________________________________________________________________
265 StIOInterFace *StIOMaker::Load()
266 {
267  StIOInterFace *Mk;
268  TString ts;
269  TClass *klass;
270 
271  const char *className = IOCLAS[fCase];
272  (void) printf("<StIOMaker::Load() trying to GetClass(%s) case %d\n",className,fCase);
273  klass = gROOT->GetClass(className);
274 
275  if (! klass || klass->Size()==0) { // lib not loaded
276  Int_t Loaded=0; // library load stack may be self-sufficient, set to 1
277 // if (fCase==kStXDF) gSystem->Load("xdf2root");
278  if (fCase==kStDAQ || fCase==kStDAT)
279  {
280  gSystem->Load("RTS");
281  gSystem->Load("StDaqLib");
282  }
283  if (fCase==kStDAT) {
284  gSystem->Load("StTrgDatFileReader");
285  }
286  if (fCase==kStMuDst){
287  Loaded = 1;
288  gSystem->Load("St_Tables");
289  gSystem->Load("StEmcUtil");
290  gSystem->Load("StStrangeMuDstMaker");
291  gSystem->Load("StMuDSTMaker");
292  }
293  if (! Loaded) gSystem->Load(className);
294  klass = gROOT->GetClass(className);
295  }
296  if ( ! klass ){
297  (void) printf("<StIOMaker::Load() className %s was not loaded\n",
298  className);
299  assert (klass);
300  }
301 
302  ts = GetName(); ts +="_"; ts += IONAME[fCase];
303  Mk = (StIOInterFace*)GetMaker(ts.Data());
304  if (Mk){
305  assert(Mk->InheritsFrom(StIOInterFace::Class()));
306  } else {
307  Mk = (StIOInterFace*)New(className,ts.Data()); assert(Mk);
308  }
309  Mk->SetAttr(this);
310  AddMaker(Mk);
311  Mk->SetIOMode(fIOMode);
312  Mk->SetTreeName(fTreeName);
313  Mk->SetFile(fNextFile);
314  Mk->SetDebug(GetDebug());
315  TDataSet *brs = Find(".branches");
316  if (brs) brs->Shunt(Mk);
317  int iret = Mk->Init();
318  return (iret) ? 0 : Mk;
319 }
320 
321 
Int_t fCase
for debug only
Definition: StIOMaker.h:56
StIOInterFace * fCurrMk
next file from file set
Definition: StIOMaker.h:52
Definition: StTree.h:125
Int_t fNIO
Main file name name.
Definition: StIOInterFace.h:33
TString fTreeName
r=read,w=write,u=update
Definition: StIOInterFace.h:31
virtual void Clear(Option_t *opt)
User defined functions.
Definition: StIOMaker.cxx:252
virtual void SetIOMode(Option_t *iomode="w")
number of transactions
Definition: StIOInterFace.h:35
virtual void Clear(Option_t *option="")
User defined functions.
Definition: StMaker.cxx:634
virtual Int_t Finish()
Definition: StIOMaker.cxx:231
void Build(StFileI *fileSet, const char *ioFile, const char *treeName)
Definition: StIOMaker.cxx:58
Int_t fSkip
case 1=root,2=xdf,3=mdc2,4=daq
Definition: StIOMaker.h:57
virtual Int_t Make()
Definition: StIOMaker.cxx:183
Definition: Stypes.h:43
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
virtual Int_t Finish()
virtual Int_t GetRunNumber() const
Returns the current RunNumber.
Definition: StMaker.cxx:1054
TString fNextFile
Chain of files.
Definition: StIOMaker.h:51
Int_t fNumEvent
for debug only
Definition: StIOMaker.h:55
virtual void Shunt(TDataSet *newParent=0)
Definition: TDataSet.cxx:810
TString fFile
Tree name.
Definition: StIOInterFace.h:32
Definition: StFileI.h:13
virtual Int_t GetIventNumber() const
Returns the current event number.
Definition: StMaker.cxx:1033
Int_t fMaxEvent
Pointers to TreeMaker,xdfin_Maker,St_io_Maker,StDAQMaker.
Definition: StIOMaker.h:54
StIOInterFace * fFmtMk[9]
Pointer to Current Maker.
Definition: StIOMaker.h:53
Definition: Stypes.h:44
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362