00001
00002
00003
00004
00006 #include <Stiostream.h>
00007 #include "TClass.h"
00008 #include "TROOT.h"
00009 #include "StMaker.h"
00010 #include "StIOMaker.h"
00011 #include "StTreeMaker/StTreeMaker.h"
00012
00013
00014
00015 enum { kStTREE=1,
00016 kStXDF=2,
00017 kStMDC2=3,
00018 kStDAQ=4,
00019 kStMuDst=5,
00020 kStDAT=6
00021 };
00022 const char IOFMTS[] = "root xdf mdc2 daq mudst dat ";
00023 const char *IOCLAS[] = {0
00024 ,"StTreeMaker"
00025 ,"St_xdfin_Maker"
00026 ,"St_io_Maker"
00027 ,"StDAQMaker"
00028 ,"StMuIOMaker"
00029 ,"StDAQMaker"
00030
00031 };
00032 const char *IONAME[] = {0,
00033 "Root",
00034 "XDF",
00035 "MDC2",
00036 "DAQ",
00037 "MuDst",
00038 "DAT"
00039 };
00040
00041 ClassImp(StIOMaker)
00042
00043
00044 StIOMaker::StIOMaker(const char *name, const char *iomode,
00045 const char *ioFile,const char *treeName)
00046 :StIOInterFace(name,iomode),fFileSet(0),fCurrMk(0)
00047 {
00048 Build(0,ioFile,treeName);
00049 }
00050
00051 StIOMaker::StIOMaker(const char *name, const char *iomode,
00052 StFileI *fileSet,const char *treeName )
00053 :StIOInterFace(name,iomode),fFileSet(0),fCurrMk(0)
00054 {
00055 Build( fileSet,0,treeName);
00056 }
00057
00058 void StIOMaker::Build(StFileI *fileSet,const char *ioFile,const char *treeName)
00059 {
00060 fCurrMk = 0;
00061 fSkip=0; fFileSet=0; fCase = 0;
00062 SetMaxEvent();
00063 memset(fFmtMk,0,sizeof(fFmtMk));
00064 SetTreeName(treeName); fCase=0;
00065
00066 if (fIOMode[0]=='r') {
00067 fFileSet= fileSet;
00068 if (ioFile && ioFile[0]) {
00069 fFileSet = new StFile();
00070 fFileSet->AddFile(ioFile);
00071
00072 }
00073 } else {
00074 fCase = 1;
00075 fNextFile = ioFile;
00076 }
00077 }
00078
00079 StIOMaker::~StIOMaker()
00080 {
00081
00082 if (fFileSet) delete fFileSet; fFileSet= 0;
00083 }
00084
00085 void StIOMaker::Rewind()
00086 {
00087 Close();
00088 if (fFileSet) fFileSet->Rewind();
00089 }
00090
00091
00092 Int_t StIOMaker::Init()
00093 {
00094
00095 return 0;
00096 }
00097
00098 void StIOMaker::SetFile(const char *file)
00099 {
00100 if (!file || !file[0]) return;
00101 StIOInterFace::SetFile(file);
00102 fNextFile = file;
00103 if (fIOMode[0]!='r') return;
00104
00105
00106 if(!fFileSet) {
00107 fFileSet = new StFile();
00108
00109 }
00110 fFileSet->AddFile(file);
00111 }
00112
00113 Int_t StIOMaker::Skip()
00114 {
00115
00116 if (!fSkip) return 0;
00117 if (!fCurrMk) OpenRead();
00118 if (!fCurrMk) return 0;
00119
00120 fSkip = fCurrMk->Skip(fSkip);
00121 if (!fSkip) return 0;
00122 SetNumber((fNumEvent+=fSkip));
00123 Close();
00124
00125 return fSkip;
00126 }
00127
00128
00129 Int_t StIOMaker::Open(const char *)
00130 {
00131 if (fIOMode[0]==0) return 0;
00132 if (fIOMode[0]=='r') { Skip(); return OpenRead();}
00133 return OpenWrite();
00134 }
00135
00136 Int_t StIOMaker::OpenRead()
00137 {
00138
00139 if (fCurrMk) return 0;
00140 fNumEvent = 0;
00141 if (!fFileSet) return kStEOF;
00142
00143 if (fFileSet->GetNextBundle()) return kStEOF;
00144
00145 fNextFile = fFileSet->GetFileName(0);
00146 if (fNextFile.IsNull()) return kStEOF;
00147 if(GetDebug()) (void) printf("<StIOMaker::Open() file %s\n",(const char*)fNextFile);
00148 TString fmt = fFileSet->GetFormat(0);
00149 TString bra = fFileSet->GetCompName(0);
00150
00151 const char *cc = strstr(IOFMTS,(const char*)fmt);
00152 if (!cc) return kStErr;
00153 fCase = (cc-IOFMTS)/6+1;
00154
00155 if (!fFmtMk[fCase-1]) fFmtMk[fCase-1] = Load();
00156 fCurrMk = fFmtMk[fCase-1];
00157
00158 if (!fCurrMk) return kStErr;
00159 fCurrMk->SetBranch(bra,fNextFile.Data(),0);
00160 StIOInterFace::SetFile(fNextFile.Data());
00161 fCurrMk->SetFile(fNextFile.Data());
00162 NotifyEm("OpenFile",fNextFile.Data());
00163 return fCurrMk->Open();
00164 }
00165
00166 Int_t StIOMaker::OpenWrite()
00167 {
00168
00169 if (fCurrMk) return 0;
00170 fNumEvent = 0;
00171 if (fNextFile.IsNull()) return kStEOF;
00172 if(GetDebug()) (void) printf("<StIOMaker::Open() file %s\n",(const char*)fNextFile);
00173 fCase = 1;
00174
00175 if (!fFmtMk[fCase-1]) fFmtMk[fCase-1] = Load();
00176 fCurrMk = fFmtMk[fCase-1];
00177 if (!fCurrMk) return kStErr;
00178 fCurrMk->Close();
00179 fCurrMk->SetFile(fNextFile.Data());
00180 return fCurrMk->Open();
00181 }
00182
00183 Int_t StIOMaker::Make(){
00184 int iret;
00185 fNIO++;
00186 if (fIOMode[0]=='r') {
00187 AGAIN:
00188 iret = MakeRead();
00189 SetNumber(++fNumEvent);
00190
00191 LOG_QA << "StIOMaker: Event: " << GetIventNumber()
00192 << " Run: " << GetRunNumber()
00193 << " EventId: " << GetEventNumber() << endm;
00194
00195
00196 if (fNumEvent > fMaxEvent) iret = kStEOF;
00197 if (iret != kStEOF) return iret;
00198 Close();
00199 iret = Open(); if (iret) return iret;
00200 goto AGAIN;
00201
00202 } else {
00203 return MakeWrite();
00204 }
00205 }
00206
00207 Int_t StIOMaker::MakeRead(){
00208
00209 if (!fCurrMk) Open();
00210 if (!fCurrMk) return kStEOF;
00211 StUKey uk = fFileSet->GetNextEvent();
00212 if (uk.EOK()) return kStEOF;
00213
00214 return MakeRead(uk);
00215 }
00216
00217 Int_t StIOMaker::MakeRead(const StUKey &uk){
00218
00219 if (!fCurrMk) Open();
00220 if (!fCurrMk) return kStEOF;
00221 return fCurrMk->MakeRead(uk);
00222 }
00223
00224 Int_t StIOMaker::MakeWrite()
00225 {
00226 if (!fCurrMk) Open();
00227 if (!fCurrMk) return kStEOF;
00228 return fCurrMk->MakeWrite();
00229 }
00230
00231 Int_t StIOMaker::Finish()
00232 {
00233 int n = sizeof(fFmtMk)/sizeof(fFmtMk[0]);
00234 for(int i=0;i<n;i++) {
00235 if (!fFmtMk[i]) continue;
00236 fFmtMk[i]->Finish();
00237 fFmtMk[i]=0;}
00238 fCurrMk = 0;
00239 StIOInterFace::Finish();
00240 delete fFileSet; fFileSet=0;
00241 return 0;
00242 }
00243
00244 void StIOMaker::Close(Option_t *)
00245 {
00246 if (!fCurrMk) return;
00247 fCurrMk->Close();
00248 NotifyEm("CloseFile",fFile.Data());
00249 fCurrMk = 0;
00250 }
00251
00252 void StIOMaker::Clear(Option_t *opt)
00253 {
00254 if(!fCurrMk) return;
00255 fCurrMk->Clear();
00256 }
00257
00258 void StIOMaker::NotifyMe(const char *about,const void *info)
00259 {
00260 if (strcmp("CloseFile",about)==0) { Close(); return;}
00261 if (strcmp("OpenFile" ,about)==0) { fNextFile =((char*)info);return;}
00262 }
00263
00264
00265 StIOInterFace *StIOMaker::Load()
00266 {
00267 StIOInterFace *Mk;
00268 TString ts;
00269 TClass *klass;
00270
00271 const char *className = IOCLAS[fCase];
00272 (void) printf("<StIOMaker::Load() trying to GetClass(%s) case %d\n",className,fCase);
00273 klass = gROOT->GetClass(className);
00274
00275 if (! klass || klass->Size()==0) {
00276 Int_t Loaded=0;
00277
00278 if (fCase==kStDAQ || fCase==kStDAT)
00279 {
00280 gSystem->Load("RTS");
00281 gSystem->Load("StDaqLib");
00282 }
00283 if (fCase==kStDAT) {
00284 gSystem->Load("StTrgDatFileReader");
00285 }
00286 if (fCase==kStMuDst){
00287 Loaded = 1;
00288 gSystem->Load("St_Tables");
00289 gSystem->Load("StEmcUtil");
00290 gSystem->Load("StStrangeMuDstMaker");
00291 gSystem->Load("StMuDSTMaker");
00292 }
00293 if (! Loaded) gSystem->Load(className);
00294 klass = gROOT->GetClass(className);
00295 }
00296 if ( ! klass ){
00297 (void) printf("<StIOMaker::Load() className %s was not loaded\n",
00298 className);
00299 assert (klass);
00300 }
00301
00302 ts = GetName(); ts +="_"; ts += IONAME[fCase];
00303 Mk = (StIOInterFace*)GetMaker(ts.Data());
00304 if (Mk){
00305 assert(Mk->InheritsFrom(StIOInterFace::Class()));
00306 } else {
00307 Mk = (StIOInterFace*)New(className,ts.Data()); assert(Mk);
00308 }
00309 Mk->SetAttr(this);
00310 AddMaker(Mk);
00311 Mk->SetIOMode(fIOMode);
00312 Mk->SetTreeName(fTreeName);
00313 Mk->SetFile(fNextFile);
00314 Mk->SetDebug(GetDebug());
00315 TDataSet *brs = Find(".branches");
00316 if (brs) brs->Shunt(Mk);
00317 int iret = Mk->Init();
00318 return (iret) ? 0 : Mk;
00319 }
00320
00321