diff --git a/DDG4/examples/CLICSidSimu.py b/DDG4/examples/CLICSidSimu.py index 30b71fa38a06cefcdc0a15d06f4f2dae7acc67d7..6f0f15856b7ffa3566c7f7adbbd965e3bd5cfe7f 100644 --- a/DDG4/examples/CLICSidSimu.py +++ b/DDG4/examples/CLICSidSimu.py @@ -72,15 +72,16 @@ def run(): mc.release() """ # Configure I/O + """ evt_root = DDG4.EventAction(kernel,'Geant4Output2ROOT/RootOutput') evt_root.Control = True evt_root.Output = "CLICSiD_"+time.strftime("%Y-%m-%d_%H-%M")+".root" evt_root.enableUI() kernel.eventAction().add(evt_root) - + """ evt_lcio = DDG4.EventAction(kernel,'Geant4Output2LCIO/LcioOutput') evt_lcio.Control = True - evt_lcio.Output = "CLICSiD_"+time.strftime("%Y-%m-%d_%H-%M")+".lcio" + evt_lcio.Output = "CLICSiD_"+time.strftime("%Y-%m-%d_%H-%M") evt_lcio.enableUI() kernel.eventAction().add(evt_lcio) @@ -122,31 +123,31 @@ def run(): # First the tracking detectors seq = DDG4.SensitiveSequence(kernel,'Geant4SensDetActionSequence/SiVertexBarrel') - act = DDG4.SensitiveAction(kernel,'Geant4SimpleTrackerAction/SiVertexBarrelHandler','SiVertexBarrel') + act = DDG4.SensitiveAction(kernel,'LcioTestTrackerAction/SiVertexBarrelHandler','SiVertexBarrel') seq.add(act) seq.add(f1) #seq.add(f4) act.add(f1) seq = DDG4.SensitiveSequence(kernel,'Geant4SensDetActionSequence/SiVertexEndcap') - act = DDG4.SensitiveAction(kernel,'Geant4SimpleTrackerAction/SiVertexEndcapHandler','SiVertexEndcap') + act = DDG4.SensitiveAction(kernel,'LcioTestTrackerAction/SiVertexEndcapHandler','SiVertexEndcap') seq.add(act) seq.add(f1) #seq.add(f4) seq = DDG4.SensitiveSequence(kernel,'Geant4SensDetActionSequence/SiTrackerBarrel') - act = DDG4.SensitiveAction(kernel,'Geant4SimpleTrackerAction/SiTrackerBarrelHandler','SiTrackerBarrel') + act = DDG4.SensitiveAction(kernel,'LcioTestTrackerAction/SiTrackerBarrelHandler','SiTrackerBarrel') seq.add(act) seq.add(f1) #seq.add(f4) seq = DDG4.SensitiveSequence(kernel,'Geant4SensDetActionSequence/SiTrackerEndcap') - act = DDG4.SensitiveAction(kernel,'Geant4SimpleTrackerAction/SiTrackerEndcapHandler','SiTrackerEndcap') + act = DDG4.SensitiveAction(kernel,'LcioTestTrackerAction/SiTrackerEndcapHandler','SiTrackerEndcap') #act.OutputLevel = Output.INFO seq.add(act) seq = DDG4.SensitiveSequence(kernel,'Geant4SensDetActionSequence/SiTrackerForward') - act = DDG4.SensitiveAction(kernel,'Geant4SimpleTrackerAction/SiTrackerForwardHandler','SiTrackerForward') + act = DDG4.SensitiveAction(kernel,'LcioTestTrackerAction/SiTrackerForwardHandler','SiTrackerForward') seq.add(act) # Now the calorimeters diff --git a/DDG4/lcio/LCIOConversions.cpp b/DDG4/lcio/LCIOConversions.cpp index 44cb83eddded376394048bcd286fb379a1ecf257..fbeac3d3abb01110b8628e74e82d7445c0058802 100644 --- a/DDG4/lcio/LCIOConversions.cpp +++ b/DDG4/lcio/LCIOConversions.cpp @@ -10,6 +10,7 @@ // Framework include files #define DDG4_MAKE_INSTANTIATIONS #include "DD4hep/LCDD.h" +#include "DD4hep/Printout.h" #include "DDG4/Geant4HitCollection.h" #include "DDG4/Geant4DataConversion.h" #include "DDG4/Geant4Data.h" @@ -74,8 +75,9 @@ namespace DD4hep { size_t nhits = coll->GetSize(); SimpleHit* hit = coll->hit(0); string dsc = encoding(args.first, hit->cellID); - lcio::LCCollectionVec* col = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); - UTIL::CellIDEncoder<SimTrackerHit> decoder(dsc,col); + lcio::LCCollectionVec* lc_coll = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); + UTIL::CellIDEncoder<SimTrackerHit> decoder(dsc,lc_coll); + lc_coll->reserve(nhits); for(size_t i=0; i<nhits; ++i) { const SimpleTracker::Hit* g4_hit = coll->hit(i); double pos[3] = {g4_hit->position.x(), g4_hit->position.y(), g4_hit->position.z()}; @@ -87,9 +89,9 @@ namespace DD4hep { lc_hit->setTime(g4_hit->truth.time); lc_hit->setMomentum( g4_hit->momentum.x(), g4_hit->momentum.y() , g4_hit->momentum.z() ); lc_hit->setPathLength( g4_hit->length); - col->addElement(lc_hit); + lc_coll->addElement(lc_hit); } - return col; + return lc_coll; } /// Data conversion interface creating lcio::SimCalorimeterHitImpl from SimpleCalorimeter::Hit structures @@ -111,9 +113,10 @@ namespace DD4hep { size_t nhits = coll->GetSize(); SimpleHit* hit = coll->hit(0); string dsc = encoding(args.first, hit->cellID); - lcio::LCCollectionVec* col = new lcio::LCCollectionVec(lcio::LCIO::SIMCALORIMETERHIT); - UTIL::CellIDEncoder<SimCalorimeterHit> decoder(dsc,col); - col->setFlag(UTIL::make_bitset32(LCIO::CHBIT_LONG,LCIO::CHBIT_STEP)); + lcio::LCCollectionVec* lc_coll = new lcio::LCCollectionVec(lcio::LCIO::SIMCALORIMETERHIT); + UTIL::CellIDEncoder<SimCalorimeterHit> decoder(dsc,lc_coll); + lc_coll->setFlag(UTIL::make_bitset32(LCIO::CHBIT_LONG,LCIO::CHBIT_STEP)); + lc_coll->reserve(nhits); for(size_t i=0; i<nhits; ++i) { const SimpleCalorimeter::Hit* g4_hit = coll->hit(i); float pos[3] = {g4_hit->position.x(), g4_hit->position.y(), g4_hit->position.z()}; @@ -122,9 +125,9 @@ namespace DD4hep { lc_hit->setCellID1( ( g4_hit->cellID >> sizeof( int ) ) & 0xFFFFFFFF ); lc_hit->setPosition(pos); lc_hit->setEnergy( g4_hit->energyDeposit ); - col->addElement(lc_hit); + lc_coll->addElement(lc_hit); } - return col; + return lc_coll; } /// Data conversion interface moving lcio::SimTrackerHitImpl objects from a Geant4HitCollection to a LCCollectionVec @@ -147,13 +150,16 @@ namespace DD4hep { { Geant4HitCollection* coll = args.second; lcio::LCCollectionVec* lc_coll = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); + lc_coll->reserve(coll->GetSize()); + printout(DEBUG,"LCIOConversion", + "+++ Converting %s to LCCollectionVec(SIMTRACKERHIT)",coll->GetName().c_str()); for(size_t i=0, nhits = coll->GetSize(); i<nhits; ++i) { Geant4HitWrapper& wrap = coll->hit(i); lcio::SimTrackerHitImpl* lc_hit = wrap; wrap.release(); // Now we have ownership! lc_coll->addElement(lc_hit); } - coll->clear(); + coll->clear(); // Since the collection now only contains NULL pointers, better clear it! return lc_coll; } @@ -176,29 +182,33 @@ namespace DD4hep { lcio::SimCalorimeterHitImpl>::operator()(const arg_t& args) const { Geant4HitCollection* coll = args.second; - lcio::LCCollectionVec* lc_coll = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); + lcio::LCCollectionVec* lc_coll = new lcio::LCCollectionVec(lcio::LCIO::SIMCALORIMETERHIT); + lc_coll->setFlag(UTIL::make_bitset32(LCIO::CHBIT_LONG,LCIO::CHBIT_STEP)); + lc_coll->reserve(coll->GetSize()); + printout(DEBUG,"LCIOConversion", + "+++ Converting %s to LCCollectionVec(SIMCALORIMETERHIT)",coll->GetName().c_str()); for(size_t i=0, nhits = coll->GetSize(); i<nhits; ++i) { Geant4HitWrapper& wrap = coll->hit(i); lcio::SimCalorimeterHitImpl* lc_hit = wrap; wrap.release(); // Now we have ownership! lc_coll->addElement(lc_hit); } - coll->clear(); + coll->clear(); // Since the collection now only contains NULL pointers, better clear it! return lc_coll; } - typedef pair<VolMgr,G4VHitsCollection*> _AA1; - typedef pair<VolMgr,Geant4HitCollection*> _AA2; - template class Geant4Conversion<lcio::LCCollectionVec,_AA1>; - DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,_AA1,Geant4HitCollection) + typedef pair<VolMgr,G4VHitsCollection*> RAW_CONVERSION_ARGS; + typedef pair<VolMgr,Geant4HitCollection*> CONVERSION_ARGS; + template class Geant4Conversion<lcio::LCCollectionVec,RAW_CONVERSION_ARGS>; + DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,RAW_CONVERSION_ARGS,Geant4HitCollection) - template class Geant4Conversion<lcio::LCCollectionVec,_AA2>; + template class Geant4Conversion<lcio::LCCollectionVec,CONVERSION_ARGS>; // Hit converters for simple Geant4Data objects - DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,_AA2,SimpleTracker::Hit) - DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,_AA2,SimpleCalorimeter::Hit) + DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,CONVERSION_ARGS,SimpleTracker::Hit) + DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,CONVERSION_ARGS,SimpleCalorimeter::Hit) // Hit converters for standard LCIO objects - DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,_AA2,lcio::SimTrackerHitImpl) - DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,_AA2,lcio::SimCalorimeterHitImpl) + DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,CONVERSION_ARGS,lcio::SimTrackerHitImpl) + DECLARE_GEANT4_HITCONVERTER(lcio::LCCollectionVec,CONVERSION_ARGS,lcio::SimCalorimeterHitImpl) } // End namespace Simulation } // End namespace DD4hep diff --git a/DDG4/lcio/LCIOSDTestActions.cpp b/DDG4/lcio/LCIOSDTestActions.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e24eb140008f91d21a4c056177f54adb538bcf18 --- /dev/null +++ b/DDG4/lcio/LCIOSDTestActions.cpp @@ -0,0 +1,128 @@ +//==================================================================== +// DDSim - LC simulation based on DD4hep +//-------------------------------------------------------------------- +// F.Gaede, DESY +//==================================================================== + +#include "IMPL/LCCollectionVec.h" +#include "IMPL/SimTrackerHitImpl.h" +#include "IMPL/SimCalorimeterHitImpl.h" +#include "IMPL/MCParticleImpl.h" +#include "UTIL/Operators.h" +#include "UTIL/ILDConf.h" +//#include "G4lcioSimHit.h" + +#include "DDG4/Geant4SensDetAction.h" +#include "DDG4/Geant4Data.h" +#include "DDG4/Geant4StepHandler.h" + +#include "DD4hep/Printout.h" +#include "DD4hep/InstanceCount.h" + +using namespace DD4hep::Simulation; +using namespace DD4hep; + +namespace Tests { + + // copied from Geant4SDActions.cpp (why is this not a public class ??????) + + /** Simple SensitiveAction class ... + */ + template <typename T> class Geant4SensitiveAction : public Geant4Sensitive { + protected: + typedef Geant4Sensitive Base; + /// Collection identifiers + size_t m_collectionID; + + // properties: + bool _detailedHitsStoring ; + + public: + // typedef SimpleHit::Contribution HitContribution; + // Standard , initializing constructor + Geant4SensitiveAction(Geant4Context* context, const std::string& name, DetElement det, LCDD& lcdd) + : Geant4Sensitive(context,name,det,lcdd), m_collectionID(0) { + declareProperty("detailedHitsStoring", _detailedHitsStoring ) ; + defineCollections(); + InstanceCount::increment(this); + } + /// Default destructor + virtual ~Geant4SensitiveAction(){ + InstanceCount::decrement(this); + } + /// Define collections created by this sensitivie action object + virtual void defineCollections() {} + /// G4VSensitiveDetector interface: Method invoked at the begining of each event. + virtual void begin(G4HCofThisEvent* hce) { + Base::begin(hce); + } + /// G4VSensitiveDetector interface: Method invoked at the end of each event. + virtual void end(G4HCofThisEvent* hce) { + Base::end(hce); + } + /// G4VSensitiveDetector interface: Method for generating hit(s) using the G4Step object. + virtual bool process(G4Step* step,G4TouchableHistory* history) { + return Base::process(step,history); + } + /// G4VSensitiveDetector interface: Method invoked if the event was aborted. + virtual void clear(G4HCofThisEvent* hce) { + Base::clear(hce); + } + }; + + + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + /// Geant4SensitiveAction<SimpleTracker> + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + struct LcioTestTracker; + /// Define collections created by this sensitivie action object + template <> void Geant4SensitiveAction<LcioTestTracker>::defineCollections() { + m_collectionID = Base::defineCollection<lcio::SimTrackerHitImpl>(m_sensitive.readout().name()); + } + + /// Method for generating hit(s) using the information of G4Step object. + template <> bool Geant4SensitiveAction<LcioTestTracker>::process(G4Step* step,G4TouchableHistory* /*hist*/ ) { + StepHandler h(step); + + Position prePos = h.prePos(); + Position postPos = h.postPos(); + Position direction = postPos - prePos; + Position position = mean_direction(prePos,postPos); + double hit_len = direction.R(); + + if (hit_len > 0) { + double new_len = mean_length(h.preMom(),h.postMom())/hit_len; + direction *= new_len/hit_len; + } + + lcio::SimTrackerHitImpl* hit = new lcio::SimTrackerHitImpl; + // (h.track->GetTrackID(), + // h.track->GetDefinition()->GetPDGEncoding(), + // step->GetTotalEnergyDeposit(), + // h.track->GetGlobalTime()); + + // HitContribution contrib = Hit::extractContribution(step); + + VolumeID cellID = volumeID( step ) ; + hit->setCellID0( cellID & 0xffffffff ) ; + hit->setCellID1( ( cellID >> 32 ) & 0xffffffff ) ; + + printout(INFO,"LcioTestTracker","%s> Add hit with deposit:%f Pos:%f %f %f - cellID0: 0x%x cellID1: 0x%x", + c_name(),step->GetTotalEnergyDeposit(),position.X(),position.Y(),position.Z() , hit->getCellID0() ,hit->getCellID1() ); + + double pos[3] = {position.x(), position.y(), position.z()}; + hit->setPosition( pos ) ; + + // hit->energyDeposit = contrib.deposit ; + // hit->position = position; + // hit->momentum = direction; + // hit->length = hit_len; + collection(m_collectionID)->add(hit); + return hit != 0; + } + + typedef Geant4SensitiveAction<LcioTestTracker> LcioTestTrackerAction; +} // namespace + +#include "DDG4/Factories.h" +DECLARE_GEANT4SENSITIVE_NS(Tests,LcioTestTrackerAction) diff --git a/DDG4/src/Geant4Exec.cpp b/DDG4/src/Geant4Exec.cpp index 045c3d1266a607fd1336d89e52cfd5c72f180e4a..a37dbfa2b899bcd04123e2195a3750db7bd1a7a1 100644 --- a/DDG4/src/Geant4Exec.cpp +++ b/DDG4/src/Geant4Exec.cpp @@ -297,7 +297,7 @@ int Geant4Exec::configure(Geant4Kernel& kernel) { // Check if the geometry was loaded if (lcdd.detectors().size() <= 1) { - printout(INFO, "Geant4Exec", "+++ Only %d subdetectors present. " + printout(WARNING, "Geant4Exec", "+++ Only %d subdetectors present. " "You sure you loaded the geometry properly?",int(lcdd.detectors().size())); } // Get the detector constructed