Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-21 03:43:06

0001 // -*- C++ -*-
0002 //
0003 // Package:    FFTJetTreeDump
0004 // Class:      FFTJetTreeDump
0005 //
0006 /**\class FFTJetTreeDump FFTJetTreeDump.cc RecoJets/FFTJetProducer/plugins/FFTJetTreeDump.cc
0007 
0008  Description: formats FFTJet clustering trees for subsequent visualization by OpenDX
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Igor Volobouev
0015 //         Created:  Sun Jun 20 14:32:36 CDT 2010
0016 //
0017 //
0018 
0019 #include <iostream>
0020 #include <memory>
0021 
0022 #include <fstream>
0023 #include <functional>
0024 #include <sstream>
0025 
0026 // FFTJet headers
0027 #include "fftjet/ProximityClusteringTree.hh"
0028 #include "fftjet/OpenDXPeakTree.hh"
0029 
0030 // user include files
0031 #include "FWCore/Framework/interface/Frameworkfwd.h"
0032 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0033 
0034 #include "FWCore/Framework/interface/Event.h"
0035 #include "FWCore/Framework/interface/MakerMacros.h"
0036 
0037 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0038 
0039 // parameter parser header
0040 #include "RecoJets/FFTJetProducers/interface/FFTJetParameterParser.h"
0041 
0042 // functions which manipulate storable trees
0043 #include "RecoJets/FFTJetAlgorithms/interface/clusteringTreeConverters.h"
0044 
0045 #include "DataFormats/Provenance/interface/RunLumiEventNumber.h"
0046 
0047 using namespace fftjetcms;
0048 
0049 //
0050 // class declaration
0051 //
0052 class FFTJetTreeDump : public edm::stream::EDAnalyzer<> {
0053 public:
0054   explicit FFTJetTreeDump(const edm::ParameterSet&);
0055   FFTJetTreeDump() = delete;
0056   FFTJetTreeDump(const FFTJetTreeDump&) = delete;
0057   FFTJetTreeDump& operator=(const FFTJetTreeDump&) = delete;
0058   ~FFTJetTreeDump() override;
0059 
0060 private:
0061   // Useful local typedefs
0062   typedef fftjet::ProximityClusteringTree<fftjet::Peak, long> ClusteringTree;
0063   typedef fftjet::SparseClusteringTree<fftjet::Peak, long> SparseTree;
0064   typedef fftjet::OpenDXPeakTree<long, fftjet::AbsClusteringTree> DXFormatter;
0065   typedef fftjet::OpenDXPeakTree<long, fftjet::SparseClusteringTree> SparseFormatter;
0066   typedef fftjet::Functor1<double, fftjet::Peak> PeakProperty;
0067   typedef reco::PattRecoTree<float, reco::PattRecoPeak<float> > StoredTree;
0068 
0069   void analyze(const edm::Event&, const edm::EventSetup&) override;
0070 
0071   void processTreeData(const edm::Event&, std::ofstream&);
0072 
0073   template <class Ptr>
0074   void checkConfig(const Ptr& ptr, const char* message) {
0075     if (ptr.get() == nullptr)
0076       throw cms::Exception("FFTJetBadConfig") << message << std::endl;
0077   }
0078 
0079   // ----------member data ---------------------------
0080   // The complete clustering tree
0081   ClusteringTree* clusteringTree;
0082 
0083   const edm::InputTag treeLabel;
0084   edm::EDGetTokenT<StoredTree> treeToken;
0085 
0086   const std::string outputPrefix;
0087   const double etaMax;
0088   const bool insertCompleteEvent;
0089   const double completeEventScale;
0090 
0091   // Distance calculator for the clustering tree
0092   std::unique_ptr<fftjet::AbsDistanceCalculator<fftjet::Peak> > distanceCalc;
0093 
0094   // Scales used
0095   std::unique_ptr<std::vector<double> > iniScales;
0096 
0097   // The sparse clustering tree
0098   SparseTree sparseTree;
0099 
0100   // Functors which define OpenDX glyph size and color
0101   std::unique_ptr<PeakProperty> glyphSize;
0102   std::unique_ptr<PeakProperty> glyphColor;
0103 
0104   // OpenDX formatters
0105   std::unique_ptr<DXFormatter> denseFormatter;
0106   std::unique_ptr<SparseFormatter> sparseFormatter;
0107 
0108   unsigned counter;
0109 };
0110 
0111 //
0112 // constructors and destructor
0113 //
0114 FFTJetTreeDump::FFTJetTreeDump(const edm::ParameterSet& ps)
0115     : clusteringTree(nullptr),
0116       treeLabel(ps.getParameter<edm::InputTag>("treeLabel")),
0117       outputPrefix(ps.getParameter<std::string>("outputPrefix")),
0118       etaMax(ps.getParameter<double>("etaMax")),
0119       insertCompleteEvent(ps.getParameter<bool>("insertCompleteEvent")),
0120       completeEventScale(ps.getParameter<double>("completeEventScale")),
0121       counter(0) {
0122   if (etaMax < 0.0)
0123     throw cms::Exception("FFTJetBadConfig") << "etaMax can not be negative" << std::endl;
0124 
0125   // Build the set of pattern recognition scales
0126   const edm::ParameterSet& InitialScales(ps.getParameter<edm::ParameterSet>("InitialScales"));
0127   iniScales = fftjet_ScaleSet_parser(InitialScales);
0128   checkConfig(iniScales, "invalid set of scales");
0129   std::sort(iniScales->begin(), iniScales->end(), std::greater<double>());
0130 
0131   // Distance calculator for the clustering tree
0132   const edm::ParameterSet& TreeDistanceCalculator(ps.getParameter<edm::ParameterSet>("TreeDistanceCalculator"));
0133   distanceCalc = fftjet_DistanceCalculator_parser(TreeDistanceCalculator);
0134   checkConfig(distanceCalc, "invalid tree distance calculator");
0135 
0136   // Determine representations for the OpenDX glyph size and color
0137   const edm::ParameterSet& GlyphSize(ps.getParameter<edm::ParameterSet>("GlyphSize"));
0138   glyphSize = fftjet_PeakFunctor_parser(GlyphSize);
0139   checkConfig(glyphSize, "invalid glyph size parameters");
0140 
0141   const edm::ParameterSet& GlyphColor(ps.getParameter<edm::ParameterSet>("GlyphColor"));
0142   glyphColor = fftjet_PeakFunctor_parser(GlyphColor);
0143   checkConfig(glyphColor, "invalid glyph color parameters");
0144 
0145   // Build the tree formatters
0146   denseFormatter = std::make_unique<DXFormatter>(glyphSize.get(), glyphColor.get(), etaMax);
0147   sparseFormatter = std::make_unique<SparseFormatter>(glyphSize.get(), glyphColor.get(), etaMax);
0148 
0149   // Build the clustering tree
0150   clusteringTree = new ClusteringTree(distanceCalc.get());
0151 
0152   treeToken = consumes<StoredTree>(treeLabel);
0153 }
0154 
0155 FFTJetTreeDump::~FFTJetTreeDump() { delete clusteringTree; }
0156 
0157 //
0158 // member functions
0159 //
0160 void FFTJetTreeDump::processTreeData(const edm::Event& iEvent, std::ofstream& file) {
0161   // Get the event number
0162   edm::RunNumber_t const runNum = iEvent.id().run();
0163   edm::EventNumber_t const evNum = iEvent.id().event();
0164 
0165   // Get the input
0166   edm::Handle<StoredTree> input;
0167   iEvent.getByToken(treeToken, input);
0168 
0169   const double eventScale = insertCompleteEvent ? completeEventScale : 0.0;
0170   if (input->isSparse()) {
0171     sparsePeakTreeFromStorable(*input, iniScales.get(), eventScale, &sparseTree);
0172     sparseFormatter->setTree(sparseTree, runNum, evNum);
0173     file << *sparseFormatter << std::endl;
0174   } else {
0175     densePeakTreeFromStorable(*input, iniScales.get(), eventScale, clusteringTree);
0176     denseFormatter->setTree(*clusteringTree, runNum, evNum);
0177     file << *denseFormatter << std::endl;
0178   }
0179 }
0180 
0181 // ------------ method called to for each event  ------------
0182 void FFTJetTreeDump::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0183   // Create the file name
0184   std::ostringstream filename;
0185   filename << outputPrefix << '_' << counter++ << ".dx";
0186 
0187   // Open the file
0188   std::ofstream file(filename.str().c_str());
0189   if (!file)
0190     throw cms::Exception("FFTJetBadConfig") << "Failed to open file \"" << filename.str() << "\"" << std::endl;
0191 
0192   processTreeData(iEvent, file);
0193 }
0194 
0195 //define this as a plug-in
0196 DEFINE_FWK_MODULE(FFTJetTreeDump);