// OutSOCProc.cc
// Contact person: P G Jones
// Contact person: R Stainforth
// See OutSOCProc.hh for more details.
//—————————————————————---------------------------------------------——//
#include
#include
#include
#include
#include
#include
using namespace RAT;
#include
#include
#include
using namespace std;
OutSOCProc::OutSOCProc() : Processor("OutSOCProc")
{
fFile = NULL;
fSOCTree = NULL;
fRunTree = NULL;
fSOCBranch = new DS::SOC();
fRunBranch = new DS::Run();
fUpdate = false;
fAutoFlush = 1000000;
fAutoSave = 10000000;
}
OutSOCProc::~OutSOCProc()
{
if( fFile != NULL )
{
// Store the current ROOT global directory (to switch back too) and switch to the file's directory
TDirectory* const oldDirectory = gDirectory;
fFile->cd();
// Write both trees to the file
if(!fSOCTree->Write())
Log::Die("Could not write SOC tree!");
if(!fRunTree->Write())
Log::Die("Could not write run tree to SOC file!");
// Write the log and macro to a string in the file
TObjString* log = new TObjString( Log::GetLogBuffer().c_str() );
if(!log->Write( "log" ))
Log::Die("Could not write log to SOC file");
TObjString* macro = new TObjString( Log::GetMacro().c_str() );
if(!macro->Write( "macro" ))
Log::Die("Could not write macro to SCO file");
// Write the database trace (of access)
TMap *dbtrace = Log::GetDBTraceMap();
if(!dbtrace->Write("db", TObject::kSingleKey))
Log::Die("Could not write db trace to SOC file!");
// set the end of job meta information
fMetaHelper.SetStoredEvents(fSOCTree->GetEntries());
MetaInformation::Get()->Finalise(fMetaHelper);
// Write the MetaInformation (RAT and DB state)
if(!MetaInformation::Get()->GetDSMeta().Write( "meta" ))
Log::Die("Could not write meta to SOC file!");
// Switch back to the previous ROOT global directory
oldDirectory->cd();
// Cleanup
fFile->Close(); // also deletes TTree and TObjString?
delete fFile;
delete fSOCBranch;
delete fRunBranch;
}
// Don't delete branchDS because it looks like the TTree took ownership of it
}
void
OutSOCProc::SetS( const std::string& param,
const std::string& value )
{
if( param == "file" )
fOverrideFileName = value;
else if( param == "updatefile" )
{
fOverrideFileName = value;
fUpdate = true;
}
else
throw Processor::ParamUnknown( param );
}
void
OutSOCProc::SetI( const std::string& param,
const int value )
{
if( param == "autosave" )
{
if( value < 0 )
throw ParamInvalid( param, "autosave value must be >= 0" );
fAutoSave = value;
// SetAutoSave might be called after the file is already opened
if( fFile != NULL )
{
fSOCTree->SetAutoSave( fAutoSave );
fRunTree->SetAutoSave( fAutoSave );
}
}
else if( param == "autoflush" )
{
fAutoFlush = value;
// SetAutoFlush might be called after the file is already opened
if( fFile != NULL )
{
fSOCTree->SetAutoFlush( fAutoFlush );
fRunTree->SetAutoFlush( fAutoFlush );
}
}
}
void
OutSOCProc::BeginOfRun( DS::Run& )
{
// Get the database file name, this is the default unless the user overrides it
try
{
fDBFileName = DB::Get()->GetLink("IO")->GetS("default_output_filename");
if( fDBFileName.find( "." ) == string::npos )
fDBFileName += ".soc.root";
}
catch( DBNotFoundError& e ) { Log::Die( "OutSOCProc::BeginOfRun: File name not found in IO[] ratdb." ); }
}
void
OutSOCProc::EndOfRun( DS::Run& run )
{
if( fFile == NULL )
OpenFile();
TDirectory* const oldDirectory = gDirectory;
fFile->cd();
*fRunBranch = run;
if(-1 == fRunTree->Fill()) Log::Die("Could not fill SOC run tree!");
vector socIDs = run.GetSOCSourceIDs();
for( size_t isoc = 0; isoc < socIDs.size(); isoc++ )
{
DS::SOC& soc = run.GetSOCData( socIDs[isoc] );
*fSOCBranch = soc;
if(-1 == fSOCTree->Fill()) Log::Die("Could not fill SOC tree!");
}
oldDirectory->cd();
fMetaHelper.AddRun(run);
}
void
OutSOCProc::OpenFile()
{
string fileName = fDBFileName;
if( !fOverrideFileName.empty() )
fileName = fOverrideFileName;
if( fUpdate )
{
info << "OutSOCProc: Appending to " << fileName << newline;
fFile = TFile::Open( fileName.c_str(), "UPDATE" );
}
else
{
info << "OutSOCProc: Writing to " << fileName << newline;
fFile = TFile::Open( fileName.c_str(), "RECREATE" );
}
if( fFile == NULL )
Log::Die( "OutSOCProc::OpenFile: Cannot open the file." );
if( fUpdate )
{
fSOCTree = reinterpret_cast( fFile->Get( "T" ) );
fRunTree = reinterpret_cast( fFile->Get( "runT" ) );
fSOCTree->SetBranchAddress( "soc", &fSOCBranch, 0 );
fRunTree->SetBranchAddress( "run", &fRunBranch, 0 );
}
else
{
// Setup tree
fSOCTree = new TTree( "T", "RAT Tree" );
fSOCTree->Branch( "soc", fSOCBranch->ClassName(), &fSOCBranch, 32000, 99 );
fRunTree = new TTree( "runT", "RAT Run Tree" );
fRunTree->Branch( "run", fRunBranch->ClassName(), &fRunBranch, 32000, 99 );
}
fSOCTree->SetAutoFlush( fAutoFlush );
fSOCTree->SetAutoSave( fAutoSave );
fRunTree->SetAutoFlush( fAutoFlush );
fRunTree->SetAutoSave( fAutoSave );
}