CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/RecoLuminosity/ROOTSchema/src/ROOTFileReader.cc

Go to the documentation of this file.
00001 #include "RecoLuminosity/ROOTSchema/interface/ROOTFileReader.h"
00002 #include "RecoLuminosity/TCPReceiver/interface/LumiStructures.hh"
00003 
00004 // C
00005 #include <cstring> // memset
00006 
00007 // STL
00008 #include <iomanip>
00009 #include <sstream>
00010 #include <algorithm>
00011 
00012 // Unix
00013 #include <dirent.h> // opendir
00014 
00015 // ROOT
00016 #include <TROOT.h>
00017 #include <TFile.h>
00018 #include <TChain.h>
00019 
00020 HCAL_HLX::ROOTFileReader::ROOTFileReader(){
00021   
00022   mChain_ = new TChain("LumiTree");
00023   Init();
00024 }
00025 
00026 HCAL_HLX::ROOTFileReader::~ROOTFileReader(){
00027 
00028   CleanUp();
00029   delete mChain_;  
00030 }
00031 
00032 int HCAL_HLX::ROOTFileReader::CreateFileNameList(){
00033 
00034   // Look for files that follow the standard naming convention.
00035   DIR *dp;
00036   struct dirent *dirp;
00037   std::string tempFileName;
00038 
00039   std::vector< std::string > fileNames;
00040   fileNames.clear();
00041 
00042   if( dirName_ == ""){
00043     return false;
00044   }
00045   
00046   // Check directory existance.
00047   if( ( dp = opendir( dirName_.c_str() )  ) == NULL ){
00048     return false;
00049   }
00050 
00051   while( (dirp = readdir(dp)) != NULL ){
00052     tempFileName = dirp->d_name;
00053     if(tempFileName.substr(0,8) == "CMS_LUMI" ){
00054       fileNames.push_back( dirName_ + tempFileName);
00055     }
00056   }
00057   closedir(dp);
00058 
00059   if( fileNames.size() == 0 ){
00060     return false;
00061   }
00062 
00063   sort(fileNames.begin(), fileNames.end());
00064   return ReplaceFile( fileNames );
00065 }
00066 
00067 int HCAL_HLX::ROOTFileReader::SetFileName(const std::string &fileName){  
00068 
00069   std::vector< std::string > tempVecOfStrings;
00070 
00071   tempVecOfStrings.clear();
00072   tempVecOfStrings.push_back( dirName_ + fileName);
00073 
00074   return ReplaceFile( tempVecOfStrings );
00075 }
00076 
00077 int HCAL_HLX::ROOTFileReader::ReplaceFile(const std::vector< std::string> &fileNames){
00078   // ReplaceFile is called by either SetFileName or CreateFileNameList.
00079   
00080   delete mChain_;
00081   mChain_ = new TChain("LumiTree");
00082   
00083   for( std::vector< std::string >::const_iterator VoS = fileNames.begin(); 
00084        VoS != fileNames.end(); 
00085        ++VoS){
00086     mChain_->Add((*VoS).c_str());
00087   }
00088 
00089   CreateTree();
00090 
00091   return mChain_->GetEntries();
00092 }
00093 
00094 void HCAL_HLX::ROOTFileReader::CreateTree(){
00095 
00096   Header_  = &(lumiSection_->hdr);
00097   Summary_ = &(lumiSection_->lumiSummary);
00098   Detail_  = &(lumiSection_->lumiDetail);
00099 
00100   mChain_->SetBranchAddress("Header.",  &Header_,  &b_Header);
00101 
00102   if( !bEtSumOnly_ ){
00103     mChain_->SetBranchAddress("Summary.", &Summary_, &b_Summary);
00104     mChain_->SetBranchAddress("Detail.",  &Detail_,  &b_Detail);
00105   }
00106 
00107   for(unsigned int iHLX = 0; iHLX < 36; ++iHLX){
00108     std::stringstream branchName;
00109 
00110     EtSumPtr_[iHLX] = &(lumiSection_->etSum[iHLX]);
00111     branchName.str(std::string());
00112     branchName << "ETSum" << std::setw(2) << std::setfill('0') << iHLX << ".";
00113     mChain_->SetBranchAddress(branchName.str().c_str(), &EtSumPtr_[iHLX], &b_ETSum[iHLX]);
00114 
00115     if( !bEtSumOnly_ ){
00116       OccupancyPtr_[iHLX] = &(lumiSection_->occupancy[iHLX]);
00117       branchName.str(std::string());
00118       branchName << "Occupancy" << std::setw(2) << std::setfill('0') << iHLX << ".";
00119       mChain_->SetBranchAddress(branchName.str().c_str(), &OccupancyPtr_[iHLX], &b_Occupancy[iHLX]);
00120       
00121       LHCPtr_[iHLX] = &(lumiSection_->lhc[iHLX]);
00122       branchName.str(std::string());
00123       branchName << "LHC" << std::setw(2) << std::setfill('0') << iHLX << ".";
00124       mChain_->SetBranchAddress(branchName.str().c_str(), &LHCPtr_[iHLX], &b_LHC[iHLX]);
00125     }
00126 
00127   }
00128 
00129 }
00130 
00131 unsigned int HCAL_HLX::ROOTFileReader::GetEntries(){
00132 
00133   return mChain_->GetEntries();
00134 }
00135 
00136 int HCAL_HLX::ROOTFileReader::GetEntry( int entry ){
00137 
00138   int bytes =  mChain_->GetEntry(entry);
00139   return bytes;
00140 }
00141 
00142 int HCAL_HLX::ROOTFileReader::GetLumiSection( HCAL_HLX::LUMI_SECTION& localSection){
00143 
00144   memcpy(&localSection, lumiSection_, sizeof(HCAL_HLX::LUMI_SECTION));
00145   return 0;
00146 }