CMS 3D CMS Logo

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