CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/RecoHI/HiCentralityAlgos/src/CentralityTableProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    CentralityTableProducer
00004 // Class:      CentralityTableProducer
00005 // 
00013 //
00014 // Original Author:  Yetkin Yilmaz
00015 //         Created:  Wed May  2 21:41:30 EDT 2007
00016 // $Id: CentralityTableProducer.cc,v 1.12 2010/11/03 14:53:05 yilmaz Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <iostream>
00023 #include <fstream>
00024 #include <vector>
00025 #include <string>
00026 
00027 // user include files
00028 #include "FWCore/Framework/interface/Frameworkfwd.h"
00029 #include "FWCore/Framework/interface/EDAnalyzer.h"
00030 
00031 #include "FWCore/Framework/interface/Event.h"
00032 #include "FWCore/Framework/interface/EventSetup.h"
00033 #include "FWCore/Framework/interface/MakerMacros.h"
00034 #include "FWCore/Framework/interface/ESHandle.h"
00035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00036 
00037 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00038 #include "FWCore/ServiceRegistry/interface/Service.h"
00039 
00040 #include "DataFormats/Candidate/interface/Candidate.h"
00041 #include "DataFormats/RecoCandidate/interface/RecoCaloTowerCandidate.h"
00042 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00043 #include "DataFormats/Common/interface/EDProduct.h"
00044 #include "DataFormats/Common/interface/Ref.h"
00045 
00046 #include "CondFormats/DataRecord/interface/HeavyIonRcd.h"
00047 #include "CondFormats/HIObjects/interface/CentralityTable.h"
00048 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00049 
00050 #include "DataFormats/HeavyIonEvent/interface/CentralityProvider.h"
00051 
00052 #include <TFile.h>
00053 
00054 using namespace std;
00055 //
00056 // class decleration
00057 //
00058 
00059 class CentralityTableProducer : public edm::EDAnalyzer {
00060    public:
00061       explicit CentralityTableProducer(const edm::ParameterSet&);
00062       ~CentralityTableProducer();
00063 
00064    private:
00065       virtual void beginRun(const edm::EventSetup&) ;
00066       virtual void analyze(const edm::Event&, const edm::EventSetup&);
00067       virtual void endJob() ;
00068    void printBin(const CentralityTable::CBin*);
00069       // ----------member data ---------------------------
00070 
00071    bool makeDBFromTFile_;
00072    bool makeTFileFromDB_;
00073   bool firstRunOnly_;
00074   bool debug_;
00075 
00076    edm::ESHandle<CentralityTable> inputDB_;
00077    TFile* inputTFile_;
00078    string inputTFileName_;   
00079    edm::Service<TFileService> fs;
00080 
00081    string rootTag_;
00082    ofstream text_;
00083 
00084    CentralityTable* CT;
00085    const CentralityBins* CB;
00086 
00087    unsigned int runnum_;
00088 
00089 };
00090 
00091 //
00092 // constants, enums and typedefs
00093 //
00094 
00095 //
00096 // static data member definitions
00097 //
00098 
00099 //
00100 // constructors and destructor
00101 //
00102 CentralityTableProducer::CentralityTableProducer(const edm::ParameterSet& iConfig):
00103    text_("bins.txt"),
00104    runnum_(0)
00105 {
00106    //now do what ever initialization is needed
00107    makeDBFromTFile_ = iConfig.getUntrackedParameter<bool>("makeDBFromTFile",1);
00108    makeTFileFromDB_ = iConfig.getUntrackedParameter<bool>("makeTFileFromDB",0);
00109    firstRunOnly_ = iConfig.getUntrackedParameter<bool>("isMC",false);
00110    debug_ = iConfig.getUntrackedParameter<bool>("debug",false);
00111    if(makeDBFromTFile_){
00112       inputTFileName_ = iConfig.getParameter<string>("inputTFile");
00113       rootTag_ = iConfig.getParameter<string>("rootTag");
00114       inputTFile_  = new TFile(inputTFileName_.data(),"read");
00115       cout<<inputTFileName_.data()<<endl;
00116    }
00117 }
00118 
00119 CentralityTableProducer::~CentralityTableProducer()
00120 {
00121    // do anything here that needs to be done at desctruction time
00122    // (e.g. close files, deallocate resources etc.)
00123 }
00124 
00125 //
00126 // member functions
00127 //
00128 
00129 // ------------ method called to for each event  ------------
00130 void
00131 CentralityTableProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00132 {
00133   if(!makeTFileFromDB_) return;
00134   if((!firstRunOnly_ && runnum_ != iEvent.id().run()) || (firstRunOnly_ && runnum_ == 0)){
00135     runnum_ = iEvent.id().run();
00136     cout<<"Adding table for run : "<<runnum_<<endl;
00137     CentralityProvider cent(iSetup);
00138     if(debug_) cent.print();
00139     TFileDirectory subDir = fs->mkdir(Form("run%d",runnum_));
00140     CB = subDir.make<CentralityBins>((CentralityBins)cent);
00141   }    
00142 }
00143 
00144 // ------------ method called once each job just before starting event loop  ------------
00145 void 
00146 CentralityTableProducer::beginRun(const edm::EventSetup& iSetup)
00147 {
00148 }
00149 
00150 // ------------ method called once each job just after ending the event loop  ------------
00151 void 
00152 CentralityTableProducer::endJob() {
00153 
00154    if(makeDBFromTFile_){
00155       runnum_ = 1;
00156       // Get values from root file
00157       CB = (CentralityBins*) inputTFile_->Get(Form("%s/run%d",rootTag_.data(),runnum_));
00158       cout<<rootTag_.data()<<endl;
00159       CT = new CentralityTable();
00160       CT->m_table.reserve(CB->getNbins());
00161 
00162       text_<<"# BinEdge NpartMean NpartVar NcollMean NcollVar NhardMean NhardVar bMean bVar"<<endl;
00163    for(int j=0; j<CB->getNbins(); j++){
00164        CentralityTable::CBin* thisBin = new CentralityTable::CBin();
00165        thisBin->bin_edge = CB->lowEdgeOfBin(j);
00166        thisBin->n_part.mean = CB->NpartMeanOfBin(j);
00167        thisBin->n_part.var  = CB->NpartSigmaOfBin(j);
00168        thisBin->n_coll.mean = CB->NcollMeanOfBin(j);
00169        thisBin->n_coll.var  = CB->NcollSigmaOfBin(j);
00170        thisBin->n_hard.mean = CB->NhardMeanOfBin(j);
00171        thisBin->n_hard.var  = CB->NhardSigmaOfBin(j);
00172        thisBin->b.mean = CB->bMeanOfBin(j);
00173        thisBin->b.var = CB->bSigmaOfBin(j);
00174        printBin(thisBin);
00175        CT->m_table.push_back(*thisBin);
00176        if(thisBin) delete thisBin;
00177   }
00178 
00179       edm::Service<cond::service::PoolDBOutputService> pool;
00180       if( pool.isAvailable() ){
00181          if( pool->isNewTagRequest( "HeavyIonRcd" ) ){
00182             pool->createNewIOV<CentralityTable>( CT, pool->beginOfTime(), pool->endOfTime(), "HeavyIonRcd" );
00183          }else{
00184             pool->appendSinceTime<CentralityTable>( CT, pool->currentTime(), "HeavyIonRcd" );
00185          }
00186       }
00187    }
00188 }
00189 
00190 
00191 void CentralityTableProducer::printBin(const CentralityTable::CBin* thisBin){
00192   
00193    cout<<"HF Cut = "<<thisBin->bin_edge<<endl;
00194    cout<<"Npart = "<<thisBin->n_part.mean<<endl;
00195    cout<<"sigma = "<<thisBin->n_part.var<<endl;
00196    cout<<"Ncoll = "<<thisBin->n_coll.mean<<endl;
00197    cout<<"sigma = "<<thisBin->n_coll.var<<endl;
00198    cout<<"B     = "<<thisBin->b.mean<<endl;
00199    cout<<"sigma = "<<thisBin->b.var<<endl;
00200    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",
00201                (Float_t)thisBin->bin_edge,
00202                (Float_t)thisBin->n_part.mean,
00203                (Float_t)thisBin->n_part.var,
00204                (Float_t)thisBin->n_coll.mean,
00205                (Float_t)thisBin->n_coll.var,
00206                (Float_t)thisBin->n_hard.mean,
00207                (Float_t)thisBin->n_hard.var,
00208                (Float_t)thisBin->b.mean,
00209                (Float_t)thisBin->b.var)
00210         <<endl;
00211    cout<<"__________________________________________________"<<endl;
00212    
00213 }
00214 
00215 
00216 
00217 //define this as a plug-in
00218 DEFINE_FWK_MODULE(CentralityTableProducer);