File indexing completed on 2024-04-06 12:24:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021
0022
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/one/EDFilter.h"
0025
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/EventSetup.h"
0028 #include "FWCore/Framework/interface/ConsumesCollector.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 #include "FWCore/ServiceRegistry/interface/Service.h"
0031 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0032
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034
0035 #include "PhysicsTools/UtilAlgos/interface/Selections.h"
0036 #include "PhysicsTools/UtilAlgos/interface/Plotter.h"
0037 #include "PhysicsTools/UtilAlgos/interface/NTupler.h"
0038 #include "CommonTools/UtilAlgos/interface/InputTagDistributor.h"
0039
0040
0041
0042
0043
0044 class ConfigurableAnalysis : public edm::one::EDFilter<edm::one::SharedResources> {
0045 public:
0046 explicit ConfigurableAnalysis(const edm::ParameterSet&);
0047
0048 private:
0049 bool filter(edm::Event&, const edm::EventSetup&) override;
0050 void endJob() override;
0051
0052 std::unique_ptr<FilterSelections> selections_;
0053 std::unique_ptr<Plotter> plotter_;
0054 std::unique_ptr<NTupler> ntupler_;
0055
0056 std::vector<std::string> flows_;
0057 bool workAsASelector_;
0058 };
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 ConfigurableAnalysis::ConfigurableAnalysis(const edm::ParameterSet& iConfig) {
0072 usesResource(TFileService::kSharedResource);
0073
0074 std::string moduleLabel = iConfig.getParameter<std::string>("@module_label");
0075
0076
0077 if (iConfig.exists("InputTags"))
0078 edm::Service<InputTagDistributorService>()->init(
0079 moduleLabel, iConfig.getParameter<edm::ParameterSet>("InputTags"), consumesCollector());
0080
0081
0082 edm::Service<VariableHelperService>()->init(
0083 moduleLabel, iConfig.getParameter<edm::ParameterSet>("Variables"), consumesCollector());
0084
0085
0086 selections_ =
0087 std::make_unique<FilterSelections>(iConfig.getParameter<edm::ParameterSet>("Selections"), consumesCollector());
0088
0089
0090 edm::ParameterSet plotPset = iConfig.getParameter<edm::ParameterSet>("Plotter");
0091 if (!plotPset.empty()) {
0092 std::string plotterName = plotPset.getParameter<std::string>("ComponentName");
0093 plotter_ = PlotterFactory::get()->create(plotterName, plotPset);
0094 }
0095
0096
0097 edm::ParameterSet ntPset = iConfig.getParameter<edm::ParameterSet>("Ntupler");
0098 if (!ntPset.empty()) {
0099 std::string ntuplerName = ntPset.getParameter<std::string>("ComponentName");
0100 ntupler_ = NTuplerFactory::get()->create(ntuplerName, ntPset);
0101 }
0102
0103 flows_ = iConfig.getParameter<std::vector<std::string>>("flows");
0104 workAsASelector_ = iConfig.getParameter<bool>("workAsASelector");
0105
0106
0107 produces<std::vector<bool>>();
0108
0109
0110 if (ntupler_)
0111 ntupler_->registerleaves(producesCollector());
0112 }
0113
0114
0115
0116
0117
0118
0119 bool ConfigurableAnalysis::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0120
0121 bool majorGlobalAccept = false;
0122
0123 auto passedProduct = std::make_unique<std::vector<bool>>(flows_.size(), false);
0124 bool filledOnce = false;
0125
0126
0127 for (FilterSelections::iterator selection = selections_->begin(); selection != selections_->end(); ++selection) {
0128
0129 bool skip = true;
0130 unsigned int iFlow = 0;
0131 for (; iFlow != flows_.size(); ++iFlow) {
0132 if (flows_[iFlow] == selection->name()) {
0133 skip = false;
0134 break;
0135 }
0136 }
0137 if (skip)
0138 continue;
0139
0140
0141 if (plotter_)
0142 plotter_->setDir(selection->name());
0143
0144
0145 std::map<std::string, bool> accept = selection->acceptMap(iEvent);
0146
0147 bool globalAccept = true;
0148 std::string separator = "";
0149 std::string cumulative = "";
0150 std::string allButOne = "allBut_";
0151 std::string fullAccept = "fullAccept";
0152 std::string fullContent = "fullContent";
0153
0154 if (selection->makeContentPlots() && plotter_)
0155 plotter_->fill(fullContent, iEvent);
0156
0157
0158 for (FilterSelection::iterator filterIt = selection->begin(); filterIt != selection->end(); ++filterIt) {
0159 SFilter& filter = (*filterIt);
0160
0161
0162
0163 cumulative += separator;
0164 if (filter.inverted())
0165 cumulative += "not";
0166 cumulative += filter->name();
0167 separator = "_";
0168
0169 if (accept[filter->name()]) {
0170
0171 if (globalAccept && selection->makeCumulativePlots() && plotter_)
0172 plotter_->fill(cumulative, iEvent);
0173 } else {
0174 globalAccept = false;
0175
0176 bool goodForAllButThisOne = true;
0177 for (std::map<std::string, bool>::iterator decision = accept.begin(); decision != accept.end(); ++decision) {
0178 if (decision->first == filter->name())
0179 continue;
0180 if (!decision->second) {
0181 goodForAllButThisOne = false;
0182 break;
0183 }
0184 }
0185 if (goodForAllButThisOne && selection->makeAllButOnePlots() && plotter_) {
0186 plotter_->fill(allButOne + filter->name(), iEvent);
0187 }
0188 }
0189
0190 }
0191
0192 if (globalAccept) {
0193 (*passedProduct)[iFlow] = true;
0194 majorGlobalAccept = true;
0195
0196 if (selection->makeFinalPlots() && !selection->makeCumulativePlots() && plotter_)
0197 plotter_->fill(fullAccept, iEvent);
0198
0199
0200 if (selection->ntuplize() && !filledOnce && ntupler_) {
0201 ntupler_->fill(iEvent);
0202 filledOnce = true;
0203 }
0204 }
0205
0206 }
0207
0208 iEvent.put(std::move(passedProduct));
0209 if (workAsASelector_)
0210 return majorGlobalAccept;
0211 else
0212 return true;
0213 }
0214
0215
0216 void ConfigurableAnalysis::endJob() {
0217
0218 selections_->print();
0219 if (plotter_)
0220 plotter_->complete();
0221 }
0222
0223 DEFINE_FWK_MODULE(ConfigurableAnalysis);