File indexing completed on 2024-04-06 12:01:08
0001 #ifndef RecoAlgos_GsfElectronSelector_h
0002 #define RecoAlgos_GsfElectronSelector_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
0017 #include "DataFormats/EgammaCandidates/interface/GsfElectronCore.h"
0018 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
0019 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0020 #include "DataFormats/GsfTrackReco/interface/GsfTrackExtra.h"
0021 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0022 #include "CommonTools/UtilAlgos/interface/SingleObjectSelector.h"
0023
0024 namespace helper {
0025 struct GsfElectronCollectionStoreManager {
0026 typedef reco::GsfElectronCollection collection;
0027 GsfElectronCollectionStoreManager(const edm::Handle<reco::GsfElectronCollection>&)
0028 : selElectrons_(new reco::GsfElectronCollection),
0029 selElectronCores_(new reco::GsfElectronCoreCollection),
0030 selSuperClusters_(new reco::SuperClusterCollection),
0031 selTracks_(new reco::GsfTrackCollection),
0032 selTrackExtras_(new reco::TrackExtraCollection),
0033 selGsfTrackExtras_(new reco::GsfTrackExtraCollection),
0034 selHits_(new TrackingRecHitCollection) {}
0035 template <typename I>
0036 void cloneAndStore(const I& begin, const I& end, edm::Event& evt) {
0037 using namespace reco;
0038 TrackingRecHitRefProd rHits = evt.template getRefBeforePut<TrackingRecHitCollection>();
0039 TrackExtraRefProd rTrackExtras = evt.template getRefBeforePut<TrackExtraCollection>();
0040 GsfTrackExtraRefProd rGsfTrackExtras = evt.template getRefBeforePut<GsfTrackExtraCollection>();
0041 GsfTrackRefProd rTracks = evt.template getRefBeforePut<GsfTrackCollection>();
0042 GsfElectronCoreRefProd rElectronCores = evt.template getRefBeforePut<GsfElectronCoreCollection>();
0043 GsfElectronRefProd rElectrons = evt.template getRefBeforePut<GsfElectronCollection>();
0044 SuperClusterRefProd rSuperClusters = evt.template getRefBeforePut<SuperClusterCollection>();
0045 size_t idx = 0, tidx = 0, hidx = 0;
0046 for (I i = begin; i != end; ++i) {
0047 const GsfElectron& ele = **i;
0048 selElectronCores_->push_back(GsfElectronCore(*(ele.core())));
0049 selElectronCores_->back().setGsfTrack(GsfTrackRef(rTracks, idx));
0050 selElectronCores_->back().setSuperCluster(SuperClusterRef(rSuperClusters, idx));
0051 selSuperClusters_->push_back(SuperCluster(*(ele.superCluster())));
0052 selElectrons_->push_back(GsfElectron(ele,
0053 GsfElectronCoreRef(rElectronCores, idx++),
0054 CaloClusterPtr(),
0055 TrackRef(),
0056 TrackBaseRef(),
0057 GsfTrackRefVector()));
0058 GsfTrackRef trkRef = ele.gsfTrack();
0059 if (trkRef.isNonnull()) {
0060 selTracks_->push_back(GsfTrack(*trkRef));
0061 GsfTrack& trk = selTracks_->back();
0062 selTrackExtras_->push_back(TrackExtra(trk.outerPosition(),
0063 trk.outerMomentum(),
0064 trk.outerOk(),
0065 trk.innerPosition(),
0066 trk.innerMomentum(),
0067 trk.innerOk(),
0068 trk.outerStateCovariance(),
0069 trk.outerDetId(),
0070 trk.innerStateCovariance(),
0071 trk.innerDetId(),
0072 trk.seedDirection()));
0073 selGsfTrackExtras_->push_back(GsfTrackExtra(*(trk.gsfExtra())));
0074 TrackExtra& tx = selTrackExtras_->back();
0075 unsigned int nHitsToAdd = trk.recHitsSize();
0076 for (auto const& hit : trk.recHits())
0077 selHits_->push_back(hit->clone());
0078 tx.setHits(rHits, hidx, nHitsToAdd);
0079 tx.setTrajParams(trk.extra()->trajParams(), trk.extra()->chi2sX5());
0080 assert(tx.trajParams().size() == tx.recHitsSize());
0081 hidx += nHitsToAdd;
0082 trk.setGsfExtra(GsfTrackExtraRef(rGsfTrackExtras, tidx));
0083 trk.setExtra(TrackExtraRef(rTrackExtras, tidx++));
0084 }
0085 }
0086 }
0087
0088 edm::OrphanHandle<reco::GsfElectronCollection> put(edm::Event& evt) {
0089 edm::OrphanHandle<reco::GsfElectronCollection> h = evt.put(std::move(selElectrons_));
0090 evt.put(std::move(selElectronCores_));
0091 evt.put(std::move(selSuperClusters_));
0092 evt.put(std::move(selTracks_));
0093 evt.put(std::move(selTrackExtras_));
0094 evt.put(std::move(selGsfTrackExtras_));
0095 evt.put(std::move(selHits_));
0096 return h;
0097 }
0098
0099 size_t size() const { return selElectrons_->size(); }
0100
0101 private:
0102 std::unique_ptr<reco::GsfElectronCollection> selElectrons_;
0103 std::unique_ptr<reco::GsfElectronCoreCollection> selElectronCores_;
0104 std::unique_ptr<reco::SuperClusterCollection> selSuperClusters_;
0105 std::unique_ptr<reco::GsfTrackCollection> selTracks_;
0106 std::unique_ptr<reco::TrackExtraCollection> selTrackExtras_;
0107 std::unique_ptr<reco::GsfTrackExtraCollection> selGsfTrackExtras_;
0108 std::unique_ptr<TrackingRecHitCollection> selHits_;
0109 };
0110
0111 class GsfElectronSelectorBase : public edm::stream::EDFilter<> {
0112 public:
0113 GsfElectronSelectorBase(const edm::ParameterSet& cfg) {
0114 std::string alias(cfg.getParameter<std::string>("@module_label"));
0115 produces<reco::GsfElectronCollection>().setBranchAlias(alias + "GsfElectrons");
0116 produces<reco::GsfElectronCoreCollection>().setBranchAlias(alias + "GsfElectronCores");
0117 produces<reco::SuperClusterCollection>().setBranchAlias(alias + "SuperClusters");
0118 produces<reco::GsfTrackCollection>().setBranchAlias(alias + "GsfTracks");
0119 produces<reco::GsfTrackExtraCollection>().setBranchAlias(alias + "GsfTrackExtras");
0120 produces<reco::TrackExtraCollection>().setBranchAlias(alias + "TrackExtras");
0121 produces<TrackingRecHitCollection>().setBranchAlias(alias + "RecHits");
0122 }
0123 };
0124
0125 struct GsfElectronCollectionStoreManagerTrait {
0126 typedef GsfElectronCollectionStoreManager type;
0127 typedef GsfElectronSelectorBase base;
0128 };
0129
0130 }
0131
0132 template <typename Selector,
0133 typename StoreManagerTrait = ::helper::GsfElectronCollectionStoreManagerTrait,
0134 typename OutputCollection =
0135 typename ::helper::SelectedOutputCollectionTrait<reco::GsfElectronCollection>::type,
0136 typename StoreContainer = typename ::helper::StoreContainerTrait<reco::GsfElectronCollection>::type,
0137 typename PostProcessor = ::helper::NullPostProcessor<reco::GsfElectronCollection> >
0138 using GsfElectronSingleObjectSelector = SingleObjectSelectorBase<reco::GsfElectronCollection,
0139 Selector,
0140 typename StoreManagerTrait::base,
0141 OutputCollection,
0142 StoreContainer,
0143 PostProcessor,
0144 typename StoreManagerTrait::type,
0145 typename StoreManagerTrait::base>;
0146
0147 #endif