#include "EventAction.hh" #include "Run.hh" #include "G4Event.hh" #include "G4RunManager.hh" #include "G4HCofThisEvent.hh" #include "G4SDManager.hh" #include "StripHit.hh" #include "StripDigitizer.hh" #include "G4DigiManager.hh" #include "G4SystemOfUnits.hh" #include "HistoManager.hh" #include "G4VPhysicalVolume.hh" #include "G4PhysicalVolumeStore.hh" //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... EventAction::EventAction() : G4UserEventAction(), fEdep(0.) { hitsCollName_x1 = "SiStrip_x1"; hitsCollName_u1 = "SiStrip_u1"; hitsCollName_v1 = "SiStrip_v1"; hitsCollName_x2 = "SiStrip_x2"; hitsCollName_u2 = "SiStrip_u2"; hitsCollName_v2 = "SiStrip_v2"; hitsCollName_TP1 = "AirStrip_TP1"; hitsCollName_TP2 = "AirStrip_TP2"; hitsCollName_TP3 = "AirStrip_TP3"; hitsCollName_TP4 = "AirStrip_TP4"; hitsCollName_x3 = "SiStrip_x3"; hitsCollName_u3 = "SiStrip_u3"; hitsCollName_v3 = "SiStrip_v3"; hitsCollName_x4 = "SiStrip_x4"; hitsCollName_u4 = "SiStrip_u4"; hitsCollName_v4 = "SiStrip_v4"; digitsCollName = "StripDigitCollection"; //We build the digitization module StripDigitizer* stripDigitizer = new StripDigitizer("StripDigitizer",1024,16); G4DigiManager * digiManager = G4DigiManager::GetDMpointer(); digiManager->AddNewModule( stripDigitizer ); // get the histomessenger for plots etc fHistoManager = HistoManager::GetHistoManager(); G4cout << "Event Action Initialisation" << G4endl; stripsConstructed = false; } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... EventAction::~EventAction() {} //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... void EventAction::BeginOfEventAction(const G4Event* evt) { fEdep = 0.; for(G4int l(0); l<24; l++) fEdep_Epi[l] = 0.; fLastRTLayer = -1; G4int evtNumber = evt->GetEventID(); if(evtNumber == 0) { // check periodically that the strips have been constructed but not on every event G4VPhysicalVolume* strip = 0; // if no strip then look for it if(!strip) { G4PhysicalVolumeStore::GetInstance()->GetVolume("x1_Sensor"); if(strip) { stripsConstructed = true; } // if still no strip then look for the DUT name else{ strip = G4PhysicalVolumeStore::GetInstance()->GetVolume("x1_SensorStrip"); if(strip) {stripsConstructed=true;} else {stripsConstructed=false;} } } } if(evtNumber < 100 || evtNumber%1000==0) { G4cout << "Event Number = " << evtNumber << G4endl; } } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... #include "G4UnitsTable.hh" void EventAction::EndOfEventAction(const G4Event* anEvent) { if(stripsConstructed) { //G4cout << "Strips Constructed" << G4endl; EndOfEventTrackerAction(anEvent); } // call the tree for strip RT EndOfEventStripRTAction(anEvent); G4double sumEdep = 0.; for(G4int l(0); l<24; l++){ if(fHistoManager){ fHistoManager->FillHisto("CMOSEdep",l, fEdep_Epi[l]/keV); } sumEdep += fEdep_Epi[l] ; } //G4cout << "The Final Layer in the RT with a hit is " << fLastRTLayer << G4endl; if(fHistoManager){ fHistoManager->FillHisto("CMOSEdepLayer0",fEdep_Epi[0]/keV); fHistoManager->FillHisto("CMOSEdepFinalLayer", fEdep_Epi[fLastRTLayer]/keV); fHistoManager->FillHisto("CMOSEdepSumLayers", sumEdep/keV); fHistoManager->FillHisto("CMOSFinalLayer", fLastRTLayer); } // calculate the energy from a binary readout RT G4double alpha = 0.0052; G4double par = 1.88; G4double e_reco = std::pow((fLastRTLayer / alpha), 1/par); if(e_reco == e_reco) // check for nan and therefore a proton not hitting the RT { //G4cout << fRTTruthEnergy << " " << e_reco << G4endl; if(fHistoManager){ fHistoManager->FillTree("RT_Ereco",0,anEvent->GetEventID(), 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, fRTTruthEnergy, e_reco, fLastRTLayer, 2212, alpha, par); } } } void EventAction::EndOfEventStripRTAction(const G4Event* anEvent) { if(fHistoManager) { //Retrieve hits collections static G4int hitsCollID = -1; if ( hitsCollID < 0 ) { G4SDManager* SDman = G4SDManager::GetSDMpointer(); hitsCollID = SDman->GetCollectionID("StripRT"); } if(hitsCollID>=0) // for now rechecking this checks it the HC was found. if not then we move on NOTE: Fix hack TP 09/01/15 { G4HCofThisEvent* HCE = anEvent->GetHCofThisEvent(); StripHitCollection* hits = 0; if ( HCE ) { hits = static_cast( HCE->GetHC(hitsCollID) ); G4cout << "EndOfEventStripRTAction" << G4endl; fHistoManager->AddStripRTEvent(anEvent->GetEventID(),hits); } } } } void EventAction::EndOfEventTrackerAction(const G4Event* anEvent) { //Digitize!! G4DigiManager * digiManager = G4DigiManager::GetDMpointer(); StripDigitizer* digiModule = static_cast( digiManager->FindDigitizerModule("StripDigitizer") ); if(anEvent->GetEventID() == 0) //only necessary to reset the value once at first event { //Used for re-setting the no. of planes and strips in case they have been changed from the default //vaules set in DetectorConstruction.cc by the .mac file. Necessary because DetectorConsturcion object with //default parameters is passed to EventAction before the .mac file is processed. EventAction then uses the //DetectorConstruction object to construct root file with array elements = to the defined no. of strips. //This then creates an error if the .mac file alters these parameters and recompiles the DetectorConstruction object. // NOTE: Need to not be hardcoded in digiModule->ReSetDigiCollectionStrips( 1024 ); digiModule->ReSetDigiCollectionPlanes( 16 ); } //Store information if ( fHistoManager && digiModule ) { //Retrieve hits collections static G4int hitsCollID_x1 = -1; static G4int hitsCollID_u1 = -1; static G4int hitsCollID_v1 = -1; static G4int hitsCollID_x2 = -1; static G4int hitsCollID_u2 = -1; static G4int hitsCollID_v2 = -1; static G4int hitsCollID_TP1 = -1; static G4int hitsCollID_TP2 = -1; static G4int hitsCollID_TP3 = -1; static G4int hitsCollID_TP4 = -1; static G4int hitsCollID_x3 = -1; static G4int hitsCollID_u3 = -1; static G4int hitsCollID_v3 = -1; static G4int hitsCollID_x4 = -1; static G4int hitsCollID_u4 = -1; static G4int hitsCollID_v4 = -1; if ( hitsCollID_x1 < 0 ) { //Also the following line works: //hitsCollID_1 = digiManager->GetHitsCollectionID(hitscollname_SD1); //But we prefer to show here a (longer) version, in case no digiManager is used G4SDManager * SDman = G4SDManager::GetSDMpointer(); hitsCollID_x1 = SDman->GetCollectionID(hitsCollName_x1); hitsCollID_u1 = SDman->GetCollectionID(hitsCollName_u1); hitsCollID_v1 = SDman->GetCollectionID(hitsCollName_v1); hitsCollID_x2 = SDman->GetCollectionID(hitsCollName_x2); hitsCollID_u2 = SDman->GetCollectionID(hitsCollName_u2); hitsCollID_v2 = SDman->GetCollectionID(hitsCollName_v2); hitsCollID_TP1 = SDman->GetCollectionID(hitsCollName_TP1); hitsCollID_TP2 = SDman->GetCollectionID(hitsCollName_TP2); hitsCollID_TP3 = SDman->GetCollectionID(hitsCollName_TP3); hitsCollID_TP4 = SDman->GetCollectionID(hitsCollName_TP4); hitsCollID_x3 = SDman->GetCollectionID(hitsCollName_x3); hitsCollID_u3 = SDman->GetCollectionID(hitsCollName_u3); hitsCollID_v3 = SDman->GetCollectionID(hitsCollName_v3); hitsCollID_x4 = SDman->GetCollectionID(hitsCollName_x4); hitsCollID_u4 = SDman->GetCollectionID(hitsCollName_u4); hitsCollID_v4 = SDman->GetCollectionID(hitsCollName_v4); } if(hitsCollID_x1>=0) // for now rechecking this checks it the HC was found. if not then we move on NOTE: Fix hack TP 09/01/15 { G4HCofThisEvent* HCE = anEvent->GetHCofThisEvent(); StripHitCollection* hits_x1 = 0; StripHitCollection* hits_u1 = 0; StripHitCollection* hits_v1 = 0; StripHitCollection* hits_x2 = 0; StripHitCollection* hits_u2 = 0; StripHitCollection* hits_v2 = 0; StripHitCollection* hits_TP1 = 0; StripHitCollection* hits_TP2 = 0; StripHitCollection* hits_TP3 = 0; StripHitCollection* hits_TP4 = 0; StripHitCollection* hits_x3 = 0; StripHitCollection* hits_u3 = 0; StripHitCollection* hits_v3 = 0; StripHitCollection* hits_x4 = 0; StripHitCollection* hits_u4 = 0; StripHitCollection* hits_v4 = 0; if ( HCE ) { hits_x1 = static_cast( HCE->GetHC(hitsCollID_x1) ); hits_u1 = static_cast( HCE->GetHC(hitsCollID_u1) ); hits_v1 = static_cast( HCE->GetHC(hitsCollID_v1) ); hits_x2 = static_cast( HCE->GetHC(hitsCollID_x2) ); hits_u2 = static_cast( HCE->GetHC(hitsCollID_u2) ); hits_v2 = static_cast( HCE->GetHC(hitsCollID_v2) ); hits_TP1 = static_cast( HCE->GetHC(hitsCollID_TP1) ); hits_TP2 = static_cast( HCE->GetHC(hitsCollID_TP2) ); hits_TP3 = static_cast( HCE->GetHC(hitsCollID_TP3) ); hits_TP4 = static_cast( HCE->GetHC(hitsCollID_TP4) ); hits_x3 = static_cast( HCE->GetHC(hitsCollID_x3) ); hits_u3 = static_cast( HCE->GetHC(hitsCollID_u3) ); hits_v3 = static_cast( HCE->GetHC(hitsCollID_v3) ); hits_x4 = static_cast( HCE->GetHC(hitsCollID_x4) ); hits_u4 = static_cast( HCE->GetHC(hitsCollID_u4) ); hits_v4 = static_cast( HCE->GetHC(hitsCollID_v4) ); } if ( digiModule ) { digiModule->Digitize(); } //Retrieve digits collection static G4int digiCollID = -1; if ( digiCollID < 0 ) { digiCollID = digiManager->GetDigiCollectionID( digitsCollName ); } G4DCofThisEvent* digitsCollections = anEvent->GetDCofThisEvent(); StripDigiCollection* digits = 0; if ( digitsCollections ) { digits = static_cast( digitsCollections->GetDC(digiCollID) ); //G4cout << "Getting digits..."; //if(digits) // G4cout << "Get digits" << G4endl; //else // G4cout << "Not got digits" << G4endl; } else G4cout << "digitsCollections empty" << G4endl; //Enable for printouts /* if(hits_x1) { int n_hit = hits_x1->entries(); G4cout << "x1 has " << n_hit << " hits." << G4endl; for(int i1=0;i1Print(); } } if(hits_u1) { int n_hit = hits_u1->entries(); G4cout << "u1 has " << n_hit << " hits." << G4endl; for(int i1=0;i1Print(); } } if(hits_v1) { int n_hit = hits_v1->entries(); G4cout << "v1 has " << n_hit << " hits." << G4endl; for(int i1=0;i1Print(); } } */ //Get Postion and Momentum of primary //This is needed to write to root file info @ z=0 const G4ThreeVector& pos = anEvent->GetPrimaryVertex()->GetPosition(); const G4ThreeVector& mom = anEvent->GetPrimaryVertex()->GetPrimary()->GetMomentum(); const G4int event = anEvent->GetEventID(); //Getting KE at entrance and exit.. G4float KE_out = 0; //now set in Rootsaver.cc using KE of primary hit in last tracking detector. const G4float KE_in = anEvent->GetPrimaryVertex()->GetPrimary()->GetKineticEnergy(); //G4cout << "Passing to Histomanager" << G4endl; //Save event to root file fHistoManager->AddTrackerEvent(event, hits_x1,hits_u1,hits_v1,hits_x2,hits_u2,hits_v2, //incoming track info hits_TP1,hits_TP2,hits_TP3,hits_TP4, hits_x3,hits_u3,hits_v3,hits_x4,hits_u4,hits_v4, //outgoing track info digits,pos,mom,KE_in,KE_out); // Print information about hits. Hits container has a method: // G4VHitsCollection::PrintAllHits() that // recursively calls G4VHit::Print() method //hits_x1->PrintAllHits(); //hits_u1->PrintAllHits(); //hits_v1->PrintAllHits(); /* hits_x1->PrintAllHits(); hits_u1->PrintAllHits(); hits_v1->PrintAllHits(); hits_x2->PrintAllHits(); hits_u2->PrintAllHits(); hits_v2->PrintAllHits(); hits_x3->PrintAllHits(); hits_u3->PrintAllHits(); hits_v3->PrintAllHits(); hits_x4->PrintAllHits(); hits_u4->PrintAllHits(); hits_v4->PrintAllHits(); digits->PrintAllDigi(); */ } } } /// BELOW IS THE OLD EVENT ACTION WHICH I KNOW WORKED /* //Get Postion and Momentum of primary //This is needed to store in ntuple info @ z=0 const G4ThreeVector& pos = anEvent->GetPrimaryVertex()->GetPosition(); const G4ThreeVector& mom = anEvent->GetPrimaryVertex()->GetPrimary()->GetMomentum(); const G4int event = anEvent->GetEventID(); //Getting KE at entrance and exit.. G4float KE_out = 0; //now set in Rootsaver.cc using KE of primary hit in last tracking detector. const G4float KE_in = anEvent->GetPrimaryVertex()->GetPrimary()->GetKineticEnergy(); G4SDManager * SDman = G4SDManager::GetSDMpointer(); G4HCofThisEvent* HCE = anEvent->GetHCofThisEvent(); // // GET THE HIT COLLECTIONS FOR THE STRIPS AND WRITE TO THE TREE // static G4int hitsCollID_x1 = SDman->GetCollectionID("SiStrip_x1"); static G4int hitsCollID_u1 = SDman->GetCollectionID("SiStrip_u1"); static G4int hitsCollID_v1 = SDman->GetCollectionID("SiStrip_v1"); static G4int hitsCollID_x2 = SDman->GetCollectionID("SiStrip_x2"); static G4int hitsCollID_u2 = SDman->GetCollectionID("SiStrip_u2"); static G4int hitsCollID_v2 = SDman->GetCollectionID("SiStrip_v2"); static G4int hitsCollID_x3 = SDman->GetCollectionID("SiStrip_x3"); static G4int hitsCollID_u3 = SDman->GetCollectionID("SiStrip_u3"); static G4int hitsCollID_v3 = SDman->GetCollectionID("SiStrip_v3"); static G4int hitsCollID_x4 = SDman->GetCollectionID("SiStrip_x4"); static G4int hitsCollID_u4 = SDman->GetCollectionID("SiStrip_u4"); static G4int hitsCollID_v4 = SDman->GetCollectionID("SiStrip_v4"); static G4int hitsCollID_TruthPlane_I = SDman->GetCollectionID("AirStrip_TruthPlane_I"); static G4int hitsCollID_TruthPlane_O = SDman->GetCollectionID("AirStrip_TruthPlane_O"); G4bool allStripCollectionsFound = false; // check that all the hit collections exist in the SD Manager or get a seg fault if( hitsCollID_x1 > -1 && hitsCollID_u1 > -1 && hitsCollID_v1 > -1 && hitsCollID_x2 > -1 && hitsCollID_u2 > -1 && hitsCollID_v2 > -1 && hitsCollID_x3 > -1 && hitsCollID_u3 > -1 && hitsCollID_v3 > -1 && hitsCollID_x4 > -1 && hitsCollID_u4 > -1 && hitsCollID_v4 > -1 && hitsCollID_TruthPlane_I > -1 && hitsCollID_TruthPlane_O > -1) { allStripCollectionsFound = true; } if(allStripCollectionsFound) { StripHitCollection* hits_x1 = static_cast( HCE->GetHC(hitsCollID_x1) ); StripHitCollection* hits_u1 = static_cast( HCE->GetHC(hitsCollID_u1) ); StripHitCollection* hits_v1 = static_cast( HCE->GetHC(hitsCollID_v1) ); StripHitCollection* hits_x2 = static_cast( HCE->GetHC(hitsCollID_x2) ); StripHitCollection* hits_u2 = static_cast( HCE->GetHC(hitsCollID_u2) ); StripHitCollection* hits_v2 = static_cast( HCE->GetHC(hitsCollID_v2) ); StripHitCollection* hits_x3 = static_cast( HCE->GetHC(hitsCollID_x3) ); StripHitCollection* hits_u3 = static_cast( HCE->GetHC(hitsCollID_u3) ); StripHitCollection* hits_v3 = static_cast( HCE->GetHC(hitsCollID_v3) ); StripHitCollection* hits_x4 = static_cast( HCE->GetHC(hitsCollID_x4) ); StripHitCollection* hits_u4 = static_cast( HCE->GetHC(hitsCollID_u4) ); StripHitCollection* hits_v4 = static_cast( HCE->GetHC(hitsCollID_v4) ); StripHitCollection* hits_TruthPlane_I = static_cast( HCE->GetHC(hitsCollID_TruthPlane_I) ); StripHitCollection* hits_TruthPlane_O = static_cast( HCE->GetHC(hitsCollID_TruthPlane_O) ); G4bool printAllHits = false; if(printAllHits) { hits_x1->PrintAllHits(); hits_u1->PrintAllHits(); hits_v1->PrintAllHits(); hits_x2->PrintAllHits(); hits_u2->PrintAllHits(); hits_v2->PrintAllHits(); hits_x3->PrintAllHits(); hits_u3->PrintAllHits(); hits_v3->PrintAllHits(); hits_x4->PrintAllHits(); hits_u4->PrintAllHits(); hits_v4->PrintAllHits(); } //Digitize!! G4DigiManager * digiManager = G4DigiManager::GetDMpointer(); StripDigitizer* digiModule = static_cast( digiManager->FindDigitizerModule("StripDigitizer") ); if ( digiModule ) { digiModule->Digitize(); } //Retrieve digits collection static G4int digiCollID = -1; if ( digiCollID < 0 ) { digiCollID = digiManager->GetDigiCollectionID( "StripDigitCollection" ); } G4DCofThisEvent* digitsCollections = anEvent->GetDCofThisEvent(); StripDigiCollection* digits = 0; if ( digitsCollections ) { digits = static_cast( digitsCollections->GetDC(digiCollID) ); } //Save event to root file fHistoManager->AddTrackerEvent(event, hits_x1,hits_u1,hits_v1,hits_x2,hits_u2,hits_v2,hits_TruthPlane_I, //incoming track info hits_TruthPlane_O,hits_x3,hits_u3,hits_v3,hits_x4,hits_u4,hits_v4, //outgoing track info digits,pos,mom,KE_in,KE_out); } // end of if(allStripCollectionsFound) // accumulate statistics in Run //Run* run // = static_cast( // G4RunManager::GetRunManager()->GetNonConstCurrentRun()); //run->AddEdep(fEdep); } //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... */