Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:26

0001 #ifndef VariableNtupler_NTupler_H
0002 #define VariableNtupler_NTupler_H
0003 
0004 #include "PhysicsTools/UtilAlgos/interface/VariableHelper.h"
0005 
0006 #include "FWCore/Utilities/interface/InputTag.h"
0007 #include "FWCore/Framework/interface/Frameworkfwd.h"
0008 #include "FWCore/Framework/interface/EDProducer.h"
0009 #include "FWCore/Framework/interface/EDFilter.h"
0010 #include "FWCore/Framework/interface/ProducesCollector.h"
0011 
0012 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0013 #include "TTree.h"
0014 #include "TBranch.h"
0015 #include "TFile.h"
0016 
0017 #include "PhysicsTools/UtilAlgos/interface/NTupler.h"
0018 
0019 #include <algorithm>
0020 
0021 class VariableNTupler : public NTupler {
0022 public:
0023   VariableNTupler(const edm::ParameterSet& iConfig) {
0024     ownTheTree_ = false;
0025     edm::ParameterSet variablePSet = iConfig.getParameter<edm::ParameterSet>("variablesPSet");
0026     if (variablePSet.getParameter<bool>("allVariables")) {
0027       VariableHelper::iterator v = edm::Service<VariableHelperService>()->get().begin();
0028       VariableHelper::iterator v_end = edm::Service<VariableHelperService>()->get().end();
0029       for (; v != v_end; ++v) {
0030         leaves_[v->second->name()] = v->second;
0031       }
0032     } else {
0033       std::vector<std::string> leaves = variablePSet.getParameter<std::vector<std::string> >("leaves");
0034       for (uint i = 0; i != leaves.size(); ++i) {
0035         leaves_[leaves[i]] = edm::Service<VariableHelperService>()->get().variable(leaves[i]);
0036       }
0037     }
0038     if (variablePSet.exists("useTFileService"))
0039       useTFileService_ = variablePSet.getParameter<bool>("useTFileService");
0040     else
0041       useTFileService_ = iConfig.getParameter<bool>("useTFileService");
0042 
0043     if (useTFileService_) {
0044       if (variablePSet.exists("treeName"))
0045         treeName_ = variablePSet.getParameter<std::string>("treeName");
0046       else
0047         treeName_ = iConfig.getParameter<std::string>("treeName");
0048     }
0049   }
0050 
0051   uint registerleaves(edm::ProducesCollector producesCollector) override {
0052     uint nLeaves = 0;
0053     if (useTFileService_) {
0054       //loop the leaves registered
0055       nLeaves = leaves_.size();
0056       // make arrays of pointer to the actual values
0057       dataHolder_ = new double[nLeaves];
0058       iterator i = leaves_.begin();
0059       iterator i_end = leaves_.end();
0060       edm::Service<TFileService> fs;
0061       if (ownTheTree_) {
0062         ownTheTree_ = true;
0063         tree_ = fs->make<TTree>(treeName_.c_str(), "VariableNTupler tree");
0064       } else {
0065         TObject* object = fs->file().Get(treeName_.c_str());
0066         if (!object) {
0067           ownTheTree_ = true;
0068           tree_ = fs->make<TTree>(treeName_.c_str(), "VariableNTupler tree");
0069         } else {
0070           tree_ = dynamic_cast<TTree*>(object);
0071           if (!tree_) {
0072             ownTheTree_ = true;
0073             tree_ = fs->make<TTree>(treeName_.c_str(), "VariableNTupler tree");
0074           } else
0075             ownTheTree_ = false;
0076         }
0077       }
0078       uint iInDataHolder = 0;
0079       for (; i != i_end; ++i, ++iInDataHolder) {
0080         tree_->Branch(i->first.c_str(), &(dataHolder_[iInDataHolder]), (i->first + "/D").c_str());
0081       }
0082     } else {
0083       //loop the leaves registered
0084       iterator i = leaves_.begin();
0085       iterator i_end = leaves_.end();
0086       for (; i != i_end; ++i) {
0087         nLeaves++;
0088         std::string lName(i->first);
0089         std::replace(lName.begin(), lName.end(), '_', '0');
0090         producesCollector.produces<double>(lName).setBranchAlias(i->first);
0091       }
0092     }
0093     return nLeaves;
0094   }
0095 
0096   void fill(edm::Event& iEvent) override {
0097     if (useTFileService_) {
0098       //fill the data holder
0099       iterator i = leaves_.begin();
0100       iterator i_end = leaves_.end();
0101       uint iInDataHolder = 0;
0102       for (; i != i_end; ++i, ++iInDataHolder) {
0103         dataHolder_[iInDataHolder] = (*i->second)(iEvent);
0104       }
0105       //fill into root;
0106       if (ownTheTree_) {
0107         tree_->Fill();
0108       }
0109     } else {
0110       //other leaves
0111       iterator i = leaves_.begin();
0112       iterator i_end = leaves_.end();
0113       for (; i != i_end; ++i) {
0114         auto leafValue = std::make_unique<double>((*i->second)(iEvent));
0115         std::string lName(i->first);
0116         std::replace(lName.begin(), lName.end(), '_', '0');
0117         iEvent.put(std::move(leafValue), lName);
0118       }
0119     }
0120   }
0121   void callBack() {}
0122 
0123 protected:
0124   typedef std::map<std::string, const CachingVariable*>::iterator iterator;
0125   std::map<std::string, const CachingVariable*> leaves_;
0126 
0127   bool ownTheTree_;
0128   std::string treeName_;
0129   double* dataHolder_;
0130 };
0131 
0132 #endif