Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:04

0001 // system include files
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/stream/EDProducer.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 
0013 #include "DataFormats/Candidate/interface/VertexCompositeCandidate.h"
0014 #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
0015 
0016 #include "DataFormats/VertexReco/interface/Vertex.h"
0017 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0018 
0019 //
0020 // class declaration
0021 //
0022 namespace {
0023   const float dummy = -9.;
0024   const GlobalPoint dummyGP(dummy, dummy, 0.);
0025 }  //end anonymous namespace
0026 
0027 class VertexCompositeCandidateCollectionSelector : public edm::stream::EDProducer<> {
0028 public:
0029   explicit VertexCompositeCandidateCollectionSelector(const edm::ParameterSet&);
0030 
0031   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0032 
0033 private:
0034   void produce(edm::Event&, const edm::EventSetup&) override;
0035 
0036   // ----------member data ---------------------------
0037 
0038   edm::EDGetTokenT<reco::VertexCompositeCandidateCollection> v0Token_;
0039   edm::EDGetTokenT<reco::BeamSpot> bsToken_;
0040   edm::EDGetTokenT<reco::VertexCollection> pvToken_;
0041 
0042   int pvNDOF_;
0043 
0044   std::string label_;
0045 
0046   // list of variables for the selection
0047   float lxyCUT_;
0048   float lxyWRTbsCUT_;
0049   bool debug_;
0050 };
0051 
0052 //
0053 // constants, enums and typedefs
0054 //
0055 
0056 //
0057 // static data member definitions
0058 //
0059 
0060 //
0061 // constructors and destructor
0062 //
0063 VertexCompositeCandidateCollectionSelector::VertexCompositeCandidateCollectionSelector(const edm::ParameterSet& iConfig)
0064     : v0Token_(consumes<reco::VertexCompositeCandidateCollection>(iConfig.getParameter<edm::InputTag>("v0"))),
0065       bsToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
0066       pvToken_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("primaryVertex"))),
0067       pvNDOF_(iConfig.getParameter<int>("pvNDOF")),
0068       label_(iConfig.getParameter<edm::InputTag>("v0").instance()),
0069       lxyCUT_(iConfig.getParameter<double>("lxyCUT")),
0070       lxyWRTbsCUT_(iConfig.getParameter<double>("lxyWRTbsCUT")),
0071       debug_(iConfig.getUntrackedParameter<bool>("debug")) {
0072   if (debug_)
0073     std::cout << "VertexCompositeCandidateCollectionSelector::VertexCompositeCandidateCollectionSelector" << std::endl;
0074   // product
0075   produces<reco::VertexCompositeCandidateCollection>();
0076 
0077   //now do what ever other initialization is needed
0078 }
0079 
0080 //
0081 // member functions
0082 //
0083 
0084 // ------------ method called to produce the data  ------------
0085 void VertexCompositeCandidateCollectionSelector::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0086   using namespace edm;
0087 
0088   if (debug_)
0089     std::cout << "VertexCompositeCandidateCollectionSelector::produce" << std::endl;
0090 
0091   // Create auto_ptr for each collection to be stored in the Event
0092   auto result = std::make_unique<reco::VertexCompositeCandidateCollection>();
0093 
0094   edm::Handle<reco::BeamSpot> beamspotHandle;
0095   iEvent.getByToken(bsToken_, beamspotHandle);
0096   reco::BeamSpot const* bs = nullptr;
0097   if (beamspotHandle.isValid())
0098     bs = &(*beamspotHandle);
0099 
0100   edm::Handle<reco::VertexCollection> pvHandle;
0101   iEvent.getByToken(pvToken_, pvHandle);
0102   reco::Vertex const* pv = nullptr;
0103   if (pvHandle.isValid()) {
0104     pv = &pvHandle->front();
0105     //--- pv fake (the pv collection should have size==1 and the pv==beam spot)
0106     if (pv->isFake() ||
0107         pv->tracksSize() == 0
0108         // definition of goodOfflinePrimaryVertex
0109         || pv->ndof() < pvNDOF_ || pv->z() > 24.)
0110       pv = nullptr;
0111   }
0112 
0113   edm::Handle<reco::VertexCompositeCandidateCollection> v0Handle;
0114   iEvent.getByToken(v0Token_, v0Handle);
0115   int n = (v0Handle.isValid() ? v0Handle->size() : -1);
0116   if (debug_)
0117     std::cout << "n: " << n << std::endl;
0118   if (n > 0) {
0119     auto const& v0s = *v0Handle.product();
0120     for (auto const& v0 : v0s) {
0121       GlobalPoint displacementFromPV2D =
0122           (pv == nullptr ? dummyGP : GlobalPoint((pv->x() - v0.vx()), (pv->y() - v0.vy()), 0.));
0123       GlobalPoint displacementFromBS2D =
0124           (bs == nullptr ? dummyGP : GlobalPoint(v0.vx() - bs->x(v0.vz()), v0.vy() - bs->y(v0.vz()), 0.));
0125       float abslxy = (pv == nullptr ? dummy : displacementFromPV2D.perp());
0126       float abslxyWRTbs = (bs == nullptr ? dummy : displacementFromBS2D.perp());
0127 
0128       if (debug_)
0129         std::cout << "abslxy: " << abslxy << " w.r.t. " << lxyCUT_ << " ==> " << (abslxy >= lxyCUT_ ? "OK" : "KO")
0130                   << std::endl;
0131       if (debug_)
0132         std::cout << "abslxyWRTbs: " << abslxyWRTbs << " w.r.t. " << lxyWRTbsCUT_ << " ==> "
0133                   << (abslxyWRTbs >= lxyWRTbsCUT_ ? "OK" : "KO") << std::endl;
0134       if (abslxy < lxyCUT_)
0135         continue;
0136       if (abslxyWRTbs < lxyWRTbsCUT_)
0137         continue;
0138       result->push_back(v0);
0139     }
0140   }
0141 
0142   if (debug_)
0143     std::cout << "result: " << result->size() << std::endl;
0144   // put into the Event
0145   // Write the collections to the Event
0146   result->shrink_to_fit();
0147   iEvent.put(std::move(result));
0148 }
0149 
0150 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0151 void VertexCompositeCandidateCollectionSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0152   //The following says we do not know what parameters are allowed so do no validation
0153   // Please change this to state exactly what you do use, even if it is no parameters
0154   edm::ParameterSetDescription desc;
0155   desc.add<edm::InputTag>("v0");
0156   desc.add<edm::InputTag>("beamSpot");
0157   desc.add<edm::InputTag>("primaryVertex");
0158   desc.add<int>("pvNDOF");
0159   desc.add<double>("lxyCUT", 16.);      // cm (2016 pixel layer3:10.2 cm ; 2017 pixel layer4: 16.0 cm)
0160   desc.add<double>("lxyWRTbsCUT", 0.);  // cm
0161   desc.addUntracked<bool>("debug", false);
0162   descriptions.add("VertexCompositeCandidateCollectionSelector", desc);
0163 }
0164 
0165 //define this as a plug-in
0166 DEFINE_FWK_MODULE(VertexCompositeCandidateCollectionSelector);