File indexing completed on 2025-12-16 10:12:59
0001
0002
0003 #ifndef EDM4HEP_MutableMCParticle_H
0004 #define EDM4HEP_MutableMCParticle_H
0005
0006 #include "edm4hep/MCParticleObj.h"
0007
0008 #include "edm4hep/MCParticle.h"
0009
0010 #include "edm4hep/Vector3d.h"
0011 #include "podio/RelationRange.h"
0012 #include <cmath>
0013 #include <cstdint>
0014 #include <edm4hep/FeatureFlags.h>
0015 #include <edm4hep/utils/bit_utils.h>
0016 #include <vector>
0017
0018 #include "podio/utilities/MaybeSharedPtr.h"
0019
0020 #include <cstdint>
0021
0022 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0023 #include "nlohmann/json_fwd.hpp"
0024 #endif
0025
0026
0027 namespace edm4hep {
0028 class MCParticleCollection;
0029 }
0030
0031 namespace edm4hep {
0032
0033
0034
0035
0036
0037 class MutableMCParticle {
0038
0039 friend class MCParticleCollection;
0040 friend class MCParticleMutableCollectionIterator;
0041 friend class MCParticle;
0042
0043 public:
0044 using object_type = MCParticle;
0045 using collection_type = MCParticleCollection;
0046
0047
0048 MutableMCParticle() = default;
0049
0050
0051 MutableMCParticle(const std::int32_t PDG, const std::int32_t generatorStatus, const std::int32_t simulatorStatus,
0052 const float charge, const float time, const double mass, const edm4hep::Vector3d& vertex,
0053 const edm4hep::Vector3d& endpoint, const edm4hep::Vector3d& momentum,
0054 const edm4hep::Vector3d& momentumAtEndpoint, const std::int32_t helicity);
0055
0056
0057 MutableMCParticle(const MutableMCParticle& other) = default;
0058
0059
0060 MutableMCParticle& operator=(MutableMCParticle other) &;
0061 MutableMCParticle&
0062 operator=(MutableMCParticle other) && = delete;
0063
0064
0065
0066 MutableMCParticle clone(bool cloneRelations = true) const;
0067
0068
0069 ~MutableMCParticle() = default;
0070
0071 public:
0072
0073 std::int32_t getPDG() const;
0074
0075
0076 std::int32_t getGeneratorStatus() const;
0077
0078
0079 std::int32_t getSimulatorStatus() const;
0080
0081
0082 float getCharge() const;
0083
0084
0085
0086 float getTime() const;
0087
0088
0089 double getMass() const;
0090
0091
0092 const edm4hep::Vector3d& getVertex() const;
0093
0094
0095 const edm4hep::Vector3d& getEndpoint() const;
0096
0097
0098 const edm4hep::Vector3d& getMomentum() const;
0099
0100
0101 const edm4hep::Vector3d& getMomentumAtEndpoint() const;
0102
0103
0104 std::int32_t getHelicity() const;
0105
0106
0107 void setPDG(const std::int32_t PDG);
0108
0109 std::int32_t& getPDG();
0110
0111 [[deprecated("use getPDG instead")]] std::int32_t& PDG();
0112
0113
0114 void setGeneratorStatus(const std::int32_t generatorStatus);
0115
0116 std::int32_t& getGeneratorStatus();
0117
0118 [[deprecated("use getGeneratorStatus instead")]] std::int32_t& generatorStatus();
0119
0120
0121 void setSimulatorStatus(const std::int32_t simulatorStatus);
0122
0123 std::int32_t& getSimulatorStatus();
0124
0125 [[deprecated("use getSimulatorStatus instead")]] std::int32_t& simulatorStatus();
0126
0127
0128 void setCharge(const float charge);
0129
0130 float& getCharge();
0131
0132 [[deprecated("use getCharge instead")]] float& charge();
0133
0134
0135
0136 void setTime(const float time);
0137
0138
0139 float& getTime();
0140
0141
0142 [[deprecated("use getTime instead")]] float& time();
0143
0144
0145 void setMass(const double mass);
0146
0147 double& getMass();
0148
0149 [[deprecated("use getMass instead")]] double& mass();
0150
0151
0152 void setVertex(const edm4hep::Vector3d& vertex);
0153
0154 edm4hep::Vector3d& getVertex();
0155
0156 [[deprecated("use getVertex instead")]] edm4hep::Vector3d& vertex();
0157
0158
0159 void setEndpoint(const edm4hep::Vector3d& endpoint);
0160
0161 edm4hep::Vector3d& getEndpoint();
0162
0163 [[deprecated("use getEndpoint instead")]] edm4hep::Vector3d& endpoint();
0164
0165
0166 void setMomentum(const edm4hep::Vector3d& momentum);
0167
0168 edm4hep::Vector3d& getMomentum();
0169
0170 [[deprecated("use getMomentum instead")]] edm4hep::Vector3d& momentum();
0171
0172
0173 void setMomentumAtEndpoint(const edm4hep::Vector3d& momentumAtEndpoint);
0174
0175 edm4hep::Vector3d& getMomentumAtEndpoint();
0176
0177 [[deprecated("use getMomentumAtEndpoint instead")]] edm4hep::Vector3d& momentumAtEndpoint();
0178
0179
0180 void setHelicity(const std::int32_t helicity);
0181
0182 std::int32_t& getHelicity();
0183
0184 [[deprecated("use getHelicity instead")]] std::int32_t& helicity();
0185
0186 void addToParents(const edm4hep::MCParticle&);
0187 std::size_t parents_size() const;
0188 edm4hep::MCParticle getParents(std::size_t) const;
0189 std::vector<edm4hep::MCParticle>::const_iterator parents_begin() const;
0190 std::vector<edm4hep::MCParticle>::const_iterator parents_end() const;
0191 podio::RelationRange<edm4hep::MCParticle> getParents() const;
0192 void addToDaughters(const edm4hep::MCParticle&);
0193 std::size_t daughters_size() const;
0194 edm4hep::MCParticle getDaughters(std::size_t) const;
0195 std::vector<edm4hep::MCParticle>::const_iterator daughters_begin() const;
0196 std::vector<edm4hep::MCParticle>::const_iterator daughters_end() const;
0197 podio::RelationRange<edm4hep::MCParticle> getDaughters() const;
0198
0199
0200 static const int BITCreatedInSimulation = 30;
0201 static const int BITBackscatter = 29;
0202 static const int BITVertexIsNotEndpointOfParent = 28;
0203 static const int BITDecayedInTracker = 27;
0204 static const int BITDecayedInCalorimeter = 26;
0205 static const int BITLeftDetector = 25;
0206 static const int BITStopped = 24;
0207 static const int BITOverlay = 23;
0208
0209 double getEnergy() const {
0210 return std::sqrt(getMomentum()[0] * getMomentum()[0] + getMomentum()[1] * getMomentum()[1] +
0211 getMomentum()[2] * getMomentum()[2] + getMass() * getMass());
0212 }
0213
0214
0215 bool isCreatedInSimulation() const { return utils::checkBit(getSimulatorStatus(), BITCreatedInSimulation); }
0216
0217 bool isBackscatter() const { return utils::checkBit(getSimulatorStatus(), BITBackscatter); }
0218
0219 bool vertexIsNotEndpointOfParent() const {
0220 return utils::checkBit(getSimulatorStatus(), BITVertexIsNotEndpointOfParent);
0221 }
0222
0223 bool isDecayedInTracker() const { return utils::checkBit(getSimulatorStatus(), BITDecayedInTracker); }
0224
0225 bool isDecayedInCalorimeter() const { return utils::checkBit(getSimulatorStatus(), BITDecayedInCalorimeter); }
0226
0227 bool hasLeftDetector() const { return utils::checkBit(getSimulatorStatus(), BITLeftDetector); }
0228
0229 bool isStopped() const { return utils::checkBit(getSimulatorStatus(), BITStopped); }
0230
0231 bool isOverlay() const { return utils::checkBit(getSimulatorStatus(), BITOverlay); }
0232
0233 bool hasHelicity() const noexcept { return getHelicity() != 9; }
0234
0235 void setCreatedInSimulation(bool bitval) {
0236 setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITCreatedInSimulation, bitval));
0237 }
0238 void setBackscatter(bool bitval) { setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITBackscatter, bitval)); }
0239 void setVertexIsNotEndpointOfParent(bool bitval) {
0240 setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITVertexIsNotEndpointOfParent, bitval));
0241 }
0242 void setDecayedInTracker(bool bitval) {
0243 setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITDecayedInTracker, bitval));
0244 }
0245 void setDecayedInCalorimeter(bool bitval) {
0246 setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITDecayedInCalorimeter, bitval));
0247 }
0248 void setHasLeftDetector(bool bitval) {
0249 setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITLeftDetector, bitval));
0250 }
0251 void setStopped(bool bitval) { setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITStopped, bitval)); }
0252 void setOverlay(bool bitval) { setSimulatorStatus(utils::setBit(getSimulatorStatus(), BITOverlay, bitval)); }
0253
0254
0255 bool isAvailable() const;
0256
0257 void unlink() { m_obj = podio::utils::MaybeSharedPtr<MCParticleObj>{nullptr}; }
0258
0259 bool operator==(const MutableMCParticle& other) const { return m_obj == other.m_obj; }
0260 bool operator==(const MCParticle& other) const;
0261
0262 bool operator!=(const MutableMCParticle& other) const { return !(*this == other); }
0263 bool operator!=(const MCParticle& other) const { return !(*this == other); }
0264
0265
0266 bool operator<(const MutableMCParticle& other) const {
0267 return podio::detail::getOrderKey(*this) < podio::detail::getOrderKey(other);
0268 }
0269
0270 podio::ObjectID id() const { return getObjectID(); }
0271
0272 const podio::ObjectID getObjectID() const;
0273
0274 friend std::hash<MutableMCParticle>;
0275
0276 friend void swap(MutableMCParticle& a, MutableMCParticle& b) {
0277 using std::swap;
0278 swap(a.m_obj, b.m_obj);
0279 }
0280
0281 private:
0282
0283 explicit MutableMCParticle(podio::utils::MaybeSharedPtr<MCParticleObj> obj);
0284
0285 podio::utils::MaybeSharedPtr<MCParticleObj> m_obj{new MCParticleObj{}, podio::utils::MarkOwned};
0286 };
0287
0288 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0289 void to_json(nlohmann::json& j, const MutableMCParticle& value);
0290 #endif
0291
0292 }
0293
0294 template <>
0295 struct std::hash<edm4hep::MutableMCParticle> {
0296 std::size_t operator()(const edm4hep::MutableMCParticle& obj) const {
0297 return std::hash<edm4hep::MCParticleObj*>{}(obj.m_obj.get());
0298 }
0299 };
0300
0301 #endif