Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-17 23:31:52

0001 // -*- C++ -*-
0002 //
0003 // Package:    ClusterAnalyzer
0004 // Class:      ClusterAnalyzer
0005 //
0006 /**\class ClusterAnalyzer ClusterAnalyzer.cc msegala/ClusterSummary/src/ClusterAnalyzer.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Michael Segala
0015 //         Created:  Wed Feb 23 17:36:23 CST 2011
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Common/interface/Provenance.h"
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0029 #include "FWCore/ServiceRegistry/interface/Service.h"
0030 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0031 #include "DataFormats/TrackerCommon/interface/ClusterSummary.h"
0032 
0033 // ROOT includes
0034 #include "TTree.h"
0035 #include "TH1.h"
0036 #include "TFile.h"
0037 
0038 //
0039 // class declaration
0040 //
0041 
0042 class ClusterSummary;
0043 
0044 using namespace std;
0045 
0046 class ClusterAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0047 public:
0048   explicit ClusterAnalyzer(const edm::ParameterSet&);
0049   ~ClusterAnalyzer();
0050 
0051 private:
0052   virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
0053 
0054   // ----------member data ---------------------------
0055 
0056   edm::EDGetTokenT<ClusterSummary> token;
0057 
0058   std::map<int, std::string> enumModules_;
0059   std::map<std::string, TH1D*> histos1D_;
0060 
0061   std::vector<int> nType_;
0062   std::vector<float> clusterSize_;
0063   std::vector<float> clusterCharge_;
0064 
0065   std::map<int, std::string> allModules_;
0066 
0067   edm::Service<TFileService> fs;
0068 
0069   bool _verbose;
0070 };
0071 
0072 ClusterAnalyzer::ClusterAnalyzer(const edm::ParameterSet& iConfig) {
0073   usesResource(TFileService::kSharedResource);
0074   token = consumes<ClusterSummary>(iConfig.getParameter<edm::InputTag>("clusterSum"));
0075   _verbose = true;  //set to true to see the event by event summary info
0076 
0077   std::vector<std::string> wantedsubdets = iConfig.getParameter<std::vector<std::string> >("wantedSubDets");
0078   for (auto iS : wantedsubdets) {
0079     ClusterSummary::CMSTracker subdet = ClusterSummary::NVALIDENUMS;
0080     for (int iN = 0; iN < ClusterSummary::NVALIDENUMS; ++iN)
0081       if (ClusterSummary::subDetNames[iN] == iS)
0082         subdet = ClusterSummary::CMSTracker(iN);
0083     if (subdet == ClusterSummary::NVALIDENUMS)
0084       throw cms::Exception("No standard selection: ") << iS;
0085 
0086     allModules_[subdet] = iS;
0087   }
0088 
0089   std::vector<edm::ParameterSet> wantedusersubdets_ps =
0090       iConfig.getParameter<std::vector<edm::ParameterSet> >("wantedUserSubDets");
0091   for (const auto& iS : wantedusersubdets_ps) {
0092     ClusterSummary::CMSTracker subdet = (ClusterSummary::CMSTracker)iS.getParameter<unsigned int>("detSelection");
0093     std::string detname = iS.getParameter<std::string>("detLabel");
0094     if (subdet <= ClusterSummary::NVALIDENUMS)
0095       throw cms::Exception("Already predefined selection: ") << subdet;
0096     if (subdet >= ClusterSummary::NTRACKERENUMS)
0097       throw cms::Exception("Selection is out of range: ") << subdet;
0098     allModules_[subdet] = detname;
0099   }
0100 
0101   edm::LogPrint("ClusterAnalyzer") << "From provenance infomation the selected modules are = ";
0102   for (auto i : allModules_)
0103     edm::LogPrint("ClusterAnalyzer") << i.second << " ";
0104   edm::LogPrint("ClusterAnalyzer") << endl;
0105 
0106   for (auto i = allModules_.begin(); i != allModules_.end(); ++i) {
0107     std::string tmpstr = i->second;
0108     histos1D_[(tmpstr + "nclusters").c_str()] =
0109         fs->make<TH1D>((tmpstr + "nclusters").c_str(), (tmpstr + "nclusters").c_str(), 1000, 0, 3000);
0110     histos1D_[(tmpstr + "nclusters").c_str()]->SetXTitle(("number of Clusters in " + tmpstr).c_str());
0111 
0112     histos1D_[(tmpstr + "avgCharge").c_str()] =
0113         fs->make<TH1D>((tmpstr + "avgCharge").c_str(), (tmpstr + "avgCharge").c_str(), 500, 0, 1000);
0114     histos1D_[(tmpstr + "avgCharge").c_str()]->SetXTitle(("average cluster charge in " + tmpstr).c_str());
0115 
0116     histos1D_[(tmpstr + "avgSize").c_str()] =
0117         fs->make<TH1D>((tmpstr + "avgSize").c_str(), (tmpstr + "avgSize").c_str(), 30, 0, 10);
0118     histos1D_[(tmpstr + "avgSize").c_str()]->SetXTitle(("average cluster size in " + tmpstr).c_str());
0119   }
0120 }
0121 
0122 ClusterAnalyzer::~ClusterAnalyzer() = default;
0123 
0124 void ClusterAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0125   using namespace edm;
0126 
0127   Handle<ClusterSummary> class_;
0128   iEvent.getByToken(token, class_);
0129 
0130   for (unsigned int iM = 0; iM < class_->getNumberOfModules(); iM++) {
0131     auto iAllModules = allModules_.find(class_->getModule(iM));
0132     if (iAllModules == allModules_.end())
0133       continue;
0134 
0135     std::string tmpstr = iAllModules->second;
0136 
0137     histos1D_[(tmpstr + "nclusters").c_str()]->Fill(class_->getNClusByIndex(iM));
0138     histos1D_[(tmpstr + "avgSize").c_str()]->Fill(class_->getClusSizeByIndex(iM) / class_->getNClusByIndex(iM));
0139     histos1D_[(tmpstr + "avgCharge").c_str()]->Fill(class_->getClusChargeByIndex(iM) / class_->getNClusByIndex(iM));
0140 
0141     edm::LogPrint("ClusterAnalyzer") << "n" << tmpstr << ", avg size, avg charge = " << class_->getNClusByIndex(iM);
0142     edm::LogPrint("ClusterAnalyzer") << ", " << class_->getClusSizeByIndex(iM) / class_->getNClusByIndex(iM);
0143     edm::LogPrint("ClusterAnalyzer") << ", " << class_->getClusChargeByIndex(iM) / class_->getNClusByIndex(iM) << endl;
0144   }
0145   edm::LogPrint("ClusterAnalyzer") << "-------------------------------------------------------" << endl;
0146 }
0147 
0148 //define this as a plug-in
0149 DEFINE_FWK_MODULE(ClusterAnalyzer);