From 884dc84cf36c5578e59c7300e2f63c11403f07b2 Mon Sep 17 00:00:00 2001 From: Kenneth Long Date: Fri, 17 Jan 2020 21:20:32 +0100 Subject: [PATCH] Remove test analyzers --- .../Core/plugins/GenWeightsTestAnalyzer.cc | 456 ------------------ .../testGenWeights_simpleZanalyzer_cfg.py | 78 --- 2 files changed, 534 deletions(-) delete mode 100644 GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc delete mode 100644 GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py diff --git a/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc b/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc deleted file mode 100644 index 492bdcd6a028f..0000000000000 --- a/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc +++ /dev/null @@ -1,456 +0,0 @@ -// -*- C++ -*- -// -// Package: GenAnalysis/GenWeightsTestAnalyzer -// Class: GenWeightsTestAnalyzer -// -/**\class GenWeightsTestAnalyzer GenWeightsTestAnalyzer.cc GenAnalysis/GenWeightsTestAnalyzer/plugins/GenWeightsTestAnalyzer.cc - - Description: [one line class summary] - - Implementation: - [Notes on implementation] -*/ -// -// Original Author: Andreas Artur Eugen Albert -// Created: Fri, 21 Jul 2017 11:42:52 GMT -// -// - - -// system include files -#include -#include // std::pair -// user include files -#include -#include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/one/EDAnalyzer.h" - -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/Run.h" -#include "FWCore/Framework/interface/MakerMacros.h" - -#include "FWCore/ParameterSet/interface/ParameterSet.h" - -#include "DataFormats/HepMCCandidate/interface/GenParticle.h" -#include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h" -#include "DataFormats/METReco/interface/GenMET.h" -#include "DataFormats/METReco/interface/GenMETCollection.h" -#include "DataFormats/JetReco/interface/GenJetCollection.h" - -#include "FWCore/ServiceRegistry/interface/Service.h" -#include "CommonTools/UtilAlgos/interface/TFileService.h" - -#include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h" -#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h" - -#include "SimDataFormats/GeneratorProducts/interface/GenWeightInfoProduct.h" -#include "SimDataFormats/GeneratorProducts/interface/GenWeightProduct.h" - -#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h" -#include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h" -#include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h" - -#include "Math/VectorUtil.h" - -#include -#include -#include -#include -// -// class declaration -// - -// If the analyzer does not use TFileService, please remove -// the template argument to the base class so the class inherits -// from edm::one::EDAnalyzer<> and also remove the line from -// constructor "usesResource("TFileService");" -// This will improve performance in multithreaded jobs. - -class GenWeightsTestAnalyzer : public edm::one::EDAnalyzer { - public: - explicit GenWeightsTestAnalyzer(const edm::ParameterSet&); - ~GenWeightsTestAnalyzer(); - - static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); - - - private: - virtual void beginJob() override; - virtual void beginLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) override; - virtual void endLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) override {} - virtual void analyze(const edm::Event&, const edm::EventSetup&) override; - virtual void endJob() override; - - void setup_variables(const edm::Event& iEvent); - std::vector setup_weights(const edm::Event& iEvent); - - // ----------member data --------------------------- - std::string tag_; - bool isMiniaod_; - std::vector scaleWeightsOrder_; - int scaleWeightsIndex_; - int pdfWeightsIndex_; - - edm::EDGetTokenT genParticleToken_; - edm::EDGetTokenT genJetToken_; - edm::EDGetTokenT LHEToken_; - edm::EDGetTokenT GenToken_; - edm::EDGetTokenT lheWeightToken_; - edm::EDGetTokenT genWeightToken_; - edm::EDGetTokenT lheWeightInfoToken_; - edm::Service fileservice; - - std::map histograms2d; - std::map histograms1d; - - TTree * tree; - std::map variables; - - TNamed * filelist; - - TH1D * h_count_event; - TH1D * h_count_sumw; - - const unsigned int KEEP_LHAPDFID_ = 23000; - int keepPdfSetIndex_ = 0; - int keepPdfIndexInSet_ = -1; -}; - -// -// constants, enums and typedefs -// - -// -// static data member definitions -// - -// -// constructors and destructor -// -GenWeightsTestAnalyzer::GenWeightsTestAnalyzer(const edm::ParameterSet& iConfig) : - tag_(iConfig.getParameter("tag")), - isMiniaod_(iConfig.getParameter("miniaod")), - scaleWeightsIndex_(-1), - pdfWeightsIndex_(-1), - genParticleToken_(consumes(iConfig.getParameter("genParticleSrc"))), - genJetToken_(consumes(iConfig.getParameter("genJetSrc"))), - LHEToken_(consumes(iConfig.getParameter("LHESrc"))), - GenToken_(consumes(iConfig.getParameter("GenSrc"))), - lheWeightToken_(consumes(edm::InputTag("testLheWeights"))), - genWeightToken_(consumes(edm::InputTag("testGenWeights"))), - lheWeightInfoToken_(consumes(edm::InputTag("testLheWeights"))) - -{ - //now do what ever initialization is needed - //usesResource("TFileService"); - TFileDirectory subdirectory = fileservice->mkdir( tag_ ); - - h_count_event = subdirectory.make("h_count_event", "h_count_event;Dummy;Events", 1,0,1); - h_count_sumw = subdirectory.make("h_count_sumw", "h_count_sumw;Weight index;Events", 1,0,1); - - int nweights = 1200; - histograms2d["pt_jet1"] = subdirectory.make("pt_jet1", "Leading jet pt;pt_{jet};weight;Events",200,0,1000, nweights, -0.5, nweights-0.5); - histograms2d["pt_jet2"] = subdirectory.make("pt_jet2", "Trailing jet pt;pt_{jet};weight;Events",200,0,1000, nweights, -0.5, nweights-0.5); - histograms2d["pt_jet3"] = subdirectory.make("pt_jet3", "3rd jet pt;pt_{jet};weight;Events",200,0,1000, nweights, -0.5, nweights-0.5); - histograms2d["pt_jet4"] = subdirectory.make("pt_jet4", "4th jet pt;pt_{jet};weight;Events",200,0,1000, nweights, -0.5, nweights-0.5); - histograms2d["ht"] = subdirectory.make("ht", "HT;HT (GeV);weight;Events",200,0,2000, nweights, -0.5, nweights-0.5); - histograms2d["dilepton_pt"] = subdirectory.make("dilepton_pt", "Dilepton p_{T}; p_{T} (GeV);weight;Events",200,0,2000, nweights, -0.5, nweights-0.5); - histograms2d["n_leptons"] = subdirectory.make("n_leptons", "number of leptons;n_lep;weight;Events",10,-0.5,9.5, nweights, -0.5, nweights-0.5); - - tree = subdirectory.make("events","events"); - for(auto const entry : histograms1d){ - tree->Branch(entry.first, &variables[entry.first],(entry.first+"/D").Data()); - } - filelist = subdirectory.make(tag_,tag_); -} - - -GenWeightsTestAnalyzer::~GenWeightsTestAnalyzer() -{ - - // do anything here that needs to be done at desctruction time - // (e.g. close files, deallocate resources etc.) - -} - - -// -// member functions -// -bool compare_pt(reco::GenParticle p1, reco::GenParticle p2){ - return p2.pt() < p1.pt(); -} -std::vector select_leptons(const std::vector genParticles) { - std::vector leptons; - for (auto const p : genParticles){ - unsigned const int id = abs(p.pdgId()); - if(p.numberOfDaughters() > 0) continue; - else if(p.pt()<20) continue; - else if(not ((id==11)||(id==13)) ) continue; - else leptons.push_back(p); - } - std::sort(leptons.begin(), leptons.end(), compare_pt ); - return leptons; -} -std::vector select_neutrinos(const std::vector genParticles) { - std::vector neutrinos; - for (auto const p : genParticles){ - unsigned const int id = abs(p.pdgId()); - if(p.pt() < 25) continue; - else if(p.numberOfDaughters() > 0) continue; - else if(not ((id==12)||(id==14)||(id==16)) ) continue; - else neutrinos.push_back(p); - } - std::sort(neutrinos.begin(), neutrinos.end(), compare_pt ); - return neutrinos; -} - -std::vector select_jets(const std::vector genJets) { - std::vector jets; - for (auto const p : genJets){ - if(fabs(p.pt()) < 10) continue; - else jets.push_back(p); - } - std::sort(jets.begin(), jets.end(), compare_pt ); - return jets; -} - -std::pair find_z_candidate(std::vector & genParticles) { - double delta_mass_min = 9999; - double m_z = 91; - reco::GenParticle p1,p2; - for( unsigned int i = 0; i < genParticles.size(); i++ ) { - for( unsigned int j = i+1; j < genParticles.size(); j++ ) { - auto candidate_p4 = genParticles.at(i).p4() + genParticles.at(j).p4(); - double dm = fabs(candidate_p4.mass()-m_z); - if(dm < delta_mass_min ) { - delta_mass_min = dm; - p1 = genParticles.at(i); - p2 = genParticles.at(j); - } - } - } - return std::make_pair(p1,p2); -} - -std::vector clean_jets(const std::vector genJets, const std::vector leptons){ - std::vector cleaned_jets; - cleaned_jets.reserve(genJets.size()); - for( auto const jet : genJets ) { - double delta_r_min = 999; - double eta = jet.eta(); - double phi = jet.phi(); - for( auto const lep : leptons ) { - double delta_r = pow((pow(lep.eta() - eta,2) + pow(lep.phi() - phi,2)),0.5); - if (delta_r < delta_r_min){ - delta_r_min = delta_r; - } - } - if(delta_r_min < 0.4) { - continue; - } else { - cleaned_jets.push_back(jet); - } - } - return cleaned_jets; -} -void print_variables(std::map variables) { - -} -double delta_phi(reco::Candidate::LorentzVector const & p1,reco::Candidate::LorentzVector const & p2) { - double dphi_raw = fabs(p1.phi() - p2.phi()); - if(dphi_raw > M_PI){ - return 2*M_PI - dphi_raw; - } else { - return dphi_raw; - } -} -//// Parse generator history of particle and look for -bool has_v_in_history(reco::GenParticle const & part){ - size_t nmothers = part.numberOfMothers(); - if(nmothers == 0) { - return false; - } else { - for( size_t im = 0; im < nmothers; im++ ){ - auto const & mother = *(part.motherRef(im)); - - if(mother.pdgId() == 23 or mother.pdgId() == 24) { - return true; - } else if( has_v_in_history(*(part.motherRef(im))) ) { - return true; - } - } - return false; - } -} - -void GenWeightsTestAnalyzer::setup_variables(const edm::Event& iEvent) { - using namespace edm; - - //// Initialize with dummy values - for ( auto pair : variables ) { - variables[pair.first] = -9999; - } - - Handle> genParticleHandle; - iEvent.getByToken(genParticleToken_, genParticleHandle); - const std::vector * genParticles = genParticleHandle.product(); - - Handle> genJetHandle; - iEvent.getByToken(genJetToken_, genJetHandle); - const std::vector * genJets = genJetHandle.product(); - - edm::Handle lheHandle; - iEvent.getByLabel("externalLHEProducer", lheHandle); - const LHEEventProduct * lhe = lheHandle.product(); - - - - - variables["weight"] = lhe->originalXWGTUP(); - - //~ //// Leptons - std::vector leptons = select_leptons(*genParticles); - variables["n_leptons"] = leptons.size(); - - if(leptons.size() >= 2) { - auto z_cand = find_z_candidate(leptons); - variables["dilepton_pt"] = (z_cand.first.p4() + z_cand.second.p4()).pt(); - } - else - variables["dilepton_pt"] = 0; - - //// Jets - std::vector jets = clean_jets(select_jets(*genJets),leptons); - int njets = jets.size(); - variables["n_jets"] = njets; - variables["pt_jet1"] = njets > 0 ? jets.at(0).pt() : -999; - variables["pt_jet2"] = njets > 1 ? jets.at(1).pt() : -999; - variables["pt_jet3"] = njets > 2 ? jets.at(2).pt() : -999; - variables["pt_jet4"] = njets > 3 ? jets.at(3).pt() : -999; - - double ht = 0; - for(auto const & j : jets){ - ht += j.pt(); - } - variables["ht"] = ht; - -} -std::vector GenWeightsTestAnalyzer::setup_weights(const edm::Event& iEvent) { - edm::Handle lheWeightHandle; - edm::Handle genWeightHandle; - iEvent.getByToken(lheWeightToken_, lheWeightHandle); - iEvent.getByToken(genWeightToken_, genWeightHandle); - const GenWeightProduct * lheWeightProduct = lheWeightHandle.product(); - const GenWeightProduct * genWeightProduct = genWeightHandle.product(); - WeightsContainer lheWeights = lheWeightProduct->weights(); - WeightsContainer genWeights = genWeightProduct->weights(); - - auto scaleWeights = scaleWeightsIndex_ >= 0 ? lheWeights.at(scaleWeightsIndex_) : std::vector(); - auto pdfWeights = pdfWeightsIndex_ > 0 ? lheWeights.at(pdfWeightsIndex_) : std::vector(); - std::vector keepWeights; - - for(auto i : scaleWeightsOrder_){ - keepWeights.push_back(scaleWeights.at(i)); - } - - for(auto& weight : pdfWeights){ - keepWeights.push_back(weight); - } - - if (genWeights.size() > 0) { - for(auto& weight : genWeights.front()){ - keepWeights.push_back(weight); - } - } - - return keepWeights; -} - - -// ------------ method called for each event ------------ -void -GenWeightsTestAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) -{ - using namespace edm; - - setup_variables(iEvent); - tree->Fill(); - auto weights = setup_weights(iEvent); - h_count_event->Fill(0.5); - h_count_sumw->Fill(0.5,weights.at(4)); - - for( auto pair : histograms2d ) { - for(uint i=0; i -9998){ - pair.second->Fill(value,i,weights.at(i)); - } - } - } -} - - -// ------------ method called once each job just before starting event loop ------------ -void -GenWeightsTestAnalyzer::beginJob() -{ -} - -// ------------ method called once each job just after ending the event loop ------------ -void -GenWeightsTestAnalyzer::endJob() -{ -} - -// ------------ method fills 'descriptions' with the allowed parameters for the module ------------ -void -GenWeightsTestAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - //The following says we do not know what parameters are allowed so do no validation - // Please change this to state exactly what you do use, even if it is no parameters - edm::ParameterSetDescription desc; - desc.setUnknown(); - descriptions.addDefault(desc); -} - -void -GenWeightsTestAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const& es) { - //edm::Handle lheWeightsInfoHandle; - //edm::Handle lheWeightsInfoHandle; - //run.getByLabel("generator", lheWeightsInfoHandle); - edm::Handle lheWeightInfoHandle; - iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle); - - // Should add a search by name function - auto allScaleWeights = lheWeightInfoHandle->weightGroupIndicesByType(gen::WeightType::kScaleWeights); - if (allScaleWeights.size() > 0) - scaleWeightsIndex_ = allScaleWeights.front(); - - auto scaleWeights = static_cast( - lheWeightInfoHandle->orderedWeightGroupInfo(scaleWeightsIndex_)); - // nano ordering of mur=0.5 muf=0.5 ; [1] is mur=0.5 muf=1 ; [2] is mur=0.5 muf=2 ; [3] is mur=1 muf=0.5 ; - // [4] is mur=1 muf=1 ; [5] is mur=1 muf=2 ; [6] is mur=2 muf=0.5 ; [7] is mur=2 muf=1 ; [8] is mur=2 muf=2 * - scaleWeightsOrder_.clear(); - scaleWeightsOrder_.push_back(scaleWeights->muR05muF05Index()); - scaleWeightsOrder_.push_back(scaleWeights->muR05muF1Index()); - scaleWeightsOrder_.push_back(scaleWeights->muR05muF2Index()); - scaleWeightsOrder_.push_back(scaleWeights->muR1muF05Index()); - scaleWeightsOrder_.push_back(scaleWeights->centralIndex()); - scaleWeightsOrder_.push_back(scaleWeights->muR1muF2Index()); - scaleWeightsOrder_.push_back(scaleWeights->muR2muF05Index()); - scaleWeightsOrder_.push_back(scaleWeights->muR2muF1Index()); - scaleWeightsOrder_.push_back(scaleWeights->muR2muF2Index()); - - auto pdfGroups = lheWeightInfoHandle->weightGroupsByType(gen::WeightType::kPdfWeights); - auto ct14Set = std::find_if(pdfGroups.begin(), pdfGroups.end(), - [] (gen::WeightGroupInfo* group) { - auto pdfGroup = dynamic_cast(group); - return pdfGroup->containsLhapdfId(1300); - }); - if (ct14Set != pdfGroups.end()) - pdfWeightsIndex_ = std::distance(pdfGroups.begin(), ct14Set); -} - -//define this as a plug-in -DEFINE_FWK_MODULE(GenWeightsTestAnalyzer); - diff --git a/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py b/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py deleted file mode 100644 index 4e364b0aa2557..0000000000000 --- a/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py +++ /dev/null @@ -1,78 +0,0 @@ -import FWCore.ParameterSet.Config as cms -import sys -import os -import re -from FWCore.ParameterSet.VarParsing import VarParsing - -def commandline(): - options = VarParsing('analysis') - - options.register( - "fileList", - "", - VarParsing.multiplicity.singleton, - VarParsing.varType.string, - "List of files to run on." - ) - options.register( - "isMiniAOD", - False, - VarParsing.multiplicity.singleton, - VarParsing.varType.bool, - "Sample is miniAOD" - ) - options.register( - "path", - None, - VarParsing.multiplicity.singleton, - VarParsing.varType.string, - "Path for input files" - ) - - options.parseArguments() - - - print "OPTIONS: MiniAOD ---> " + ("Yes" if options.isMiniAOD else "No") - print "OPTIONS: Tag ---> {TAG}".format(TAG=options.tag) - - - if(len(options.inputFiles) and options.fileList != ""): - print "ERROR: Please provide either fileList or inputFiles but not both." - sys.exit(1) - elif(options.fileList != ""): - options.inputFiles.extend(read_list_from_file(options.fileList)) - elif(options.path): - options.inputFiles.extend(find_files(options.path,'.*\.root')) - print "Found {N} files.".format(N=len(options.inputFiles)) - - return options -# Define the CMSSW process -process = cms.Process("demo") -options = commandline() - -process.load("FWCore.MessageService.MessageLogger_cfi") -process.MessageLogger.cerr.FwkReport.reportEvery = 1000 - -process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(options.maxEvents) ) - -process.source = cms.Source("PoolSource", - fileNames = cms.untracked.vstring( options.inputFiles ) -) - -process.testLheWeights = cms.EDProducer("LHEWeightProductProducer") -process.testGenWeights = cms.EDProducer("GenWeightProductProducer") - -process.demo = cms.EDAnalyzer('GenWeightsTestAnalyzer', - tag = cms.string(options.tag), - miniaod = cms.bool(options.isMiniAOD), - genParticleSrc = cms.InputTag("prunedGenParticles" if options.isMiniAOD else "genParticles"), - genJetSrc = cms.InputTag( "slimmedGenJets" if options.isMiniAOD else "ak4GenJetsNoNu"), - genMETSrc = cms.InputTag( "slimmedMETs" if options.isMiniAOD else "genMetTrue"), - LHESrc = cms.InputTag("externalLHEProducer"), - GenSrc = cms.InputTag("generator") -) - -process.TFileService = cms.Service("TFileService", fileName = cms.string("analysis_{TAG}.root".format(TAG=options.tag)) ) - -process.p = cms.Path(process.testLheWeights*process.testGenWeights*process.demo) -