Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

WriteStdHepFileModule.cxx

Go to the documentation of this file.
00001 
00002 // $Id: WriteStdHepFileModule.cxx,v 1.2 2007/08/02 19:02:56 rhatcher Exp $
00003 //
00004 // A Module to write out the StdHep structure found in a SimSnarlRecord
00005 // to a binary file compatible with future use by pre-existing FORTRAN
00006 // programs (e.g. gminos)
00007 //
00008 // rhatcher@fnal.gov
00010 #include "RerootExodus/WriteStdHepFileModule.h"
00011 using namespace std;
00012 
00013 #include "TClonesArray.h"
00014 #include "TSystem.h"
00015 #include "TParticle.h"
00016 
00017 // MINOS includes
00018 #include "MessageService/MsgService.h"     // MSG text output
00019 #include "MinosObjectMap/MomNavigator.h"   // Data access
00020 #include "Record/SimSnarlHeader.h"
00021 #include "Record/SimSnarlRecord.h"
00022 
00023 #include "JobControl/JobCommand.h"         // JobCommand handling
00024 #include "JobControl/JobCModuleRegistry.h" // JOBMODULE registration macro
00025 
00026 #include "Util/UtilPDG.h"
00027 #include "Util/UtilHepevt.h"
00028 
00029 CVSID("$Id: WriteStdHepFileModule.cxx,v 1.2 2007/08/02 19:02:56 rhatcher Exp $");
00030 JOBMODULE(WriteStdHepFileModule,"WriteStdHepFileModule","Write /HEPEVT/ file.\n");
00031 
00032 //......................................................................
00033 
00034 WriteStdHepFileModule::WriteStdHepFileModule()
00035   : fStdHepFilename("blah.dat"), fD(-1), fIonScheme(UtilPDG::kIonUnchanged), 
00036     fRecordSets(0)
00037 {
00038 
00039 }
00040 
00041 //......................................................................
00042 
00043 const Registry& WriteStdHepFileModule::DefaultConfig() const
00044 {
00045 //======================================================================
00046 // Create a registry which holds the default configuration and return it
00047 //======================================================================
00048   static Registry r;
00049 
00050   // Set name of config
00051   std::string name = this->GetName();
00052   name += ".config.default";
00053   r.SetName(name.c_str());
00054 
00055   // Set values of config
00056   r.UnLockValues();
00057   r.Set("OutputFilename","hepevt.dat");
00058   r.Set("ConvertIonPDG",(int)UtilPDG::kIonUnchanged);
00059   r.LockValues();
00060 
00061   return r;
00062 }
00063 
00064 //......................................................................
00065 
00066 void WriteStdHepFileModule::Config(const Registry& r)
00067 {
00068 //======================================================================
00069 // Configure the module given the registry r
00070 //======================================================================
00071     const char*   tmpc;
00072     int           tmpi;
00073 
00074     std::string oldname(fStdHepFilename);
00075     if (r.Get("OutputFilename",tmpc)) fStdHepFilename = std::string(tmpc);
00076 
00077     if (fStdHepFilename != oldname) CloseFile();
00078 
00079     if (r.Get("ConvertIonPDG",tmpi)) {
00080       if (fIonScheme != tmpi) {
00081         MSG("Exodus",Msg::kInfo) 
00082           << GetName() << " will convert ions using "
00083           << UtilPDG::ionSchemeName((UtilPDG::ionscheme_t)tmpi)
00084           << "." << endl;
00085       }
00086       fIonScheme = tmpi;
00087     }
00088 }
00089 
00090 //......................................................................
00091 
00092 void WriteStdHepFileModule::Report() 
00093 {
00094   MSG("Exodus",Msg::kInfo) 
00095     << "Report for WriteStdHepFileModule Module:" << endl;
00096 
00097   MSG("Exodus",Msg::kInfo) 
00098     << "   Processed " << fRecordSets << " records written to " 
00099     << fStdHepFilename << "." << endl;
00100 }
00101 
00102 //......................................................................
00103 
00104 void WriteStdHepFileModule::EndJob() 
00105 {
00106     Report();
00107     CloseFile();
00108 }
00109 
00110 //......................................................................
00111 
00112 void WriteStdHepFileModule::OpenFile() 
00113 {
00114   MSG("Exodus",Msg::kInfo) 
00115     << "Open file: " << fStdHepFilename << endl;
00116 
00117   fD = UtilHepevt::openBinary(fStdHepFilename.c_str(),true);
00118 
00119   if (fD < 0) {
00120     MSG("Exodus",Msg::kInfo) 
00121       << GetName() << " failed to open file: " 
00122       << fStdHepFilename << "." << endl;
00123   }
00124 
00125 }
00126 
00127 //......................................................................
00128 
00129 void WriteStdHepFileModule::CloseFile() 
00130 {
00131   if ( fD < 0 ) return;  // nothing open
00132 
00133   MSG("Exodus",Msg::kInfo) 
00134     << "Closing file: " << fStdHepFilename << endl;
00135 
00136   UtilHepevt::closeBinary(fD);
00137   fD = -1;
00138 
00139 }
00140 
00141 //......................................................................
00142 
00143 JobCResult WriteStdHepFileModule::Ana(const MomNavigator *mom) 
00144 {
00145 
00146   JobCResult result = JobCResult::kFailed;
00147   int snarlsInRecSet = 0;
00148 
00149   TObject* recObj = 0;
00150 
00151   // find the SimSnarlRecord
00152   TIter recItr = mom->FragmentIter();
00153   while ( ( recObj = recItr.Next() ) ) {
00154     SimSnarlRecord* simrec = dynamic_cast<SimSnarlRecord*>(recObj);
00155     if (!simrec) continue;  // skip things in mom that aren't SimSnarlRecords
00156 
00157     const SimSnarlHeader* simhead = simrec->GetSimSnarlHeader();
00158     int evtnum = simhead->GetSnarl();
00159 
00160     const TObject* obj = simrec->FindComponent("TClonesArray","StdHep");
00161     const TClonesArray* stdhep = dynamic_cast<const TClonesArray*>(obj);
00162 
00163     if (stdhep) {
00164       if ( fD < 0 ) OpenFile();  // if first time open the file
00165       // record current scheme for translating ion codes
00166       UtilPDG::ionscheme_t ionsave = UtilPDG::getDfltStdIonScheme();
00167       // set requested scheme
00168       UtilPDG::setDfltStdIonScheme((UtilPDG::ionscheme_t)fIonScheme);
00169       // fill the /HEPEVT/ common from the TClonesArray of TParticles
00170       UtilHepevt::fillCommon(stdhep);
00171       // set the event # in /HEPEVT/ common
00172       UtilHepevt::setNevHep(evtnum);
00173       // write the binary file from the /HEPEVT/ common
00174       if ( UtilHepevt::writeBinary(fD) ) {
00175         // count record that have been processed
00176         fRecordSets++;
00177         snarlsInRecSet++;
00178         result = JobCResult::kPassed;
00179       }
00180       // restore the ion translation scheme
00181       UtilPDG::setDfltStdIonScheme(ionsave);
00182     }
00183     
00184   }
00185 
00186   if ( snarlsInRecSet > 1 ) {
00187       MSG("Exodus",Msg::kWarning )
00188           << GetName() 
00189           << " RecordSet contained more than one StdHep "
00190           << endl;
00191   }
00192   return result;
00193 }
00194 

Generated on Mon Feb 15 11:07:51 2010 for loon by  doxygen 1.3.9.1