Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-26 03:52:01

0001 
0002 // system include files
0003 #include <iostream>
0004 #include <fstream>
0005 #include <vector>
0006 #include <string>
0007 
0008 // user include files
0009 #include "FWCore/Framework/interface/Frameworkfwd.h"
0010 #include "FWCore/Framework/interface/EDAnalyzer.h"
0011 
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/Framework/interface/MakerMacros.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 
0017 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 
0020 #include "DataFormats/Candidate/interface/Candidate.h"
0021 #include "DataFormats/RecoCandidate/interface/RecoCaloTowerCandidate.h"
0022 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
0023 
0024 #include "DataFormats/Common/interface/Ref.h"
0025 
0026 #include "DataFormats/HeavyIonEvent/interface/CentralityBins.h"
0027 
0028 #include "CondFormats/DataRecord/interface/HeavyIonRcd.h"
0029 #include "CondFormats/HIObjects/interface/CentralityTable.h"
0030 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0031 
0032 #include <TFile.h>
0033 
0034 using namespace std;
0035 //
0036 // class decleration
0037 //
0038 
0039 class CentralityTableProducer : public edm::EDAnalyzer {
0040 public:
0041   explicit CentralityTableProducer(const edm::ParameterSet&);
0042   ~CentralityTableProducer() override;
0043 
0044 private:
0045   void analyze(const edm::Event&, const edm::EventSetup&) override;
0046   void endJob() override;
0047   void printBin(const CentralityTable::CBin*);
0048   // ----------member data ---------------------------
0049 
0050   bool makeDBFromTFile_;
0051   bool makeTFileFromDB_;
0052   bool firstRunOnly_;
0053   bool debug_;
0054 
0055   TFile* inputTFile_;
0056   string inputTFileName_;
0057   edm::Service<TFileService> fs;
0058 
0059   string rootTag_;
0060   ofstream text_;
0061 
0062   const CentralityBins* CB;
0063 
0064   unsigned int runnum_;
0065 };
0066 
0067 //
0068 // constants, enums and typedefs
0069 //
0070 
0071 //
0072 // static data member definitions
0073 //
0074 
0075 //
0076 // constructors and destructor
0077 //
0078 CentralityTableProducer::CentralityTableProducer(const edm::ParameterSet& iConfig) : text_("bins.txt"), runnum_(0) {
0079   //now do what ever initialization is needed
0080   makeDBFromTFile_ = iConfig.getUntrackedParameter<bool>("makeDBFromTFile", true);
0081   makeTFileFromDB_ = iConfig.getUntrackedParameter<bool>("makeTFileFromDB", false);
0082   firstRunOnly_ = iConfig.getUntrackedParameter<bool>("isMC", false);
0083   debug_ = iConfig.getUntrackedParameter<bool>("debug", false);
0084   if (makeDBFromTFile_) {
0085     inputTFileName_ = iConfig.getParameter<string>("inputTFile");
0086     rootTag_ = iConfig.getParameter<string>("rootTag");
0087     inputTFile_ = new TFile(inputTFileName_.data(), "read");
0088     cout << inputTFileName_.data() << endl;
0089   }
0090 }
0091 
0092 CentralityTableProducer::~CentralityTableProducer() {
0093   // do anything here that needs to be done at desctruction time
0094   // (e.g. close files, deallocate resources etc.)
0095 }
0096 
0097 //
0098 // member functions
0099 //
0100 
0101 // ------------ method called to for each event  ------------
0102 void CentralityTableProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0103   if (!makeTFileFromDB_)
0104     return;
0105   if ((!firstRunOnly_ && runnum_ != iEvent.id().run()) || (firstRunOnly_ && runnum_ == 0)) {
0106     runnum_ = iEvent.id().run();
0107     cout << "Adding table for run : " << runnum_ << endl;
0108     TFileDirectory subDir = fs->mkdir(Form("run%d", runnum_));
0109     CB = subDir.make<CentralityBins>();
0110   }
0111 }
0112 
0113 // ------------ method called once each job just after ending the event loop  ------------
0114 void CentralityTableProducer::endJob() {
0115   if (makeDBFromTFile_) {
0116     runnum_ = 1;
0117     // Get values from root file
0118     CB = (CentralityBins*)inputTFile_->Get(Form("%s/run%d", rootTag_.data(), runnum_));
0119     cout << rootTag_.data() << endl;
0120     CentralityTable CT;
0121     CT.m_table.reserve(CB->getNbins());
0122 
0123     text_ << "# BinEdge NpartMean NpartVar NcollMean NcollVar NhardMean NhardVar bMean bVar" << endl;
0124     for (int j = 0; j < CB->getNbins(); j++) {
0125       CentralityTable::CBin* thisBin = new CentralityTable::CBin();
0126       thisBin->bin_edge = CB->lowEdgeOfBin(j);
0127       thisBin->n_part.mean = CB->NpartMeanOfBin(j);
0128       thisBin->n_part.var = CB->NpartSigmaOfBin(j);
0129       thisBin->n_coll.mean = CB->NcollMeanOfBin(j);
0130       thisBin->n_coll.var = CB->NcollSigmaOfBin(j);
0131       thisBin->n_hard.mean = CB->NhardMeanOfBin(j);
0132       thisBin->n_hard.var = CB->NhardSigmaOfBin(j);
0133       thisBin->b.mean = CB->bMeanOfBin(j);
0134       thisBin->b.var = CB->bSigmaOfBin(j);
0135       printBin(thisBin);
0136       CT.m_table.push_back(*thisBin);
0137       if (thisBin)
0138         delete thisBin;
0139     }
0140 
0141     edm::Service<cond::service::PoolDBOutputService> pool;
0142     if (pool.isAvailable()) {
0143       pool->writeOneIOV(CT, pool->currentTime(), "HeavyIonRcd");
0144     }
0145   }
0146 }
0147 
0148 void CentralityTableProducer::printBin(const CentralityTable::CBin* thisBin) {
0149   cout << "HF Cut = " << thisBin->bin_edge << endl;
0150   cout << "Npart = " << thisBin->n_part.mean << endl;
0151   cout << "sigma = " << thisBin->n_part.var << endl;
0152   cout << "Ncoll = " << thisBin->n_coll.mean << endl;
0153   cout << "sigma = " << thisBin->n_coll.var << endl;
0154   cout << "B     = " << thisBin->b.mean << endl;
0155   cout << "sigma = " << thisBin->b.var << endl;
0156   text_ << Form("%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f",
0157                 (Float_t)thisBin->bin_edge,
0158                 (Float_t)thisBin->n_part.mean,
0159                 (Float_t)thisBin->n_part.var,
0160                 (Float_t)thisBin->n_coll.mean,
0161                 (Float_t)thisBin->n_coll.var,
0162                 (Float_t)thisBin->n_hard.mean,
0163                 (Float_t)thisBin->n_hard.var,
0164                 (Float_t)thisBin->b.mean,
0165                 (Float_t)thisBin->b.var)
0166         << endl;
0167   cout << "__________________________________________________" << endl;
0168 }
0169 
0170 //define this as a plug-in
0171 DEFINE_FWK_MODULE(CentralityTableProducer);