Message: Re: Access to hits collection Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: Access to hits collection 

Forum: Hits, Digitization and Pileup
Re: Question Access to hits collection (Vlasios Vasileiou)
Date: 27 Jan, 2005
From: Jeremy McCormick <Jeremy McCormick>

Hi.

Managing hit collections can be somewhat complicated and usually
depends on the type of application and personal preference.

I will show you one way to do it, fairly "generically".

First, use the name of the G4 SD to retrieve a pointer to it.

G4VSensitiveDetector* SD =
  G4SDManager::GetSDMpointer()->FindSensitiveDetector( sdname );

I always have a base type for SDs, so that I can retrieve basic
information like its type and HC ID.  Once I get a pointer to a G4 
SD, I cast to this base type.

MySensitiveDetector* mySD = static_cast<MySensitiveDetector*> (SD);

Then you can cast to the correct type of HC using the SD's type,
which should unambiguously tell you the type of the HC.

MyHitCollection* myHC = 0;
if ( mySD->getType() == eSomeType ) {
  myHC = static_cast<MyHitCollection*> (g4hc);
}

At least, this is how I do it.

My preference is to retrieve a list of the HC IDs
at the outset using my base SD class.  Then I can 
use information from the base class to lookup the
HC and cast to the correct type.

Here is a utility function to retrieve all the HC IDs
into a vector of ints.

// user's base type of SD
#include "MySensitiveDetector.hh"

// G4 includes
#include "G4SDManager.hh"
#include "G4HCtable.hh"
#include "globals.hh"

std::vector<int> getHCIDs()
{
  G4SDManager* SDmgr = G4SDManager::GetSDMpointer();
  G4HCtable* HCtbl = SDmgr->GetHCtable();    
  G4int num_entries = HCtbl->entries();    
  std::vector<int> hcids;

  for (G4int i = 0;
       i < num_entries;
       i++) {

    // get the name of this SD
    G4String sdname = HCtbl->GetSDname( i );

    // retrieve corresponding Sensitive Detector from Geant4 store and cast to my type      
    MySensitiveDetector* SD = 
      static_cast<MySensitiveDetector*> ( G4SDManager::GetSDMpointer()->FindSensitiveDetector( sdname ) );

    // add HCID from the SD to the list
    if (SD) {      
      hcids.push_back( SD->getHCID() );
    }
  }
    
  return hcids;
}

Good luck.

--Jeremy

Inline Depth:
 1 1
 All All
Outline Depth:
 1 1
 2 2
 All All
Add message: (add)

 Add Message Add Message
to: "Re: Access to hits collection"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]