Skip to content

Commit

Permalink
Merge pull request key4hep#4 from delitez/debug_pga
Browse files Browse the repository at this point in the history
Debug pga
  • Loading branch information
delitez authored Aug 16, 2022
2 parents 34c0232 + bc4e648 commit ead3c5f
Show file tree
Hide file tree
Showing 22 changed files with 423 additions and 125 deletions.
18 changes: 15 additions & 3 deletions empty_alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from Configurables import objectTestFind
from Configurables import objectTest
from Configurables import ParticleGunAlg
from Configurables import GeoSvc
from Configurables import RandomNumberSvc
from Configurables import EventCounter
#
# algList = []
#
Expand Down Expand Up @@ -33,12 +36,21 @@
a.nMultiplicity = 5;
a.nParticles = 10;
#a.objectPath = "/Event/MyParticle4"
algList.append(a)
#algList.append(a)

b = RandomNumberSvc("MyRndNbrSvc")




d=EmptyAlg("MyEmptyAlg")
algList.append(d)
#algList.append(d)

c = GeoSvc("GeoSvc")
c.detectors = ["/home/delitez/ACTS/acts/thirdparty/OpenDataDetector/xml/OpenDataDetector.xml"]
c.debugGeometry = True
c.outputFileName = "MyObjFileTESTparticleGun"

from Configurables import ApplicationMgr
#print("CRASH TEST")
ApplicationMgr(TopAlg=algList, EvtSel="NONE", EvtMax=5, ExtSvc=[], OutputLevel=DEBUG)
ApplicationMgr(TopAlg=algList, EvtSel="NONE", EvtMax=1, ExtSvc=[b], OutputLevel=DEBUG)
6 changes: 4 additions & 2 deletions eventgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
a.nMultiplicity = 5;
a.nParticles = 10;
algList.append(a)

from Configurables import EmptyAlg
d=EmptyAlg("MyEmptyAlg")
algList.append(d)


from Configurables import ApplicationMgr

ApplicationMgr(TopAlg=algList, EvtSel="NONE", EvtMax=2, ExtSvc=[], OutputLevel=DEBUG)
ApplicationMgr(TopAlg=algList, EvtSel="NONE", EvtMax=1, ExtSvc=[], OutputLevel=DEBUG)
1 change: 1 addition & 0 deletions k4ActsTracking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gaudi_add_module(k4ActsTrackingPlugins
ActsDD4hep::ActsDD4hep
Boost::program_options
ROOT::Core
ActsFatras
)

set(GAUDI_GENCONF_DIR "genConfDir")
Expand Down
3 changes: 3 additions & 0 deletions k4ActsTracking/src/components/EmptyAlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ StatusCode EmptyAlg::initialize() { return StatusCode::SUCCESS; }

StatusCode EmptyAlg::execute() {
std::cout << "HALLO WELT!" << std::endl;
const SimParticleContainer* p_partvect = p_partvec.get();

std::cout << "HADE WELT!" << std::endl;
return StatusCode::SUCCESS;
}

Expand Down
5 changes: 5 additions & 0 deletions k4ActsTracking/src/components/EmptyAlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// GAUDI
#include "Gaudi/Property.h"
#include "GaudiAlg/GaudiAlgorithm.h"
#include "ParticleGunAlg.h"


class EmptyAlg : public GaudiAlgorithm {
public:
Expand All @@ -24,4 +26,7 @@ class EmptyAlg : public GaudiAlgorithm {
private:
// member variable
int m_member = 0;
DataObjectHandle<AnyDataWrapper<SimParticleContainer>> p_partvec{"/Event/testVec", Gaudi::DataHandle::Reader, this};


};
1 change: 1 addition & 0 deletions k4ActsTracking/src/components/GeoSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "DD4hep/Printout.h"
#include "GaudiKernel/Service.h"
#include "TGeoManager.h"
#include "RandomNumberSvc.h"

using namespace Gaudi;

Expand Down
8 changes: 4 additions & 4 deletions k4ActsTracking/src/components/GeoSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class GeoSvc : public extends<Service, IGeoSvc> {
Acts::GeometryContext m_trackingGeoCtx;

/// ACTS Tracking Geometry
// std::unique_ptr<const Acts::TrackingGeometry> m_trackingGeo{nullptr};
std::shared_ptr<const Acts::TrackingGeometry> m_trackingGeo{nullptr};


/// ACTS Material Decorator
std::shared_ptr<const Acts::IMaterialDecorator> m_materialDeco{nullptr};

Expand Down Expand Up @@ -73,12 +73,12 @@ class GeoSvc : public extends<Service, IGeoSvc> {

StatusCode createGeoObj();

// virtual const Acts::TrackingGeometry& trackingGeometry() const;
// virtual std::shared_ptr<const Acts::TrackingGeometry> m_trackingGeo{nullptr};
virtual std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry() const;


};

//inline const Acts::TrackingGeometry& GeoSvc::trackingGeometry() const { return *m_trackingGeo; }
inline std::shared_ptr<const Acts::TrackingGeometry> GeoSvc::trackingGeometry() const { return m_trackingGeo; }


#endif
21 changes: 21 additions & 0 deletions k4ActsTracking/src/components/IEventGeneratorSvc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// D. Elitez, July 2022
// Based on eic/juggler

#ifndef IEVENTGENERATORSVC_H
#define IEVENTGENERATORSVC_H

#include <GaudiKernel/IService.h>



class GAUDI_API IEventGeneratorSvc : virtual public IService {
public:


public:
DeclareInterfaceID(IEventGeneratorSvc, 1, 0);

virtual ~IEventGeneratorSvc() {}
};

#endif // IGEOSVC_H
21 changes: 21 additions & 0 deletions k4ActsTracking/src/components/IRandomNumberSvc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// D. Elitez, August 2022


#ifndef IRANDOMNUMBERSVC_H
#define IRANDOMNUMBERSVC_H

#include <GaudiKernel/IService.h>
#include <unordered_map>


class GAUDI_API IRandomNumberSvc : virtual public IService {
public:


public:
DeclareInterfaceID(IRandomNumberSvc, 1, 0);
virtual uint64_t getSeed() = 0;
virtual ~IRandomNumberSvc() {}
};

#endif
116 changes: 51 additions & 65 deletions k4ActsTracking/src/components/ParticleGunAlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ StatusCode ParticleGunAlg::initialize() {
}
StatusCode ParticleGunAlg::execute() {

SimParticleContainer particles{};


static int seed = 5;
std::mt19937 rng{seed++};
Expand All @@ -32,65 +32,50 @@ StatusCode ParticleGunAlg::execute() {
// Solution: set stuff per hand as rand gauss

// auto vertexPosition = (*vertex)(rng);

std::normal_distribution<double> gauss2(0., 1.);
Acts::Vector4 vertexPosition(0., 0., 0., 0.);
// double vpx = sqrt(gauss2(rng)*gauss2(rng));
// double vpy = sqrt(gauss2(rng)*gauss2(rng));
// Acts::Vector4 vertexPosition(1., 5., 0., 0.);
// std::cout << "vertex position vpx and vpy: " << vpx << " and " << vpy << std::endl;
//
//
std::cout << "line 44"<< std::endl;

auto updateParticleInPlace = [&](ActsFatras::Particle& particle) {

const auto pid = ActsFatras::Barcode(particle.particleId())
.setVertexPrimary(nPrimaryVertices);
std::cout << "line 50"<< std::endl;
const auto pos4 = (vertexPosition + particle.fourPosition()).eval();
std::cout << "line 52"<< std::endl;
particle = particle.withParticleId(pid).setPosition4(pos4);
std::cout << "line 54"<< std::endl;
};
std::cout << "line56" << std::endl;
auto vertexPosition = Acts::Vector4(1.,1.,1.,1.);
auto vertexParticles = genVertexParticles(rng,gauss);

for (auto& vertexParticle : vertexParticles) {
updateParticleInPlace(vertexParticle);
}
std::cout << "line 61"<< std::endl;
particles.merge(std::move(vertexParticles));
std::cout << "line 63"<< std::endl;
}


// Container *containedParticle = new Container();
// containedParticle->m_particles = particles;


// This registiration must be revised because path is given in the python code and it crashes when it is not changed each time ---- TODO: Find a better way
// Question: Should particles registered individually or all together --- if all together, then the ParticleGunAlg must run only once and it is okay as it is now.
// Remark: Same name is okay if no compile, if compile, same name causes a crash

// find a way that objectPath doesnt have to be changed all the time! --- it must work over many events

// StatusCode sc = eventSvc()->registerObject(objectPath, containedParticle);
//
// if( sc.isFailure() ) {
// std::cout << "Object cannot be registered." << std::endl;
// return StatusCode::FAILURE;
// }
// else{
// std::cout << "Object is registered in " << objectPath << std::endl;
// }

std::cout << "BEFORE PUT WELT!" << std::endl;
m_partvec.put(std::move(particles));
std::cout << "AFTER PUT WELT!" << std::endl;
Acts::PdgParticle pdg = Acts::PdgParticle::eMuon;

auto updateParticleInPlace = [&](ActsFatras::Particle& particle) { // aus truth level zu vertexpos
// std::cout << "particle before update: " << particle << std::endl;
// std::cout << "particle 4 position before update: " << particle.fourPosition() << std::endl;
// std::cout << "nPrimaryVertices: " << nPrimaryVertices << std::endl;
// const auto pid = ActsFatras::Barcode(particle.particleId()).setVertexPrimary(nPrimaryVertices);
const auto pid = ActsFatras::Barcode(0u).setParticle(nPrimaryVertices);
// std::cout << "Define pid " << std::endl;
// std::cout << "vertexPosition: " << vertexPosition << std::endl;
// std::cout << "particle four position: " << particle.fourPosition() << std::endl;
// std::cout << "pos4 evaluation: " << (vertexPosition + particle.fourPosition()).eval() << std::endl;
//
// const auto pos4 = (vertexPosition + particle.fourPosition()).eval();
// std::cout << "Define pos4 " << std::endl;
// particle = particle.withParticleId(pid).setPosition4(pos4);
particle.setParticleId(pid);
particle.setPosition4(1.,1.,1.,1.);
// std::cout << "particle after update: " << particle << std::endl;
// std::cout << "particle 4 position after update: " << particle.fourPosition() << std::endl;
// std::cout << "nPrimaryVertices: " << nPrimaryVertices << std::endl;

};



for (auto& vertexParticle : vertexParticles) {
updateParticleInPlace(vertexParticle);
}
particles.merge(std::move(vertexParticles));
}



// std::cout << "BEFORE PUT WELT!" << std::endl;
m_partvec.put(std::move(particles));
// std::cout << "AFTER PUT WELT!" << std::endl;
//
//
//



Expand All @@ -107,6 +92,8 @@ StatusCode ParticleGunAlg::finalize() {

SimParticleContainer ParticleGunAlg::genVertexParticles(std::mt19937& rng, std::normal_distribution<double>& gauss) {

SimParticleContainer gen_particles;

using UniformIndex = std::uniform_int_distribution<unsigned int>;


Expand All @@ -127,8 +114,7 @@ SimParticleContainer ParticleGunAlg::genVertexParticles(std::mt19937& rng, std::
// braucht man 3 elements statt 2?
const double qChoices[] = {charge,-charge, };

SimParticleContainer particles;
std::cout << "line 130"<< std::endl;

for (size_t ip = 1 ; ip <= nParticles; ++ip) {
const auto pid = ActsFatras::Barcode(0u).setParticle(ip);
const unsigned int type = particleTypeChoice(rng);
Expand All @@ -146,17 +132,17 @@ std::cout << "line 130"<< std::endl;
direction[Acts::eMom2] = cos(theta);

//TODO: Fix the mass -- how to extract it from pdg? config file? by hand?
std::cout << "line 148"<< std::endl;

double mass = 0.5;

ActsFatras::Particle particle(pid, pdg, q, mass);
particle.setDirection(direction);
particle.setAbsoluteMomentum(p);
std::cout << "line 155"<< std::endl;
particles.insert(particles.end(), std::move(particle));
std::cout << "line 157"<< std::endl;
ActsFatras::Particle _particle(pid, pdg, q, mass);
_particle.setDirection(direction);
_particle.setAbsoluteMomentum(p);

gen_particles.insert(gen_particles.end(), std::move(_particle));

}
std::cout << "line 159"<< std::endl;
return particles;

return gen_particles;

};
23 changes: 4 additions & 19 deletions k4ActsTracking/src/components/ParticleGunAlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include "GaudiAlg/GaudiAlgorithm.h"
#include "GaudiKernel/DataObjectHandle.h"
#include "GaudiKernel/AnyDataWrapper.h"
#include <GaudiKernel/AnyDataHandle.h>
#include "IEventGeneratorSvc.h"
#include "GaudiKernel/AnyDataHandle.h"
#include "Acts/Definitions/Algebra.hpp"
#include "ActsFatras/EventData/Particle.hpp"
#include "ActsFatras/Utilities/ParticleData.hpp"
Expand Down Expand Up @@ -51,25 +50,11 @@ class ParticleGunAlg : public GaudiAlgorithm {

SimParticleContainer genVertexParticles(std::mt19937& rng, std::normal_distribution<double>& gauss);


struct MultiplicityGenerator {
virtual ~MultiplicityGenerator() = default;
virtual size_t operator()(std::mt19937& rng) const = 0;
};

struct VertexGenerator{
virtual ~VertexGenerator() = default;
virtual Acts::Vector4 operator()(std::mt19937& rng) const = 0;
};

struct Container : public DataObject {
SimParticleContainer m_particles;
};

SimParticleContainer particles;

private:

AnyDataHandle<SimParticleContainer> m_partvec{"/Event/TestVec3", Gaudi::DataHandle::Writer, this};
DataObjectHandle<AnyDataWrapper<SimParticleContainer>> m_partvec{"/Event/testVec", Gaudi::DataHandle::Writer, this};


public:
Expand All @@ -87,7 +72,7 @@ AnyDataHandle<SimParticleContainer> m_partvec{"/Event/TestVec3", Gaudi::DataHand

Gaudi::Property<std::string> objectPath{this, "objectPath", " ", "Path for the object."};

std::shared_ptr<VertexGenerator> vertex = nullptr;
// std::shared_ptr<VertexGenerator> vertex = nullptr;

ParticleGunAlg(const std::string& name, ISvcLocator* svc);

Expand Down
Loading

0 comments on commit ead3c5f

Please sign in to comment.