// $Id: ICOMETOutput.cxx,v 1.11 2011/09/07 21:55:44 mcgrew Exp $ // // Implement the COMET::ICOMETOutput class which is a specialization of the // TEventOutput class that writes the events in a root format. This is the // native format. // #include #include #include #include #include "ICOMETOutput.hxx" #include "ICOMETEvent.hxx" #include "IFieldDescription.hxx" #include "IOADatabase.hxx" #include "ICOMETLog.hxx" ClassImp(COMET::ICOMETOutput); COMET::ICOMETOutput::ICOMETOutput(const char *fileName, Option_t* opt, int compress) : TFile(fileName, opt, "ROOT Output File", compress), fEventTree(NULL), fEventPointer(NULL), fAttached(false), fEventsWritten(0), fGeometry(NULL),fFieldDescription(NULL) { COMETVerbose("Open output file " << fileName); IsAttached(); } COMET::ICOMETOutput::~ICOMETOutput(void) { if (IsOpen()) Close(); } bool COMET::ICOMETOutput::IsAttached(void) { if (!IsOpen()) return false; if (gFile != this) { if (gFile) { COMETDebug("Changing current file from " << gFile->GetName() << " to " << this->GetName() << " to write."); } cd(); } if (fAttached) return true; COMETInfo("Attaching to " << this->GetName()); ////////////// // Hmm... Not attached, so make sure it is attached. // Make sure the object is attached fEventPointer = NULL; if (!fEventTree) { COMETTrace("Create a new tree"); fEventTree = new TTree("COMETEvents", "COMET Event Tree"); } COMETTrace("Add the branch pointer"); fEventTree->Branch("COMETEvent","COMET::ICOMETEvent",&fEventPointer,128000,0); fEventPointer = NULL; // Make sure it's empty. fAttached = true; COMETTrace("Attached"); return fAttached; } int COMET::ICOMETOutput::GetEventsWritten(void) {return fEventsWritten;} void COMET::ICOMETOutput::WriteEvent(COMET::ICOMETEvent& event) { if (!IsAttached()) return; // Copy the pointer into the location attached to the file. fEventPointer = &event; // Put the event into the tree; if (fEventTree->Fill()<0) { COMETError("Error while writing an event"); throw COMET::ECOMETOutputWriteFailed(); } // Empty out the fEventPointer so that it can't be written twice. fEventPointer = NULL; } // Save a geometry to the output file. void COMET::ICOMETOutput::WriteGeometry(TGeoManager* geom) { if (!IsAttached()) return; if (!geom) return; TKey *key = FindKey(geom->GetName()); if (key) return; fGeometry = geom; if (geom->Write()<1) { COMETError("Error while writing geometry"); throw COMET::ECOMETOutputWriteFailed(); } Flush(); COMETLog("** Geometry " << geom->GetName() << " written to output file "); } bool COMET::ICOMETOutput::GeometryWritten(void) { return fGeometry; } void COMET::ICOMETOutput::WriteEMField(COMET::IFieldDescription* field){ if (!IsAttached()) return; if (!field) return; TKey *key = FindKey(field->GetName()); if (key) return; fFieldDescription = field; if (field->Write()<1) { COMETError("Error while writing field description"); throw COMET::ECOMETOutputWriteFailed(); } Flush(); COMETLog("** EM Field description " << field->GetName() << " written to output file "); } bool COMET::ICOMETOutput::EMFieldWritten(void) { return fFieldDescription; } void COMET::ICOMETOutput::Commit(void) { if (!IsAttached()) return; fEventTree->AutoSave(); Flush(); } void COMET::ICOMETOutput::Close(Option_t* opt) { Write(); if (COMET::ICOMETLog::LogLevel <= COMET::ICOMETLog::GetLogLevel()) { TFile::ls(); } TFile::Close(opt); }