CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/RecoLuminosity/ROOTSchema/src/ROOTFileWriter.cc

Go to the documentation of this file.
00001 #include "RecoLuminosity/ROOTSchema/interface/ROOTFileWriter.h"
00002 #include "RecoLuminosity/TCPReceiver/interface/LumiStructures.hh"
00003 
00004 #include <sstream>
00005 #include <typeinfo>
00006 #include <iomanip>
00007 #include <vector>
00008 #include <ctime>
00009 
00010 #include <stddef.h>
00011 
00012 // mkdir
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 
00016 #include <TROOT.h>
00017 #include <TChain.h>
00018 #include <TTree.h>
00019 #include <TFile.h>
00020 
00021 HCAL_HLX::ROOTFileWriter::ROOTFileWriter():bMerge_(false){
00022 
00023   Init(); // ROOTFileBase
00024 }
00025 
00026 HCAL_HLX::ROOTFileWriter::~ROOTFileWriter(){
00027 
00028   CleanUp(); // ROOTFileBase
00029 }
00030 
00031 bool HCAL_HLX::ROOTFileWriter::OpenFile(const HCAL_HLX::LUMI_SECTION &localSection){
00032 
00033   return OpenFile( localSection.hdr.runNumber,
00034                    localSection.hdr.sectionNumber);
00035 }
00036 
00037 bool HCAL_HLX::ROOTFileWriter::OpenFile( const unsigned int runNumber, 
00038                                          const unsigned int sectionNumber){
00039 
00040   SetFileName( runNumber, sectionNumber);
00041 
00042   m_file = new TFile( (dirName_ + fileName_).c_str(), "RECREATE");
00043   if( !m_file ){
00044     return false;
00045   }
00046 
00047   m_file->cd();
00048   CreateTree();
00049 
00050   return true;
00051 }
00052 
00053 void HCAL_HLX::ROOTFileWriter::CreateTree(){
00054   
00055   m_tree   = new TTree("LumiTree","");
00056 
00057   Header_  = &(lumiSection_->hdr);
00058   Summary_ = &(lumiSection_->lumiSummary);
00059   Detail_  = &(lumiSection_->lumiDetail);
00060   
00061   m_tree->Bronch("Header.",  "HCAL_HLX::LUMI_SECTION_HEADER", &Header_,  1);
00062 
00063   if( !bEtSumOnly_ ){
00064     m_tree->Bronch("Summary.", "HCAL_HLX::LUMI_SUMMARY",        &Summary_, 1);
00065     m_tree->Bronch("Detail.",  "HCAL_HLX::LUMI_DETAIL",         &Detail_,  1);
00066   }
00067 
00068   for( unsigned int iHLX = 0; iHLX < 36; ++iHLX ){
00069     EtSumPtr_[iHLX] = &( lumiSection_->etSum[iHLX] );
00070     MakeBranch(lumiSection_->etSum[iHLX], &EtSumPtr_[iHLX], iHLX);
00071     
00072     if( !bEtSumOnly_ ){
00073       OccupancyPtr_[iHLX] = &(lumiSection_->occupancy[iHLX]);
00074       MakeBranch(lumiSection_->occupancy[iHLX], &OccupancyPtr_[iHLX], iHLX);
00075       
00076       LHCPtr_[iHLX] = &(lumiSection_->lhc[iHLX]);
00077       MakeBranch(lumiSection_->lhc[iHLX], &LHCPtr_[iHLX], iHLX);
00078     }
00079   }
00080 }
00081 
00082 template< class T >
00083 void HCAL_HLX::ROOTFileWriter::MakeBranch(const T &in, T **out, const int HLXNum){
00084   
00085   const std::string typeName = typeid(T).name();
00086   std::string className;
00087   std::string branchName;
00088   std::ostringstream numString;
00089 
00090   if(typeName == "N8HCAL_HLX11LHC_SECTIONE"){
00091     className = "HCAL_HLX::LHC_SECTION";
00092     branchName = "LHC";
00093   }else if(typeName == "N8HCAL_HLX17OCCUPANCY_SECTIONE"){
00094     className = "HCAL_HLX::OCCUPANCY_SECTION";
00095     branchName = "Occupancy";
00096   }else if(typeName == "N8HCAL_HLX14ET_SUM_SECTIONE"){
00097     className = "HCAL_HLX::ET_SUM_SECTION";
00098     branchName = "ETSum";
00099   }
00100   
00101   numString << std::setfill('0') << std::setw(2) << HLXNum;
00102   branchName = branchName + numString.str() + ".";
00103   m_tree->Bronch(branchName.c_str(), className.c_str(), out, 1);
00104 }
00105 
00106 void HCAL_HLX::ROOTFileWriter::FillTree(const HCAL_HLX::LUMI_SECTION& localSection){
00107 
00108   memcpy( lumiSection_, &localSection, sizeof(HCAL_HLX::LUMI_SECTION));
00109   m_tree->Fill();
00110 }
00111 
00112 bool HCAL_HLX::ROOTFileWriter::CloseFile(){
00113   
00114   m_tree->Write();
00115   m_file->Close();
00116   
00117   if(m_file != NULL){
00118     delete m_file;
00119     //delete m_tree; // NO!!! root does this when you delete m_file
00120     m_file = NULL;
00121     m_tree = NULL;
00122   }
00123   
00124   return true;
00125 }