Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:18:08

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/one/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::one::EDAnalyzer<edm::one::SharedResources> {
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   usesResource(TFileService::kSharedResource);
0080   //now do what ever initialization is needed
0081   makeDBFromTFile_ = iConfig.getUntrackedParameter<bool>("makeDBFromTFile", true);
0082   makeTFileFromDB_ = iConfig.getUntrackedParameter<bool>("makeTFileFromDB", false);
0083   firstRunOnly_ = iConfig.getUntrackedParameter<bool>("isMC", false);
0084   debug_ = iConfig.getUntrackedParameter<bool>("debug", false);
0085   if (makeDBFromTFile_) {
0086     inputTFileName_ = iConfig.getParameter<string>("inputTFile");
0087     rootTag_ = iConfig.getParameter<string>("rootTag");
0088     inputTFile_ = new TFile(inputTFileName_.data(), "read");
0089     cout << inputTFileName_.data() << endl;
0090   }
0091 }
0092 
0093 CentralityTableProducer::~CentralityTableProducer() {
0094   // do anything here that needs to be done at desctruction time
0095   // (e.g. close files, deallocate resources etc.)
0096 }
0097 
0098 //
0099 // member functions
0100 //
0101 
0102 // ------------ method called to for each event  ------------
0103 void CentralityTableProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0104   if (!makeTFileFromDB_)
0105     return;
0106   if ((!firstRunOnly_ && runnum_ != iEvent.id().run()) || (firstRunOnly_ && runnum_ == 0)) {
0107     runnum_ = iEvent.id().run();
0108     cout << "Adding table for run : " << runnum_ << endl;
0109     TFileDirectory subDir = fs->mkdir(Form("run%d", runnum_));
0110     CB = subDir.make<CentralityBins>();
0111   }
0112 }
0113 
0114 // ------------ method called once each job just after ending the event loop  ------------
0115 void CentralityTableProducer::endJob() {
0116   if (makeDBFromTFile_) {
0117     runnum_ = 1;
0118     // Get values from root file
0119     CB = (CentralityBins*)inputTFile_->Get(Form("%s/run%d", rootTag_.data(), runnum_));
0120     cout << rootTag_.data() << endl;
0121     CentralityTable CT;
0122     CT.m_table.reserve(CB->getNbins());
0123 
0124     text_ << "# BinEdge NpartMean NpartVar NcollMean NcollVar NhardMean NhardVar bMean bVar" << endl;
0125     for (int j = 0; j < CB->getNbins(); j++) {
0126       CentralityTable::CBin* thisBin = new CentralityTable::CBin();
0127       thisBin->bin_edge = CB->lowEdgeOfBin(j);
0128       thisBin->n_part.mean = CB->NpartMeanOfBin(j);
0129       thisBin->n_part.var = CB->NpartSigmaOfBin(j);
0130       thisBin->n_coll.mean = CB->NcollMeanOfBin(j);
0131       thisBin->n_coll.var = CB->NcollSigmaOfBin(j);
0132       thisBin->n_hard.mean = CB->NhardMeanOfBin(j);
0133       thisBin->n_hard.var = CB->NhardSigmaOfBin(j);
0134       thisBin->b.mean = CB->bMeanOfBin(j);
0135       thisBin->b.var = CB->bSigmaOfBin(j);
0136       printBin(thisBin);
0137       CT.m_table.push_back(*thisBin);
0138       if (thisBin)
0139         delete thisBin;
0140     }
0141 
0142     edm::Service<cond::service::PoolDBOutputService> pool;
0143     if (pool.isAvailable()) {
0144       pool->writeOneIOV(CT, pool->currentTime(), "HeavyIonRcd");
0145     }
0146   }
0147 }
0148 
0149 void CentralityTableProducer::printBin(const CentralityTable::CBin* thisBin) {
0150   cout << "HF Cut = " << thisBin->bin_edge << endl;
0151   cout << "Npart = " << thisBin->n_part.mean << endl;
0152   cout << "sigma = " << thisBin->n_part.var << endl;
0153   cout << "Ncoll = " << thisBin->n_coll.mean << endl;
0154   cout << "sigma = " << thisBin->n_coll.var << endl;
0155   cout << "B     = " << thisBin->b.mean << endl;
0156   cout << "sigma = " << thisBin->b.var << endl;
0157   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",
0158                 (Float_t)thisBin->bin_edge,
0159                 (Float_t)thisBin->n_part.mean,
0160                 (Float_t)thisBin->n_part.var,
0161                 (Float_t)thisBin->n_coll.mean,
0162                 (Float_t)thisBin->n_coll.var,
0163                 (Float_t)thisBin->n_hard.mean,
0164                 (Float_t)thisBin->n_hard.var,
0165                 (Float_t)thisBin->b.mean,
0166                 (Float_t)thisBin->b.var)
0167         << endl;
0168   cout << "__________________________________________________" << endl;
0169 }
0170 
0171 //define this as a plug-in
0172 DEFINE_FWK_MODULE(CentralityTableProducer);