A: To get the information at tracking time in a given volume A, one can adopt either one or a combination of the following strategies:
1. If the geometry is simple enough, and wish to score some commonly used physics quantities (e.g. energy deposition, dose, flux, etc.), just activate G4ScoringManager in your main program, and use the scorer-based UI commands to transform volume A into a scorer.
See Option 6 below, and the example examples/extended/runAndEvent/RE03.
2. Through the SteppingAction, check that the particle is inside volume A and do whatever needed. Hints can be found in the previous chapter of this FAQ document.
Usually, the hits containers and histograms are attributes of a Track, Event or Run and can be managed through either a TrackingAction, EventAction and/or RunAction and eventually messaging their pointer to the SteppingAction.
A similar approach is illustrated in examples/basic/B2, B4, extended/electromagnetic, optical, and many others…
3. In DetectorConstruction, by declaring volume A as a SensitiveDetector. At stepping time, the Geant4 kernel will automatically check that a particle is inside volume A and will handle the control to a specific function G4VSensitiveDetector::ProcessHits(). It is just necessary to instanciate a class inherited from G4VSensitiveDetector, say VolumeA_SD, and do whatever needed by implementing the function VolumeA_SD::ProcessHits(), as described in Option 2 above.
4. In addition to Option 3 above, should create a HitsCollection to store the information. A HitsCollection can be created in VolumeA_SD::Initialize(). A Hit can be created or filled in VolumeA_SD::ProcessHits(). Additional operations on HitsCollection can be performed in VolumeA_SD::EndOfEvent().
This approach is illustrated in examples/basic/B2, B4 and extended/analysis, extended/runAndEvent/RE01, etc…
5. In DetectorConstruction, volume A can be declared as SensitiveDetector, and one or several pre-defined scorers can be attached to volume A. In this case, neither a SteppingAction nor a spcific VolumeA_SD sensitive detector is needed any longer. It is just necessary to create a dedicated scorer, e.g. MyRunScorer, inherited from G4Run, and handle the HitsCollections within MyRunScorer::RecordEvent(). MyRunScorer itself can be instanciated from RunAction::GenerateRun().
This approach is illustrated in examples/novice/N07, extended/runAndEvent/RE02.
6. A set of build-in scorer-based UI commands allows to perform most possible operations described through the previous Option 5 directly from run-time macros.
The energy deposit and track lengths of the charged particles are recorded on an event by event basis in the Absober and Gap layers. In order to demonstrate several possible ways of data scoring, the example is provided in four variants: Variant a: User Actions These 4 quantities are data members of the B4aEventAction class. They are collected step by step in B4aSteppingAction::UserSteppingAction(), and passed to the event action via two methods: B4aEventAction::AddAbs() and B4aEventAction::AddGap(). In B4aEventAction::EndOfEventAction(), these quantities are printed and filled in H1D histograms and ntuple to accumulate statistic and compute dispersion. Variant b: User data object In order to avoid dependencies between action classes, a user object B4bRunData, derived from G4Run, is defined with data members needed for the accounted information. In order to reduce the number of data members a 2-dimensions array is introduced for each quantity. Then the quantities are collected step by step in user action classes: B4bSteppingAction::UserSteppingAction() and B4bEventAction::EndOfEventAction() in a similar way as in variant a. Variant c: Hits and Sensitive detectors In this option, the physics quantities are accounted using the hits and sensitive detectors framework defined in the Geant4 kernel. The physics quantities are stored in B4cCalorHit via two B4cCalorimeterSD objects, one associated with the Absorber volume and another one with Gap in B4cDetectorConstruction::ConstructSDandField(). In contrary to the B2 example (Tracker) where a new hit is created with each track passing the sensitive volume (in the calorimeter), only one hit is created for each calorimeter layer and one more hit to account for the total quantities in all layers. In addition to the variants a and b, the quantities per each layer are also available in addition to the total quantities. Variant d: Scorer In this option, the Geant4 scorers which are defined on the top of hits and sensitive detectors Geant4 framework are used. In practice this means that the user does not need to define hits and sensitive detector classes but rather uses the classes already defined in Geant4. In this example, the G4MultiFunctionalDetector with G4PSEnergyDeposit and G4PSTrackLength primitive scores are used (see B4dDetectorConstruction::ConstructSDandField()). Also with this approach, the quantities per each layer are available in addition to the total quantities. 7- HISTOGRAMS
The analysis tools are used to accumulate statistics and compute the dispersion of the energy deposit and track lengths of the charged particles. H1D histograms are created in B4[b]RunAction::B4[b]RunAction() for the following quantities: - Energy deposit in absorber - Energy deposit in gap - Track length in absorber - Track length in gap The same values are also saved in an ntuple.
The histograms and the ntuple are saved in the output file in a format according to a technology selected in B4Analysis.hh, the default in this example is ROOT.
The accumulated statistic and computed dispersion is printed at the end of run, in B4RunAction::EndOfRunAction(). When running in multi-threading mode, the histograms and the ntuple accumulated on threads are merged in a single output file. While merging of histograms is performed by default, merging of ntuples is explicitly activated in the B4RunAction constructor.
The ROOT histograms and ntuple can be plotted with ROOT using the plotHisto.C and plotNtuple.C macros.
Geant4 Simulation Data ReadOut by SensitiveDetctor + HitCollection Methods
// set printing event number per each event G4RunManager::GetRunManager()->SetPrintProgress(1);
// Create analysis manager // The choice of analysis technology is done via selectin of a namespace // in B4Analysis.hh auto analysisManager = G4AnalysisManager::Instance(); G4cout << "Using " << analysisManager->GetType() << G4endl;
analysisManager->SetVerboseLevel(1); analysisManager->SetNtupleMerging(true); // Note: merging ntuples is available only with Root output
• beginOfEventAction() This method is invoked before converting the primary particles to G4Track objects. A typical use of this method would be to initialize and/or book histograms for a particular event. • endOfEventAction() This method is invoked at the very end of event processing. It is typically used for a simple analysis of the processed event. If the user wants to keep the currently processing event until the end of the current run, the user can invoke fpEventManager->KeepTheCurrentEvent(); so that it is kept in G4Run object. This should be quite useful if you simulate quite many events and want to visualize only the most interest ones after the long execution. Given the memory size of an event and its contents may be large, it is the user’s responsibility not to keep unnecessary events.
// get analysis manager auto analysisManager = G4AnalysisManager::Instance();
//analyse G4int n_hit = HC->entries(); for(G4int i=0;i<n_hit;i++) { G4String SDName =(*HC)[i]->GetSDName(); G4int EventID=(*HC)[i]->GetEventID();// G4int ParentID=(*HC)[i]->GetParentID();// G4int TrackID=(*HC)[i]->GetTrackID();// G4int StepID=(*HC)[i]->GetStepID();// // Total steps number up to now G4double fEdep=(*HC)[i]->GetEdep(); G4ThreeVector fPos=(*HC)[i]->GetPos(); G4double GlobalTime=(*HC)[i]->GetGlobalTime();// //Time since event is created G4double LocalTime=(*HC)[i]->GetLocalTime();// // Time since track is created G4double ProperTime=(*HC)[i]->GetProperTime();// // Time since track is created (in rest frame of particle) G4double Ekin=(*HC)[i]->GetEkin();// G4double Velocity=(*HC)[i]->GetVelocity();// G4ThreeVector MomentumDirection = (*HC)[i]->GetMomentumDirection(); // Direction of momentum G4String CreatorProcess=(*HC)[i]->GetCreatorProcess();//
A hit is a snapshot of the physical interaction of a track in the sensitive region of a detector. In it you can store information associated with a G4Step object. This information can be
ProcessHits() This method is invoked by G4SteppingManager when a step is composed in the G4LogicalVolume which has the pointer to this sensitive detector. The first argument of this method is a G4Step object of the current step. The second argument is a G4TouchableHistory object for the Readout geometry described in the next section. The second argument is NULL if Readout geometry is not assigned to this sensitive detector. In this method, one or more G4VHit objects should be constructed if the current step is meaningful for your detector. Initialize() This method is invoked at the beginning of each event. The argument of this method is an object of the G4HCofThisEvent class. Hit collections, where hits produced in this particular event are stored, can be associated with the G4HCofThisEvent object in this method. The hit collections associated with the G4HCofThisEvent object during this method can be used for during the event processing digitization.
Initialize() This method is invoked at the beginning of each event. The argument of this method is an object of the G4HCofThisEvent class. Hit collections, where hits produced in this particular event are stored, can be associated with the G4HCofThisEvent object in this method. The hit collections associated with the G4HCofThisEvent object during this method can be used for during the event processing digitization.
example
1 2 3 4 5 6 7 8 9
void PLANETOCOSSoilSD::Initialize(G4HCofThisEvent*HCE) { static int HCID = -1; SoilEdepHitCollection = new PLANETOCOSEdepHitsCollection (SensitiveDetectorName,collectionName[0]); HCID = GetCollectionID(0); HCE->AddHitsCollection(HCID,SoilEdepHitCollection); }
// Add this collection in hce auto hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]); hce->AddHitsCollection( hcID, fHitsCollection );
// Create hits // fNofCells for cells + one more for total sums for (G4int i=0; i<fNofCells+1; i++ ) { fHitsCollection->insert(new B4cCalorHit()); } }
// get the collection name according to the object(name). if(HCID<0){ HCID = G4SDManager::GetSDMpointer() ->GetCollectionID(fTrackHitCollection); } HCE->AddHitsCollection(HCID,fTrackHitCollection); }