#include "hsudummyrec.h"
#include "hevent.h"
#include "hades.h"
#include "hcategory.h"
#include "hiterator.h"
#include "hdatasource.h"
#include "TROOT.h"
#include "TError.h"
using namespace std;
ClassImp(HSUDummyRec)
ClassImp(HSUIteratorObject)
HSUDummyRec::HSUDummyRec(Bool_t bVerbose)
: HReconstructor("SuDummyRec", "Dummy reconstructor")
{
setDefault();
setVerbose(bVerbose);
}
HSUDummyRec::HSUDummyRec(const Char_t *pOutFileName, Bool_t bVerbose)
: HReconstructor("SuDummyRec", "Dummy reconstructor")
{
setDefault();
setVerbose(bVerbose);
setOutFileName(pOutFileName);
}
HSUDummyRec::HSUDummyRec(const Text_t name[],const Text_t title[], Bool_t bVerbose)
: HReconstructor(name, title)
{
setDefault();
setVerbose(bVerbose);
}
HSUDummyRec::HSUDummyRec(const Char_t *pOutFileName,
const Text_t name[],const Text_t title[], Bool_t bVerbose)
: HReconstructor(name, title)
{
setDefault();
setVerbose(bVerbose);
setOutFileName(pOutFileName);
}
HSUDummyRec::~HSUDummyRec(void)
{
if(pOutFile != NULL)
delete pOutFile;
}
void HSUDummyRec::setDefault(void)
{
lToDelete.SetOwner();
bVerbose = kTRUE;
bInitOk = kFALSE;
pOutFile = NULL;
iEvents = 0;
bWriteEvents = kFALSE;
}
HCategory* HSUDummyRec::getCategory(Cat_t cat, Bool_t bRaport)
{
HCategory *pCat;
HEvent *pEvent;
if((gHades == NULL) || ((pEvent = gHades->getCurrentEvent()) == NULL))
{
::Error("HSUDummyRec::getCategory", "Cannot access current event");
return NULL;
}
if((pCat = pEvent->getCategory(cat)) != NULL)
return pCat;
if(bRaport)
::Warning("HSUDummyRec::getCategory", "No category %d", cat);
return NULL;
}
HCategory* HSUDummyRec::getCategory(const Char_t* pName, Bool_t bRaport)
{
Int_t iMax = 1000;
Int_t i;
HCategory *pCat;
HEvent *pEvent;
if(pName == NULL)
{
:: Error("HSUDummyRec::getCategory", "pName == NULL");
return NULL;
}
if((gHades == NULL) || ((pEvent = gHades->getCurrentEvent()) == NULL))
{
::Error("HSUDummyRec::getCategory", "Cannot access current event");
return NULL;
}
for(i = 0; i < iMax; i++)
{
if((pCat = pEvent->getCategory(Cat_t(i))) == NULL)
continue;
if(strcmp(pCat->getClassName(), pName) == 0)
return pCat;
}
if(bRaport)
::Warning("HSUDummyRec::getCategory", "No category %s", pName);
return NULL;
}
HIterator* HSUDummyRec::getIterator(Cat_t cat, Bool_t bDelete)
{
HIterator *pI;
HCategory *pCat;
if((pCat = getCategory(cat, bVerbose)) == NULL)
return NULL;
if((pI = (HIterator *) pCat->MakeIterator()) == NULL)
Error("getIterator", "Cannot create iterator for category %d", cat);
else
{
if(bDelete)
lToDelete.Add(new HSUIteratorObject(pI));
}
return pI;
}
HIterator* HSUDummyRec::getIterator(const Char_t* pName, Bool_t bDelete)
{
HIterator *pI;
HCategory *pCat;
if((pCat = getCategory(pName, bVerbose)) == NULL)
return NULL;
if((pI = (HIterator *) pCat->MakeIterator()) == NULL)
Error("getIterator", "Cannot create iterator for category %s", pName);
else
{
if(bDelete)
lToDelete.Add(new HSUIteratorObject(pI));
}
return pI;
}
void HSUDummyRec::addObjectToWrite(TObject *pObj, Bool_t bDelete)
{
if(pObj == NULL)
return;
lToWrite.Add(pObj);
if(bDelete)
lToDelete.Add(pObj);
}
Bool_t HSUDummyRec::removeObject(TObject *pObj)
{
Bool_t bReturn = kFALSE;
bReturn |= (lToDelete.Remove(pObj) != NULL);
bReturn |= (lToWrite.Remove(pObj) != NULL);
return bReturn;
}
Bool_t HSUDummyRec::removeIterator(HIterator *pI)
{
TIterator *i = lToDelete.MakeIterator();
TObject *p;
i->Reset();
while((p = i->Next()) != NULL)
{
if(p->IsA() != HSUIteratorObject::Class())
continue;
if(((HSUIteratorObject *)p)->get() != pI)
continue;
lToDelete.Remove(p);
delete p;
return kTRUE;
}
return kFALSE;
}
Int_t HSUDummyRec::writeObjects(TFile *pFile)
{
TFile *pTmp = gFile;
Int_t iWritten;
if(pFile == NULL)
pFile = gFile;
if((pFile == NULL) || ( ! pFile->IsOpen()) || ( ! pFile->IsWritable()))
{
Error("writeObjects", "Cannot write to file: %s",
(pFile == NULL) ? "(nil)" : pFile->GetName());
return 0;
}
pFile->cd();
iWritten = lToWrite.Write();
if(pTmp != NULL)
pTmp->cd();
return iWritten;
}
Int_t HSUDummyRec::writeObjects(const Char_t *pFileName, Bool_t bRecreate)
{
if(pFileName == NULL)
return writeObjects((TFile *)NULL);
TFile *pTmp = gFile;
TFile *pFile = (TFile *)gROOT->GetListOfFiles()->FindObject(pFileName);
Bool_t bClose = kTRUE;
Int_t iWritten;
if((pFile != NULL) && (bRecreate))
{
if(pTmp == pFile)
pTmp = NULL;
delete pFile;
pFile = NULL;
bClose = kFALSE;
}
if(pFile == NULL)
pFile = new TFile(pFileName, (bRecreate) ? "RECREATE" : "UPDATE");
iWritten = writeObjects(pFile);
if(bClose)
delete pFile;
if(pTmp != NULL)
pTmp->cd();
return iWritten;
}
void HSUDummyRec::print(void) const
{
TObject::Print();
printf("Verbose: %s\n", (bVerbose) ? "YES" : "NO");
printf("To write:\n");
lToWrite.Print();
printf("\nTo delete:\n");
lToDelete.Print();
}
Bool_t HSUDummyRec::setOutFile(TFile *pFile, Bool_t bCloseOld)
{
if((pOutFile != NULL) && (bCloseOld == kTRUE))
delete pOutFile;
pOutFile = NULL;
if(pFile == NULL)
return kTRUE;
if(( ! pFile->IsOpen()) || ( ! pFile->IsWritable()))
{
Error("setOutFile", "File %s is not writable", pFile->GetName());
return kFALSE;
}
pOutFile = pFile;
return kTRUE;
}
TFile* HSUDummyRec::openOutFile(const Char_t *pName, Bool_t bRecr)
{
if(pOutFile != NULL)
{
delete pOutFile;
pOutFile = NULL;
}
if(pName == NULL)
{
pName = sOutFileName.Data();
if((pName == NULL) || (strlen(pName) < 1))
{
Error("openOutFile", "NULL file name");
return NULL;
}
}
TFile *pFile = (TFile *)gROOT->GetListOfFiles()->FindObject(pName);
Bool_t bCreated = kFALSE;
if((pFile != NULL) && (bRecr))
{
delete pFile;
pFile = NULL;
}
if(pFile == NULL)
{
pFile = new TFile(pName, (bRecr) ? "RECREATE" : "UPDATE");
bCreated = kTRUE;
}
if(( ! pFile->IsOpen()) || ( ! pFile->IsWritable()))
{
Error("openOutFile", "Cannot open %s for writing", pName);
if(bCreated)
delete pFile;
}
else
pOutFile = pFile;
return pOutFile;
}
Bool_t HSUDummyRec::writeAndCloseOutFile(void)
{
if(pOutFile == NULL)
{
Error("writeAndCloseOutFile", "No output file");
return kFALSE;
}
if(bVerbose)
Warning("writeAndCloseOutFile", "writing ...");
if(bWriteEvents)
{
TFile *pOldFile = gFile;
TNamed *pName;
Char_t s[1000];
sprintf(s, "%d", iEvents);
pName = new TNamed("events", s);
pOutFile->cd();
pName->Write();
if(pOldFile != NULL)
pOldFile->cd();
delete pName;
}
pOutFile->Write();
delete pOutFile;
pOutFile = NULL;
if(bVerbose)
Warning("writeAndCloseOutFile", "ok ...");
return kTRUE;
}
HSUIteratorObject::~HSUIteratorObject(void)
{
if(pIter != NULL)
delete pIter;
}
Last change: Sat May 22 13:12:43 2010
Last generated: 2010-05-22 13:12
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.