File indexing completed on 2024-04-06 12:30:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021
0022 #include <iostream>
0023 #include <memory>
0024 #include <vector>
0025
0026
0027 #include "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/stream/EDProducer.h"
0029
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/MakerMacros.h"
0032
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 #include "FWCore/Framework/interface/Event.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/Utilities/interface/InputTag.h"
0037
0038 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
0039 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0040 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0041 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0042 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
0043 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0044 #include "SimDataFormats/PileupSummaryInfo/interface/PileupMixingContent.h"
0045
0046 #include "DataFormats/Provenance/interface/ProductID.h"
0047 #include "DataFormats/Common/interface/Handle.h"
0048 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0049 #include "FWCore/Utilities/interface/EDMException.h"
0050 #include "FWCore/Utilities/interface/Algorithms.h"
0051 #include "FWCore/Framework/interface/ConstProductRegistry.h"
0052 #include "FWCore/ServiceRegistry/interface/Service.h"
0053 #include "DataFormats/Common/interface/Handle.h"
0054 #include "DataFormats/Provenance/interface/Provenance.h"
0055 #include "DataFormats/Provenance/interface/BranchDescription.h"
0056
0057 #include <string>
0058
0059 using namespace std;
0060
0061
0062
0063
0064
0065 namespace edm {
0066
0067 class HiMixingModule;
0068
0069 class HiMixingWorkerBase {
0070 public:
0071 explicit HiMixingWorkerBase() { ; }
0072
0073 HiMixingWorkerBase(std::string& object, std::vector<InputTag>& tags, std::string& label)
0074 : object_(object), tags_(tags), label_(label) {
0075 ;
0076 }
0077 virtual ~HiMixingWorkerBase() { ; }
0078
0079 virtual void addSignals(edm::Event& e) = 0;
0080
0081 std::string object_;
0082 std::vector<InputTag> tags_;
0083 std::string label_;
0084 };
0085
0086 template <class T>
0087 class HiMixingWorker : public HiMixingWorkerBase {
0088 public:
0089 HiMixingWorker(std::string& object, std::vector<InputTag>& tags, std::string& label)
0090 : HiMixingWorkerBase(object, tags, label) {
0091 ;
0092 }
0093 ~HiMixingWorker() override { ; }
0094 void addSignals(edm::Event& e) override {
0095 std::vector<Handle<std::vector<T> > > handles;
0096 bool get = true;
0097 for (size_t itag = 0; itag < tags_.size(); ++itag) {
0098 LogInfo("HiEmbedding") << "itag " << itag;
0099 LogInfo("HiEmbedding") << "label " << tags_[itag].label();
0100 LogInfo("HiEmbedding") << "instance " << tags_[itag].instance();
0101 Handle<std::vector<T> > hand;
0102 handles.push_back(hand);
0103 get = get && e.getByLabel(tags_[itag], handles[itag]);
0104 if (!get)
0105 LogWarning("Product inconsistency")
0106 << "One of the sub-events is missing the product with type " << object_ << ", instance "
0107 << tags_[itag].instance() << " whereas the other one is fine.";
0108 }
0109
0110 if (get) {
0111 std::unique_ptr<CrossingFrame<T> > crFrame(new CrossingFrame<T>());
0112 crFrame->addSignals(handles[0].product(), e.id());
0113 for (size_t itag = 1; itag < tags_.size(); ++itag) {
0114 std::vector<T>* product = const_cast<std::vector<T>*>(handles[itag].product());
0115 EncodedEventId id(0, itag);
0116 for (auto& item : *product) {
0117 item.setEventId(id);
0118 }
0119 crFrame->addPileups(*product);
0120 }
0121 e.put(std::move(crFrame), label_);
0122 }
0123 }
0124 };
0125
0126 template <>
0127 void HiMixingWorker<HepMCProduct>::addSignals(edm::Event& e) {
0128 std::vector<Handle<HepMCProduct> > handles;
0129 bool get = true;
0130 for (size_t itag = 0; itag < tags_.size(); ++itag) {
0131 Handle<HepMCProduct> hand;
0132 handles.push_back(hand);
0133 get = get && e.getByLabel(tags_[itag], handles[itag]);
0134 if (!get)
0135 LogWarning("Product inconsistency")
0136 << "One of the sub-events is missing the product with type " << object_ << ", instance "
0137 << tags_[itag].instance() << " whereas the other one is fine.";
0138 }
0139
0140 if (get) {
0141 std::unique_ptr<CrossingFrame<HepMCProduct> > crFrame(new CrossingFrame<HepMCProduct>());
0142 crFrame->addSignals(handles[0].product(), e.id());
0143 for (size_t itag = 1; itag < tags_.size(); ++itag) {
0144 HepMCProduct* product = const_cast<HepMCProduct*>(handles[itag].product());
0145 crFrame->addPileups(*product);
0146 }
0147 e.put(std::move(crFrame), label_);
0148 }
0149 }
0150
0151 class HiMixingModule : public edm::stream::EDProducer<> {
0152 public:
0153 explicit HiMixingModule(const edm::ParameterSet&);
0154 ~HiMixingModule() override;
0155
0156 private:
0157
0158 void produce(edm::Event&, const edm::EventSetup&) override;
0159
0160 bool verifyRegistry(std::string object, std::string subdet, InputTag& tag, std::string& label);
0161
0162
0163 std::vector<HiMixingWorkerBase*> workers_;
0164 };
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 HiMixingModule::HiMixingModule(const edm::ParameterSet& pset) {
0178 ParameterSet ps = pset.getParameter<ParameterSet>("mixObjects");
0179 std::vector<std::string> names = ps.getParameterNames();
0180 std::vector<std::string> simtags = pset.getParameter<std::vector<std::string> >("srcSIM");
0181 std::vector<std::string> gentags = pset.getParameter<std::vector<std::string> >("srcGEN");
0182
0183 if (simtags.size() != gentags.size())
0184 LogError("MixingInput") << "Generator and Simulation input lists are not matching each other" << endl;
0185
0186 for (std::vector<string>::iterator it = names.begin(); it != names.end(); ++it) {
0187 ParameterSet pstag = ps.getParameter<ParameterSet>((*it));
0188 if (!pstag.exists("type"))
0189 continue;
0190 std::string object = pstag.getParameter<std::string>("type");
0191 std::vector<InputTag> tags = pstag.getParameter<std::vector<InputTag> >("input");
0192
0193 std::string signal;
0194 for (size_t itag = 0; itag < tags.size(); ++itag) {
0195 InputTag tag = tags[itag];
0196 std::vector<InputTag> inputs;
0197
0198 for (size_t input = 0; input < simtags.size(); ++input) {
0199 if (object == "HepMCProduct")
0200 signal = gentags[input];
0201 else
0202 signal = simtags[input];
0203 inputs.push_back(InputTag(signal, tag.instance()));
0204 }
0205
0206 std::string label = tag.label() + tag.instance();
0207
0208 if (object == "HepMCProduct") {
0209 workers_.push_back(new HiMixingWorker<HepMCProduct>(object, inputs, label));
0210 produces<CrossingFrame<HepMCProduct> >(label);
0211 consumes<HepMCProduct>(tag);
0212 } else if (object == "SimTrack") {
0213 workers_.push_back(new HiMixingWorker<SimTrack>(object, inputs, label));
0214 produces<CrossingFrame<SimTrack> >(label);
0215 consumes<std::vector<SimTrack> >(tag);
0216 } else if (object == "SimVertex") {
0217 workers_.push_back(new HiMixingWorker<SimVertex>(object, inputs, label));
0218 produces<CrossingFrame<SimVertex> >(label);
0219 consumes<std::vector<SimVertex> >(tag);
0220 } else if (object == "PSimHit") {
0221 workers_.push_back(new HiMixingWorker<PSimHit>(object, inputs, label));
0222 produces<CrossingFrame<PSimHit> >(label);
0223 consumes<std::vector<PSimHit> >(tag);
0224 } else if (object == "PCaloHit") {
0225 workers_.push_back(new HiMixingWorker<PCaloHit>(object, inputs, label));
0226 produces<CrossingFrame<PCaloHit> >(label);
0227 consumes<std::vector<PCaloHit> >(tag);
0228 } else
0229 LogInfo("Error") << "What the hell is this object?!";
0230
0231 LogInfo("HiMixingModule") << "Will mix " << object << "s with InputTag= " << tag.encode() << ", label will be "
0232 << label;
0233 }
0234 }
0235
0236 produces<PileupMixingContent>();
0237 }
0238
0239 HiMixingModule::~HiMixingModule() {
0240
0241
0242 }
0243
0244
0245
0246
0247
0248
0249 void HiMixingModule::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0250 using namespace edm;
0251
0252 for (size_t i = 0; i < workers_.size(); ++i) {
0253 (workers_[i])->addSignals(iEvent);
0254 }
0255
0256 std::unique_ptr<PileupMixingContent> PileupMixing_ = std::make_unique<PileupMixingContent>();
0257 iEvent.put(std::move(PileupMixing_));
0258 }
0259
0260 bool HiMixingModule::verifyRegistry(std::string object, std::string subdet, InputTag& tag, std::string& label) {
0261
0262
0263
0264 edm::Service<edm::ConstProductRegistry> reg;
0265
0266 std::string lookfor;
0267 if (object == "HepMCProduct")
0268 lookfor = "edm::" + object;
0269 else if (object == "edm::HepMCProduct")
0270 lookfor = object;
0271 else
0272 lookfor = "std::vector<" + object + ">";
0273 bool found = false;
0274 for (edm::ProductRegistry::ProductList::const_iterator it = reg->productList().begin();
0275 it != reg->productList().end();
0276 ++it) {
0277
0278
0279 edm::BranchDescription desc = it->second;
0280 if (desc.className() == lookfor && desc.moduleLabel() == tag.label() &&
0281 desc.productInstanceName() == tag.instance()) {
0282 label = desc.moduleLabel() + desc.productInstanceName();
0283 found = true;
0284
0285
0286
0287
0288
0289 break;
0290 }
0291 }
0292 if (!found) {
0293 LogWarning("MixingModule") << "!!!!!!!!!Could not find in registry requested object: " << object << " with "
0294 << tag << ".\nWill NOT be considered for mixing!!!!!!!!!";
0295 return false;
0296 }
0297
0298 return true;
0299 }
0300
0301
0302 DEFINE_FWK_MODULE(HiMixingModule);
0303
0304 }