Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RefToBase to Ptr conversion for pat::TriggerObjectStandAlone #5

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions DataFormats/Common/interface/RefToBaseToPtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef DataFormats_Common_RefToBaseToPtr_h
#define DataFormats_Common_RefToBaseToPtr_h

/*----------------------------------------------------------------------

Ref: A function template for conversion from RefToBase to Ptr

----------------------------------------------------------------------*/

#include "DataFormats/Common/interface/RefToBase.h"
#include "DataFormats/Common/interface/Ptr.h"

namespace edm {
template <typename T>
Ptr<T> refToBaseToPtr(RefToBase<T> const& ref) {
if (ref.isNull()) {
return Ptr<T>();
}
if (ref.isTransient()) {
return Ptr<T>(ref.get(), ref.key());
} else {
//Another thread could change this value so get only once
EDProductGetter const* getter = ref.productGetter();
if (getter) {
return Ptr<T>(ref.id(), ref.key(), getter);
}
}
return Ptr<T>(ref.id(), ref.get(), ref.key());
}
} // namespace edm
#endif
37 changes: 37 additions & 0 deletions DataFormats/Common/interface/RefToBaseToPtr_ioread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef DataFormats_Common_RefToBaseToPtr_ioread_h
#define DataFormats_Common_RefToBaseToPtr_ioread_h

/*----------------------------------------------------------------------

Ref: A function template for conversion from RefToBase to Ptr for use
in ROOT ioread rules for schema migration

----------------------------------------------------------------------*/

#include "DataFormats/Common/interface/RefToBase.h"
#include "DataFormats/Common/interface/Ptr.h"

namespace edm {
template <typename T>
Ptr<T> refToBaseToPtr_ioread(RefToBase<T> const& ref) {
if (ref.isNull()) {
return Ptr<T>();
}
if (ref.isTransient()) {
// This is a logic error, any ref being deserialized
// by construction would be persistent
return Ptr<T>();
} else {
EDProductGetter const* getter = ref.productGetter();
if (getter) {
return Ptr<T>(ref.id(), ref.key(), getter);
}
}
// If this is called in an iorule outside the framework, we cannot call
// ref.get(), but since outside the framework we can never fetch the ref,
// the Ptr will only be useful if accessed later from inside the framework.
// We can fill with a nullptr for now
return Ptr<T>(ref.id(), static_cast<const T*>(nullptr), ref.key());
}
} // namespace edm
#endif
28 changes: 15 additions & 13 deletions DataFormats/PatCandidates/interface/TriggerObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace pat {
/// Reference to trigger object,
/// meant for 'l1extra' particles to access their additional functionalities,
/// empty otherwise
reco::CandidateBaseRef refToOrig_;
reco::CandidatePtr refToOrig_;

public:
/// Constructors and Destructor
Expand Down Expand Up @@ -117,37 +117,39 @@ namespace pat {
/// Special methods for 'l1extra' particles

/// General getters
const reco::CandidateBaseRef& origObjRef() const { return refToOrig_; };
const reco::Candidate* origObjCand() const { return refToOrig_.get(); };
/// Getters specific to the 'l1extra' particle type for
/// - EM
const l1extra::L1EmParticleRef origL1EmRef() const;
const L1GctEmCand* origL1GctEmCand() const {
return origL1EmRef().isNonnull() ? origL1EmRef()->gctEmCand() : nullptr;
auto* cand = dynamic_cast<const l1extra::L1EmParticle*>(origObjCand());
return cand ? cand->gctEmCand() : nullptr;
};
/// - EtMiss
const l1extra::L1EtMissParticleRef origL1EtMissRef() const;
const L1GctEtMiss* origL1GctEtMiss() const {
return origL1EtMissRef().isNonnull() ? origL1EtMissRef()->gctEtMiss() : nullptr;
auto* cand = dynamic_cast<const l1extra::L1EtMissParticle*>(origObjCand());
return cand ? cand->gctEtMiss() : nullptr;
};
const L1GctEtTotal* origL1GctEtTotal() const {
return origL1EtMissRef().isNonnull() ? origL1EtMissRef()->gctEtTotal() : nullptr;
auto* cand = dynamic_cast<const l1extra::L1EtMissParticle*>(origObjCand());
return cand ? cand->gctEtTotal() : nullptr;
};
const L1GctHtMiss* origL1GctHtMiss() const {
return origL1EtMissRef().isNonnull() ? origL1EtMissRef()->gctHtMiss() : nullptr;
auto* cand = dynamic_cast<const l1extra::L1EtMissParticle*>(origObjCand());
return cand ? cand->gctHtMiss() : nullptr;
};
const L1GctEtHad* origL1GctEtHad() const {
return origL1EtMissRef().isNonnull() ? origL1EtMissRef()->gctEtHad() : nullptr;
auto* cand = dynamic_cast<const l1extra::L1EtMissParticle*>(origObjCand());
return cand ? cand->gctEtHad() : nullptr;
};
/// - Jet
const l1extra::L1JetParticleRef origL1JetRef() const;
const L1GctJetCand* origL1GctJetCand() const {
return origL1JetRef().isNonnull() ? origL1JetRef()->gctJetCand() : nullptr;
auto* cand = dynamic_cast<const l1extra::L1JetParticle*>(origObjCand());
return cand ? cand->gctJetCand() : nullptr;
};
/// - Muon
const l1extra::L1MuonParticleRef origL1MuonRef() const;
const L1MuGMTExtendedCand* origL1GmtMuonCand() const {
return origL1MuonRef().isNonnull() ? &(origL1MuonRef()->gmtMuonCand()) : nullptr;
auto* cand = dynamic_cast<const l1extra::L1MuonParticle*>(origObjCand());
return cand ? &(cand->gmtMuonCand()) : nullptr;
};

/// Special methods for the cut string parser
Expand Down
56 changes: 2 additions & 54 deletions DataFormats/PatCandidates/src/TriggerObject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "DataFormats/PatCandidates/interface/TriggerObject.h"

#include "DataFormats/Common/interface/RefToBaseToPtr.h"
#include "FWCore/Utilities/interface/EDMException.h"

using namespace pat;
Expand All @@ -25,7 +26,7 @@ TriggerObject::TriggerObject(const reco::LeafCandidate& leafCand) : reco::LeafCa

// Constructors from base candidate reference (for 'l1extra' particles)
TriggerObject::TriggerObject(const reco::CandidateBaseRef& candRef)
: reco::LeafCandidate(*candRef), refToOrig_(candRef) {
: reco::LeafCandidate(*candRef), refToOrig_(edm::refToBaseToPtr(candRef)) {
triggerObjectTypes_.clear();
}

Expand Down Expand Up @@ -79,56 +80,3 @@ bool TriggerObject::hasTriggerObjectType(trigger::TriggerObjectType triggerObjec
return false;
}

// Special methods for 'l1extra' particles

// Getters specific to the 'l1extra' particle types
// Exceptions of type 'edm::errors::InvalidReference' are thrown,
// if wrong particle type is requested

// EM
const l1extra::L1EmParticleRef TriggerObject::origL1EmRef() const {
l1extra::L1EmParticleRef l1Ref;
try {
l1Ref = origObjRef().castTo<l1extra::L1EmParticleRef>();
} catch (edm::Exception const& X) {
if (X.categoryCode() != edm::errors::InvalidReference)
throw X;
}
return l1Ref;
}

// EtMiss
const l1extra::L1EtMissParticleRef TriggerObject::origL1EtMissRef() const {
l1extra::L1EtMissParticleRef l1Ref;
try {
l1Ref = origObjRef().castTo<l1extra::L1EtMissParticleRef>();
} catch (edm::Exception const& X) {
if (X.categoryCode() != edm::errors::InvalidReference)
throw X;
}
return l1Ref;
}

// Jet
const l1extra::L1JetParticleRef TriggerObject::origL1JetRef() const {
l1extra::L1JetParticleRef l1Ref;
try {
l1Ref = origObjRef().castTo<l1extra::L1JetParticleRef>();
} catch (edm::Exception const& X) {
if (X.categoryCode() != edm::errors::InvalidReference)
throw X;
}
return l1Ref;
}

// Muon
const l1extra::L1MuonParticleRef TriggerObject::origL1MuonRef() const {
l1extra::L1MuonParticleRef l1Ref;
try {
l1Ref = origObjRef().castTo<l1extra::L1MuonParticleRef>();
} catch (edm::Exception const& X) {
if (X.categoryCode() != edm::errors::InvalidReference)
throw X;
}
return l1Ref;
}
9 changes: 7 additions & 2 deletions DataFormats/PatCandidates/src/classes_def_trigger.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<lcgdict>
<selection>

<class name="pat::TriggerObject" ClassVersion="12">
<class name="pat::TriggerObject" ClassVersion="13">
<version ClassVersion="13" checksum="1564486686"/>
<version ClassVersion="12" checksum="2628734905"/>
<version ClassVersion="11" checksum="2574350865"/>
<version ClassVersion="10" checksum="2299474032"/>
</class>
<ioread sourceClass="pat::TriggerObject" version="[-12]" source="reco::CandidateBaseRef refToOrig_" targetClass="pat::TriggerObject" target="refToOrig_">
<![CDATA[ refToOrig_ = edm::refToBaseToPtr_ioread(onfile.refToOrig_); ]]>
</ioread>
<class name="std::vector&lt;pat::TriggerObject&gt;" />
<class name="std::vector&lt;pat::TriggerObject&gt;::const_iterator" />
<class name="edm::Wrapper&lt;std::vector&lt;pat::TriggerObject&gt; &gt;" />
Expand All @@ -26,7 +30,8 @@
<class name="std::map&lt;std::string, edm::RefProd&lt;edm::Association&lt;std::vector&lt;pat::TriggerObject&gt; &gt; &gt; &gt;::const_iterator" />
<class name="edm::Wrapper&lt;std::map&lt;std::string, edm::RefProd&lt;edm::Association&lt;std::vector&lt;pat::TriggerObject&gt; &gt; &gt; &gt; &gt;" />

<class name="pat::TriggerObjectStandAlone" ClassVersion="14">
<class name="pat::TriggerObjectStandAlone" ClassVersion="15">
<version ClassVersion="15" checksum="3059129506"/>
<version ClassVersion="14" checksum="2704342787"/>
<version ClassVersion="13" checksum="54935316"/>
<version ClassVersion="12" checksum="2923001116"/>
Expand Down
1 change: 1 addition & 0 deletions DataFormats/PatCandidates/src/classes_trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "DataFormats/Common/interface/Association.h"
#include "DataFormats/Common/interface/Wrapper.h"
#include "DataFormats/Common/interface/PtrVector.h"
#include "DataFormats/Common/interface/RefToBaseToPtr_ioread.h"

#include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
#include "DataFormats/PatCandidates/interface/TriggerEvent.h"
Expand Down