#include "THaRunBase.h"
#include "THaRunParameters.h"
#include "THaEvData.h"
#include "TClass.h"
#include <iostream>
#if ROOT_VERSION_CODE < ROOT_VERSION(3,1,6)
#include <ctime>
#endif
using namespace std;
static const int UNDEFDATE = 19950101;
static const char* NOTINIT = "uninitialized run";
THaRunBase::THaRunBase( const char* description ) :
TNamed(NOTINIT, description ),
fNumber(-1), fType(0), fDate(UNDEFDATE,0), fNumAnalyzed(0),
fDBRead(kFALSE), fIsInit(kFALSE), fOpened(kFALSE), fAssumeDate(kFALSE),
fDataSet(0), fDataRead(0), fDataRequired(kDate), fParam(0)
{
ClearEventRange();
}
THaRunBase::THaRunBase( const THaRunBase& rhs ) :
TNamed( rhs ), fNumber(rhs.fNumber), fType(rhs.fType),
fDate(rhs.fDate), fNumAnalyzed(rhs.fNumAnalyzed), fDBRead(rhs.fDBRead),
fIsInit(rhs.fIsInit), fOpened(kFALSE), fAssumeDate(rhs.fAssumeDate),
fDataSet(rhs.fDataSet), fDataRead(rhs.fDataRead),
fDataRequired(rhs.fDataRequired)
{
fEvtRange[0] = rhs.fEvtRange[0];
fEvtRange[1] = rhs.fEvtRange[1];
if( rhs.fParam ) {
fParam = static_cast<THaRunParameters*>(rhs.fParam->IsA()->New());
*fParam = *rhs.fParam;
} else
fParam = 0;
}
THaRunBase& THaRunBase::operator=(const THaRunBase& rhs)
{
if (this != &rhs) {
TNamed::operator=(rhs);
fNumber = rhs.fNumber;
fType = rhs.fType;
fDate = rhs.fDate;
fEvtRange[0] = rhs.fEvtRange[0];
fEvtRange[1] = rhs.fEvtRange[1];
fNumAnalyzed = rhs.fNumAnalyzed;
fDBRead = rhs.fDBRead;
fIsInit = rhs.fIsInit;
fOpened = kFALSE;
fAssumeDate = rhs.fAssumeDate;
fDataSet = rhs.fDataSet;
fDataRead = rhs.fDataRead;
fDataRequired = rhs.fDataRequired;
if( rhs.fParam ) {
fParam = static_cast<THaRunParameters*>(rhs.fParam->IsA()->New());
*fParam = *rhs.fParam;
} else
fParam = 0;
}
return *this;
}
THaRunBase::~THaRunBase()
{
delete fParam ; fParam = 0;
}
Int_t THaRunBase::Update( const THaEvData* evdata )
{
if( !evdata )
return -1;
Int_t ret = 0;
if( evdata->IsPrestartEvent() ) {
if( !fAssumeDate ) {
fDate.Set( evdata->GetRunTime() );
fDataSet |= kDate;
}
SetNumber( evdata->GetRunNum() );
SetType( evdata->GetRunType() );
fDataRead |= kDate|kRunNumber|kRunType;
ret = 1;
}
if( evdata->IsPrescaleEvent() ) {
for(int i=0; i<fParam->GetPrescales().GetSize(); i++)
fParam->Prescales()[i] = evdata->GetPrescaleFactor(i+1);
fDataSet |= kPrescales;
fDataRead |= kPrescales;
ret = 2;
}
return ret;
}
bool THaRunBase::operator==( const THaRunBase& rhs ) const
{
return (fNumber == rhs.fNumber);
}
bool THaRunBase::operator!=( const THaRunBase& rhs ) const
{
return (fNumber != rhs.fNumber);
}
bool THaRunBase::operator<( const THaRunBase& rhs ) const
{
return (fNumber < rhs.fNumber);
}
bool THaRunBase::operator>( const THaRunBase& rhs ) const
{
return (fNumber > rhs.fNumber);
}
bool THaRunBase::operator<=( const THaRunBase& rhs ) const
{
return (fNumber <= rhs.fNumber);
}
bool THaRunBase::operator>=( const THaRunBase& rhs ) const
{
return (fNumber >= rhs.fNumber);
}
void THaRunBase::Clear( const Option_t* opt )
{
TString sopt(opt);
bool doing_init = (sopt == "INIT");
Close();
fName = NOTINIT;
fNumber = -1;
fType = 0;
fNumAnalyzed = 0;
fDBRead = fIsInit = fOpened = kFALSE;
fDataSet = fDataRead = 0;
fParam->Clear(opt);
if( doing_init && fAssumeDate )
SetDate(fDate);
else
ClearDate();
if( !doing_init ) {
ClearEventRange();
}
}
void THaRunBase::ClearDate()
{
fDate.Set(UNDEFDATE,0);
fAssumeDate = fIsInit = kFALSE;
fDataSet &= ~kDate;
}
void THaRunBase::ClearEventRange()
{
fEvtRange[0] = 1;
fEvtRange[1] = kMaxUInt;
}
Int_t THaRunBase::Compare( const TObject* obj ) const
{
if (this == obj) return 0;
const THaRunBase* rhs = dynamic_cast<const THaRunBase*>(obj);
if( !rhs ) return -1;
if ( fNumber < rhs->fNumber ) return -1;
else if ( fNumber > rhs->fNumber ) return 1;
return 0;
}
Bool_t THaRunBase::HasInfo( UInt_t bits ) const
{
return ((bits & fDataSet) == bits);
}
Bool_t THaRunBase::HasInfoRead( UInt_t bits ) const
{
return ((bits & fDataRead) == bits);
}
Int_t THaRunBase::Init()
{
static const char* const here = "Init";
if( !fParam )
fParam = new THaRunParameters;
Clear("INIT");
Int_t retval = 0;
if( !IsOpen() ) {
retval = Open();
if( retval )
return retval;
}
retval = ReadInitInfo();
Close();
if( retval )
return retval;
if( !HasInfo(fDataRequired) ) {
const char* errmsg[] = { "run date", "run number", "run type",
"prescale factors", 0 };
TString errtxt("Missing run parameters: ");
UInt_t i = 0, n = 0;
const char** msg = errmsg;
while( *msg ) {
if( !HasInfo(BIT(i)&fDataRequired) ) {
if( n>0 )
errtxt += ", ";
errtxt += *msg;
n++;
}
i++;
msg++;
}
errtxt += ". Run not initialized.";
Error( here, errtxt );
return 255;
}
retval = ReadDatabase();
if( retval )
return retval;
fIsInit = kTRUE;
return 0;
}
Bool_t THaRunBase::IsOpen() const
{
return fOpened;
}
void THaRunBase::Print( Option_t* opt ) const
{
TNamed::Print( opt );
cout << "Run number: " << fNumber << endl;
cout << "Run date: " << fDate.AsString() << endl;
cout << "Requested event range: " << fEvtRange[0] << "-"
<< fEvtRange[1] << endl;
TString sopt(opt);
if( sopt == "STARTINFO" )
return;
cout << "Analyzed events: " << fNumAnalyzed << endl;
cout << "Assume Date: " << fAssumeDate << endl;
cout << "Database read: " << fDBRead << endl;
cout << "Initialized : " << fIsInit << endl;
cout << "Opened : " << fOpened << endl;
cout << "Date set/read/req: "
<< HasInfo(kDate) << " " << HasInfoRead(kDate) << " "
<< (Bool_t)((kDate & fDataRequired) == kDate) << endl;
cout << "Run num set/read/req: "
<< HasInfo(kRunNumber) << " " << HasInfoRead(kRunNumber) << " "
<< (Bool_t)((kRunNumber & fDataRequired) == kRunNumber) << endl;
cout << "Run type set/read/req: "
<< HasInfo(kRunType) << " " << HasInfoRead(kRunType) << " "
<< (Bool_t)((kRunType & fDataRequired) == kRunType) << endl;
cout << "Prescales set/rd/req: "
<< HasInfo(kPrescales) << " " << HasInfoRead(kPrescales) << " "
<< (Bool_t)((kPrescales & fDataRequired) == kPrescales) << endl;
if( fParam )
fParam->Print(opt);
}
Int_t THaRunBase::ReadDatabase()
{
static const char* const here = "ReadDatabase";
if( !fParam ) {
Error( here, "No run parameter object defined!?! "
"Something is very wrong." );
return -255;
}
TDatime undef(UNDEFDATE,0);
if( fDate == undef ) {
Error( here, "Run date undefined. Cannot read database without a date." );
return -254;
}
Int_t st = fParam->ReadDatabase(fDate);
if( st )
return st;
fDBRead = true;
return 0;
}
Int_t THaRunBase::ReadInitInfo()
{
if( !fAssumeDate )
Error( "ReadInitInfo", "Run date not set." );
return 0;
}
void THaRunBase::SetDate( const TDatime& date )
{
if( fDate != date ) {
fDate = date;
fIsInit = kFALSE;
}
fAssumeDate = kTRUE;
fDataSet |= kDate;
}
void THaRunBase::SetDate( UInt_t tloc )
{
#if ROOT_VERSION_CODE >= ROOT_VERSION(3,1,6)
TDatime date( tloc );
#else
time_t t = tloc;
struct tm* tp = localtime(&t);
TDatime date;
date.Set( tp->tm_year, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min, tp->tm_sec );
#endif
SetDate( date );
}
void THaRunBase::SetEventRange( UInt_t first, UInt_t last )
{
fEvtRange[0] = first;
fEvtRange[1] = last;
}
void THaRunBase::SetFirstEvent( UInt_t n )
{
fEvtRange[0] = n;
}
void THaRunBase::SetLastEvent( UInt_t n )
{
fEvtRange[1] = n;
}
void THaRunBase::SetNumber( Int_t number )
{
fNumber = number;
SetName( Form("RUN_%u", number) );
fDataSet |= kRunNumber;
}
void THaRunBase::SetType( Int_t type )
{
fType = type;
fDataSet |= kRunType;
}
ClassImp(THaRunBase)
Last change: Sat Nov 7 21:26:50 2009
Last generated: 2009-11-07 21:26
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.