CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/RecoJets/FFTJetProducers/plugins/FFTJetTreeDump.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    FFTJetTreeDump
00004 // Class:      FFTJetTreeDump
00005 // 
00013 //
00014 // Original Author:  Igor Volobouev
00015 //         Created:  Sun Jun 20 14:32:36 CDT 2010
00016 // $Id: FFTJetTreeDump.cc,v 1.2 2010/12/07 00:19:43 igv Exp $
00017 //
00018 //
00019 
00020 #include <iostream>
00021 #include <sstream>
00022 #include <fstream>
00023 #include <functional>
00024 
00025 // FFTJet headers
00026 #include "fftjet/ProximityClusteringTree.hh"
00027 #include "fftjet/OpenDXPeakTree.hh"
00028 
00029 // user include files
00030 #include "FWCore/Framework/interface/Frameworkfwd.h"
00031 #include "FWCore/Framework/interface/EDAnalyzer.h"
00032 
00033 #include "FWCore/Framework/interface/Event.h"
00034 #include "FWCore/Framework/interface/MakerMacros.h"
00035 
00036 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00037 
00038 // parameter parser header
00039 #include "RecoJets/FFTJetProducers/interface/FFTJetParameterParser.h"
00040 
00041 // functions which manipulate storable trees
00042 #include "RecoJets/FFTJetAlgorithms/interface/clusteringTreeConverters.h"
00043 
00044 using namespace fftjetcms;
00045 
00046 //
00047 // class declaration
00048 //
00049 class FFTJetTreeDump : public edm::EDAnalyzer
00050 {
00051 public:
00052     explicit FFTJetTreeDump(const edm::ParameterSet&);
00053     ~FFTJetTreeDump();
00054 
00055 private:
00056     // Useful local typedefs
00057     typedef fftjet::ProximityClusteringTree<fftjet::Peak,long> ClusteringTree;
00058     typedef fftjet::SparseClusteringTree<fftjet::Peak,long> SparseTree;
00059     typedef fftjet::OpenDXPeakTree<long,fftjet::AbsClusteringTree> DXFormatter;
00060     typedef fftjet::OpenDXPeakTree<long,fftjet::SparseClusteringTree> SparseFormatter;
00061     typedef fftjet::Functor1<double,fftjet::Peak> PeakProperty;
00062 
00063     FFTJetTreeDump();
00064     FFTJetTreeDump(const FFTJetTreeDump&);
00065     FFTJetTreeDump& operator=(const FFTJetTreeDump&);
00066 
00067     virtual void beginJob() ;
00068     virtual void analyze(const edm::Event&, const edm::EventSetup&);
00069     virtual void endJob() ;
00070 
00071     template<class Real>
00072     void processTreeData(const edm::Event&, std::ofstream&);
00073 
00074     template<class Ptr>
00075     void checkConfig(const Ptr& ptr, const char* message)
00076     {
00077         if (ptr.get() == NULL)
00078             throw cms::Exception("FFTJetBadConfig") << message << std::endl;
00079     }
00080 
00081     // ----------member data ---------------------------
00082     // The complete clustering tree
00083     ClusteringTree* clusteringTree;
00084 
00085     const edm::InputTag treeLabel;
00086     const std::string outputPrefix;
00087     const double etaMax;
00088     const bool storeInSinglePrecision;
00089     const bool insertCompleteEvent;
00090     const double completeEventScale;
00091 
00092     // Distance calculator for the clustering tree
00093     std::auto_ptr<fftjet::AbsDistanceCalculator<fftjet::Peak> > distanceCalc;
00094 
00095     // Scales used
00096     std::auto_ptr<std::vector<double> > iniScales;
00097 
00098     // The sparse clustering tree
00099     SparseTree sparseTree;
00100 
00101     // Functors which define OpenDX glyph size and color
00102     std::auto_ptr<PeakProperty> glyphSize;
00103     std::auto_ptr<PeakProperty> glyphColor;
00104 
00105     // OpenDX formatters
00106     std::auto_ptr<DXFormatter> denseFormatter;
00107     std::auto_ptr<SparseFormatter> sparseFormatter;
00108 
00109     unsigned counter;
00110 };
00111 
00112 //
00113 // constructors and destructor
00114 //
00115 FFTJetTreeDump::FFTJetTreeDump(const edm::ParameterSet& ps)
00116     : clusteringTree(0),
00117       treeLabel(ps.getParameter<edm::InputTag>("treeLabel")),
00118       outputPrefix(ps.getParameter<std::string>("outputPrefix")),
00119       etaMax(ps.getParameter<double>("etaMax")),
00120       storeInSinglePrecision(true),
00121       insertCompleteEvent(ps.getParameter<bool>("insertCompleteEvent")),
00122       completeEventScale(ps.getParameter<double>("completeEventScale")),
00123       counter(0)
00124 {
00125     if (etaMax < 0.0)
00126         throw cms::Exception("FFTJetBadConfig")
00127             << "etaMax can not be negative" << std::endl;
00128 
00129     // Build the set of pattern recognition scales
00130     const edm::ParameterSet& InitialScales(
00131         ps.getParameter<edm::ParameterSet>("InitialScales"));
00132     iniScales = fftjet_ScaleSet_parser(InitialScales);
00133     checkConfig(iniScales, "invalid set of scales");
00134     std::sort(iniScales->begin(), iniScales->end(), std::greater<double>());
00135 
00136     // Distance calculator for the clustering tree
00137     const edm::ParameterSet& TreeDistanceCalculator(
00138         ps.getParameter<edm::ParameterSet>("TreeDistanceCalculator"));
00139     distanceCalc = fftjet_DistanceCalculator_parser(TreeDistanceCalculator);
00140     checkConfig(distanceCalc, "invalid tree distance calculator");
00141 
00142     // Determine representations for the OpenDX glyph size and color
00143     const edm::ParameterSet& GlyphSize(
00144         ps.getParameter<edm::ParameterSet>("GlyphSize"));
00145     glyphSize = fftjet_PeakFunctor_parser(GlyphSize);
00146     checkConfig(glyphSize, "invalid glyph size parameters");
00147 
00148     const edm::ParameterSet& GlyphColor(
00149         ps.getParameter<edm::ParameterSet>("GlyphColor"));
00150     glyphColor = fftjet_PeakFunctor_parser(GlyphColor);
00151     checkConfig(glyphColor, "invalid glyph color parameters");
00152 
00153     // Build the tree formatters
00154     denseFormatter = std::auto_ptr<DXFormatter>(new DXFormatter(
00155         glyphSize.get(), glyphColor.get(), etaMax));
00156     sparseFormatter = std::auto_ptr<SparseFormatter>(new SparseFormatter(
00157         glyphSize.get(), glyphColor.get(), etaMax));
00158 
00159     // Build the clustering tree
00160     clusteringTree = new ClusteringTree(distanceCalc.get());
00161 }
00162 
00163 
00164 FFTJetTreeDump::~FFTJetTreeDump()
00165 {
00166     delete clusteringTree;
00167 }
00168 
00169 
00170 //
00171 // member functions
00172 //
00173 template<class Real>
00174 void FFTJetTreeDump::processTreeData(const edm::Event& iEvent,
00175                                      std::ofstream& file)
00176 {
00177     typedef reco::PattRecoTree<Real,reco::PattRecoPeak<Real> > StoredTree;
00178 
00179     // Get the event number
00180     const unsigned long runNum = iEvent.id().run();
00181     const unsigned long evNum = iEvent.id().event();
00182 
00183     // Get the input
00184     edm::Handle<StoredTree> input;
00185     iEvent.getByLabel(treeLabel, input);
00186 
00187     const double eventScale = insertCompleteEvent ? completeEventScale : 0.0;
00188     if (input->isSparse())
00189     {
00190         sparsePeakTreeFromStorable(*input, iniScales.get(),
00191                                    eventScale, &sparseTree);
00192         sparseFormatter->setTree(sparseTree, runNum, evNum);
00193         file << *sparseFormatter << std::endl;
00194     }
00195     else
00196     {
00197         densePeakTreeFromStorable(*input, iniScales.get(),
00198                                   eventScale, clusteringTree);
00199         denseFormatter->setTree(*clusteringTree, runNum, evNum);
00200         file << *denseFormatter << std::endl;
00201     }
00202 }
00203 
00204 
00205 // ------------ method called to for each event  ------------
00206 void FFTJetTreeDump::analyze(const edm::Event& iEvent,
00207                              const edm::EventSetup& iSetup)
00208 {
00209     // Create the file name
00210     std::ostringstream filename;
00211     filename << outputPrefix << '_' << counter++ << ".dx";
00212 
00213     // Open the file
00214     std::ofstream file(filename.str().c_str());
00215     if (!file)
00216         throw cms::Exception("FFTJetBadConfig") 
00217             << "Failed to open file \""
00218             << filename.str() << "\"" << std::endl;
00219 
00220     if (storeInSinglePrecision)
00221         processTreeData<float>(iEvent, file);
00222     else
00223         processTreeData<double>(iEvent, file);
00224 }
00225 
00226 
00227 // ------------ method called once each job just before starting event loop
00228 void FFTJetTreeDump::beginJob()
00229 {
00230 }
00231 
00232 // ------------ method called once each job just after ending the event loop
00233 void FFTJetTreeDump::endJob() {
00234 }
00235 
00236 //define this as a plug-in
00237 DEFINE_FWK_MODULE(FFTJetTreeDump);