Heading Inclusion

#include "B2TrackerSD.hh" #include "G4HCofThisEvent.hh" #include "G4Step.hh" #include "G4ThreeVector.hh" #include "G4SDManager.hh" #include "G4ios.hh" #include "TObject.h" #include "TH1F.h" #include "TFile.h" #include "G4AnalysisManager.hh"

These lines include necessary Geant4 headers, ROOT headers (TObject, TH1F, TFile), and the G4AnalysisManager header.

Constructor

B2TrackerSD::B2TrackerSD(const G4String& name, const G4String& hitsCollectionName) : G4VSensitiveDetector(name), fHitsCollection(NULL) { collectionName.insert(hitsCollectionName); // fRoot = new TFile("simulation.root","RECREATE"); /// fRoot = TFile::Open("simulation.root","RECREATE"); // std::unique_ptr<TFile> fRoot(TFile::Open("simulation.root","RECREATE")); /// fH1 = new TH1F("fH1"," Deposited energy (MeV) ",100,0.,5.); // G4RunManager::GetRunManager()->SetPrintProgress(1); // Create analysis manager // The choice of the output format is done via the specified // file extension. auto analysisManager = G4AnalysisManager::Instance(); // Get analysis manager //auto analysisManager = G4AnalysisManager::Instance(); // Open an output file// G4String fileName = "simulation.root"; // Other supported output types: // G4String fileName = "B4.csv"; // G4String fileName = "B4.hdf5"; // G4String fileName = "B4.xml"; analysisManager->OpenFile(fileName); G4cout << "Using " << analysisManager->GetType() << G4endl; // Creating histograms analysisManager->CreateH1("fH1"," Deposited energy (MeV)", 100, 0., 5.); }

This constructor initializes the sensitive detector, sets up the hits collection, and creates a ROOT file and histogram for data analysis.

Destructor

B2TrackerSD::~B2TrackerSD() { auto analysisManager = G4AnalysisManager::Instance(); analysisManager->Write(); analysisManager->CloseFile(); }

The destructor writes the analysis data to file and closes the file.

Initialize Method

void B2TrackerSD::Initialize(G4HCofThisEvent* hce) { // ... (code omitted for brevity) }

This method is called at the beginning of each event. It creates a new hits collection and adds it to the G4HCofThisEvent object.

Process Hits Method

G4bool B2TrackerSD::ProcessHits(G4Step* aStep, G4TouchableHistory*) { // ... (code omitted for brevity) }

This method is called for each step in the sensitive detector. It creates a new hit, sets its properties based on the step information, and adds it to the hits collection.

End of Event Method

void B2TrackerSD::EndOfEvent(G4HCofThisEvent*) { // ... (earlier part of the method) G4double eDepTot = 0; for (G4int i=0;i<nofHits;i++) { eDepTot += (*fHitsCollection)[i]->GetEdep(); } // printf(" Total deposited energy = %f \n",eDepTot); /// fH1->Fill(eDepTot); // get analysis manager auto analysisManager = G4AnalysisManager::Instance(); // fill histograms analysisManager->FillH1(0, eDepTot); G4cout << "eDepTot" << eDepTot <<G4endl; // fRoot->Write(); // fRoot->Close(); }

This method is called at the end of each event. It calculates the total energy deposited in all hits, fills a histogram with this value, and optionally prints out hit information.

Key points: