EventAction.cc

Go to the documentation of this file.
00001 //! \file
00002 //!
00003 //! Source file for the EventAction class.
00004 //!
00005 //! Defines the EventAction class and the member routines which
00006 //! control the actions taken during the event.
00007 //!
00008 //! \author D.K. Hasell
00009 //! \version 1.0
00010 //! \date 2010-10-14
00011 //!
00012 //! \ingroup control
00013 
00014 // Include user header files referenced in this file.
00015 
00016 #include "EventAction.h"
00017 #include "RunAction.h"
00018 
00019 #include "GN_Data.h"
00020 #include "EV_Data.h"
00021 #include "GT_Data.h"
00022 #include "WC_Data.h"
00023 #include "TF_Data.h"
00024 #include "LM_Data.h"
00025 #include "MW_Data.h"
00026 #include "SM_Data.h"
00027 
00028 // Include the GEANT4 header files referenced in this file.
00029 
00030 #include "G4Event.hh"
00031 #include "G4PrimaryVertex.hh"
00032 #include "G4PrimaryParticle.hh"
00033 #include "G4ParticleDefinition.hh"
00034 
00035 // Include required C++ header files.
00036 
00037 #include <iomanip>
00038 
00039 // Include the ROOT header files referenced in this file.
00040 
00041 #include "TTree.h"
00042 
00043 // Initialise the static data members.
00044 
00045 GN_Data * EventAction::gndata = new GN_Data();
00046 EV_Data * EventAction::evdata = new EV_Data();
00047 GT_Data * EventAction::gtdata = new GT_Data();
00048 WC_Data * EventAction::wcdata = new WC_Data();
00049 TF_Data * EventAction::tfdata = new TF_Data();
00050 LM_Data * EventAction::lmdata = new LM_Data();
00051 MW_Data * EventAction::mwdata = new MW_Data();
00052 SM_Data * EventAction::smdata = new SM_Data();
00053 
00054 // Define the EventAction class.
00055 
00056 // Constructor.
00057 
00058 EventAction::EventAction() {}
00059 
00060 // Destructor.
00061 
00062 EventAction::~EventAction() {}
00063 
00064 // Define what happens at the beginning of an event.
00065 
00066 void EventAction::BeginOfEventAction( const G4Event * event ) {
00067 
00068    // Print the event information at the beginning of the event.
00069    // Note this happens after the primary particles are generated.
00070 
00071 //   G4cout << "Beginning of Event " << event->GetEventID() << "\n" << G4endl;
00072 //   Print( event );
00073 
00074    // Get event number.
00075 
00076    G4int id = event->GetEventID();
00077    
00078    // Print silly display to show it is working.
00079 
00080    if( id == 0 ) G4cout << "\nEvent processing status. One dot = 100 events."
00081                         << endl;
00082    if( id % 5000 == 0 ) G4cout << "\nEvent " << setw(10) << id << " " << flush;
00083    else if( id % 1000 == 0 ) G4cout << "|" << flush;
00084    else if( id %  500 == 0 ) G4cout << "+" << flush;
00085    else if( id %  100 == 0 ) G4cout << "." << flush;
00086 
00087 }
00088 
00089 // Define what happens at the end of an event.
00090 
00091 void EventAction::EndOfEventAction( const G4Event * event ) {
00092 
00093    // Print the event information at the end of the event.
00094    // Note this happens after the sensitive detector end of event routines.
00095 
00096 //   G4cout << "End of Event " << event->GetEventID() << "\n" << G4endl;
00097 //   Print( event );
00098 
00099    // Get pointer to TTree.
00100 
00101    TTree * Tree = RunAction::Tree;
00102 
00103    // Define the various branches if not already defined.
00104 
00105    if( !Tree->GetBranchStatus("GN") ) Tree->Branch( "GN", "GN_Data", &gndata );
00106 
00107    if( !Tree->GetBranchStatus("EV") ) Tree->Branch( "EV", "EV_Data", &evdata );
00108 
00109    if( !Tree->GetBranchStatus("GT") ) Tree->Branch( "GT", "GT_Data", &gtdata );
00110 
00111    if( !Tree->GetBranchStatus("WC") ) Tree->Branch( "WC", "WC_Data", &wcdata );
00112 
00113    if( !Tree->GetBranchStatus("TF") ) Tree->Branch( "TF", "TF_Data", &tfdata );
00114 
00115    if( !Tree->GetBranchStatus("LM") ) Tree->Branch( "LM", "LM_Data", &lmdata );
00116 
00117    if( !Tree->GetBranchStatus("MW") ) Tree->Branch( "MW", "MW_Data", &mwdata );
00118 
00119    if( !Tree->GetBranchStatus("SM") ) Tree->Branch( "SM", "SM_Data", &smdata );
00120 
00121    // Reset and fill the EV event information.
00122 
00123    evdata->Reset();
00124 
00125    evdata->nEV = event->GetEventID();
00126 
00127    // Reset and fill the GN event information.
00128 
00129    gndata->Reset();
00130 
00131    G4int nPrim = event->GetNumberOfPrimaryVertex();
00132 
00133    gndata->nGN = nPrim;
00134 
00135    G4PrimaryVertex * vertex;
00136    G4PrimaryParticle * particle;
00137    G4ParticleDefinition * definition;
00138 
00139    for( G4int i = 0; i < nPrim; ++i ) {
00140 
00141       vertex = event->GetPrimaryVertex(i);
00142 
00143       particle = vertex->GetPrimary();
00144 
00145       definition = particle->GetG4code();
00146 
00147       gndata->id.push_back( particle->GetPDGcode() );
00148       gndata->q.push_back( particle->GetCharge() );
00149       gndata->tr.push_back( particle->GetTrackID() );
00150 
00151       gndata->x.push_back( vertex->GetX0()/cm );
00152       gndata->y.push_back( vertex->GetY0()/cm );
00153       gndata->z.push_back( vertex->GetZ0()/cm );
00154 
00155       gndata->px.push_back( particle->GetPx()/GeV );
00156       gndata->py.push_back( particle->GetPy()/GeV );
00157       gndata->pz.push_back( particle->GetPz()/GeV );
00158 
00159    }
00160 
00161    // Write the data to the Tree.
00162 
00163    RunAction::Tree->Fill();
00164 
00165 }
00166 
00167 // Print generated event information.
00168 
00169 void EventAction::Print( const G4Event * event ) {
00170 
00171    G4int nPrim = event->GetNumberOfPrimaryVertex();
00172    G4PrimaryVertex * vertex;
00173    G4PrimaryParticle * particle;
00174    G4ParticleDefinition * definition;
00175 
00176    G4cout << "   Number of primary vertices " << nPrim << "\n" << G4endl;
00177 
00178    for( G4int i = 0; i < nPrim; ++i ) {
00179 
00180       vertex = event->GetPrimaryVertex(i);
00181 
00182       particle = vertex->GetPrimary();
00183 
00184       definition = particle->GetG4code();
00185 
00186       G4cout << "   Vertex " << i 
00187              << " PDG ID " << particle->GetPDGcode()
00188              << " Charge " << particle->GetCharge()
00189              << " Name " << definition->GetParticleName()
00190              << G4endl;
00191 
00192       G4cout << "      Origin "
00193              << vertex->GetX0()/cm << " " 
00194              << vertex->GetY0()/cm << " " 
00195              << vertex->GetZ0()/cm << " [cm]" << G4endl;
00196 
00197       G4cout << "      Momentum "
00198              << particle->GetPx()/GeV << " " 
00199              << particle->GetPy()/GeV << " " 
00200              << particle->GetPz()/GeV << " [GeV/c]\n" << G4endl;
00201    }
00202 }