Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-22 02:30:55

0001 #include <memory>
0002 
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryPlugin.h"
0005 
0006 #include <TString.h>
0007 #include <TObjArray.h>
0008 
0009 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryBase.h"
0010 
0011 /// A factory that can combine the functionality of several 'trajectory factories'. At construction
0012 /// time, it is given an ordered list of what kinds factories it should use. When called, all the
0013 /// factories are called one after each other,
0014 ///  - either until one of them gives a result
0015 ///  - or until all factories are really called.
0016 /// This is determined by the useAllFactories flag.
0017 ///
0018 /// Example:
0019 /// Combine TwoBodyDecayTrajectoryFactory and ReferenceTrajectoryFactory
0020 /// with useAllFactories = false. In case the former
0021 /// can't produce a trajectory from two given tracks, the tracks can still be used for 'ordinary'
0022 /// reference trajectories (see also TrajectoryFactories.cff).
0023 
0024 class CombinedTrajectoryFactory : public TrajectoryFactoryBase {
0025 public:
0026   CombinedTrajectoryFactory(const edm::ParameterSet &config, edm::ConsumesCollector &iC);
0027   ~CombinedTrajectoryFactory() override;
0028 
0029   const ReferenceTrajectoryCollection trajectories(const edm::EventSetup &setup,
0030                                                    const ConstTrajTrackPairCollection &tracks,
0031                                                    const reco::BeamSpot &beamSpot) const override;
0032 
0033   const ReferenceTrajectoryCollection trajectories(const edm::EventSetup &setup,
0034                                                    const ConstTrajTrackPairCollection &tracks,
0035                                                    const ExternalPredictionCollection &external,
0036                                                    const reco::BeamSpot &beamSpot) const override;
0037 
0038   CombinedTrajectoryFactory *clone() const override { return new CombinedTrajectoryFactory(*this); }
0039 
0040 private:
0041   CombinedTrajectoryFactory(const CombinedTrajectoryFactory &other)
0042       : TrajectoryFactoryBase(other), theUseAllFactories{other.theUseAllFactories} {
0043     theFactories.reserve(other.theFactories.size());
0044     for (const auto &f : other.theFactories) {
0045       theFactories.emplace_back(f->clone());
0046     }
0047   }
0048 
0049   std::vector<std::unique_ptr<TrajectoryFactoryBase>> theFactories;
0050   bool theUseAllFactories;  /// use not only the first 'successful'?
0051 };
0052 
0053 //////////////////////////////////////////////////////////////////////////////////////
0054 //////////////////////////////////////////////////////////////////////////////////////
0055 //////////////////////////////////////////////////////////////////////////////////////
0056 
0057 using namespace std;
0058 
0059 CombinedTrajectoryFactory::CombinedTrajectoryFactory(const edm::ParameterSet &config, edm::ConsumesCollector &iC)
0060     : TrajectoryFactoryBase(config, iC), theUseAllFactories(config.getParameter<bool>("useAllFactories")) {
0061   vector<string> factoryNames = config.getParameter<vector<string>>("TrajectoryFactoryNames");
0062   for (auto const &factoryName : factoryNames) {
0063     // auto_ptr to avoid missing a delete due to throw...
0064     std::unique_ptr<TObjArray> namePset(TString(factoryName.c_str()).Tokenize(","));
0065     if (namePset->GetEntriesFast() != 2) {
0066       throw cms::Exception("BadConfig") << "@SUB=CombinedTrajectoryFactory"
0067                                         << "TrajectoryFactoryNames must contain 2 comma "
0068                                         << "separated strings, but is '" << factoryName << "'";
0069     }
0070     const edm::ParameterSet factoryCfg = config.getParameter<edm::ParameterSet>(namePset->At(1)->GetName());
0071     theFactories.emplace_back(TrajectoryFactoryPlugin::get()->create(namePset->At(0)->GetName(), factoryCfg, iC));
0072   }
0073 }
0074 
0075 CombinedTrajectoryFactory::~CombinedTrajectoryFactory(void) {}
0076 
0077 const CombinedTrajectoryFactory::ReferenceTrajectoryCollection CombinedTrajectoryFactory::trajectories(
0078     const edm::EventSetup &setup, const ConstTrajTrackPairCollection &tracks, const reco::BeamSpot &beamSpot) const {
0079   ReferenceTrajectoryCollection trajectories;
0080   ReferenceTrajectoryCollection tmpTrajectories;  // outside loop for efficiency
0081 
0082   for (auto const &factory : theFactories) {
0083     tmpTrajectories = factory->trajectories(setup, tracks, beamSpot);
0084     trajectories.insert(trajectories.end(), tmpTrajectories.begin(), tmpTrajectories.end());
0085 
0086     if (!theUseAllFactories && !trajectories.empty())
0087       break;
0088   }
0089 
0090   return trajectories;
0091 }
0092 
0093 const CombinedTrajectoryFactory::ReferenceTrajectoryCollection CombinedTrajectoryFactory::trajectories(
0094     const edm::EventSetup &setup,
0095     const ConstTrajTrackPairCollection &tracks,
0096     const ExternalPredictionCollection &external,
0097     const reco::BeamSpot &beamSpot) const {
0098   ReferenceTrajectoryCollection trajectories;
0099   ReferenceTrajectoryCollection tmpTrajectories;  // outside loop for efficiency
0100 
0101   for (auto const &factory : theFactories) {
0102     tmpTrajectories = factory->trajectories(setup, tracks, external, beamSpot);
0103     trajectories.insert(trajectories.end(), tmpTrajectories.begin(), tmpTrajectories.end());
0104 
0105     if (!theUseAllFactories && !trajectories.empty())
0106       break;
0107   }
0108 
0109   return trajectories;
0110 }
0111 
0112 DEFINE_EDM_PLUGIN(TrajectoryFactoryPlugin, CombinedTrajectoryFactory, "CombinedTrajectoryFactory");