CMS 3D CMS Logo

ROOTFileBase.cc

Go to the documentation of this file.
00001 #include "RecoLuminosity/ROOTSchema/interface/ROOTFileBase.h"
00002 
00003 #include <sstream>
00004 #include <iostream>
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::ROOTFileBase::ROOTFileBase(){
00022 #ifdef DEBUG
00023   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00024 #endif
00025 
00026   Header          = new HCAL_HLX::LUMI_SECTION_HEADER;
00027   Summary         = new HCAL_HLX::LUMI_SUMMARY;
00028   Detail          = new HCAL_HLX::LUMI_DETAIL;
00029 
00030   EtSum           = new HCAL_HLX::ET_SUM_SECTION[HCAL_HLX_MAX_HLXS];
00031   Occupancy       = new HCAL_HLX::OCCUPANCY_SECTION[HCAL_HLX_MAX_HLXS];
00032   LHC             = new HCAL_HLX::LHC_SECTION[HCAL_HLX_MAX_HLXS];
00033 
00034   Threshold       = new HCAL_HLX::LUMI_THRESHOLD;
00035   L1Trigger       = new HCAL_HLX::LEVEL1_TRIGGER;
00036   HLT             = new HCAL_HLX::HLT;
00037   TriggerDeadtime = new HCAL_HLX::TRIGGER_DEADTIME;
00038   RingSet         = new HCAL_HLX::LUMI_HF_RING_SET;
00039 
00040   outputDir_ = ".";
00041   outputFilePrefix_ = "CMS_LUMI_RAW";
00042 
00043   m_file = NULL;
00044 
00045   fileCounter_ = 0;
00046 
00047 #ifdef DEBUG
00048   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00049 #endif
00050 }
00051 
00052 HCAL_HLX::ROOTFileBase::~ROOTFileBase(){
00053 
00054 #ifdef DEBUG
00055   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00056 #endif
00057 
00058   delete Header;
00059   delete Summary;
00060   delete Detail;
00061 
00062   delete [] EtSum;
00063   delete [] Occupancy;
00064   delete [] LHC;
00065 
00066   delete Threshold;
00067   delete L1Trigger;
00068   delete HLT;
00069   delete TriggerDeadtime; 
00070   delete RingSet;
00071 
00072 #ifdef DEBUG
00073   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00074 #endif
00075 }
00076 
00077 void HCAL_HLX::ROOTFileBase::SetFileName(const std::string& fileName){
00078 
00079   fileName_ = fileName;
00080 
00081 } 
00082 
00083 std::string HCAL_HLX::ROOTFileBase::CreateLSFileName(const unsigned int runNumber, const unsigned int sectionNumber){
00084 #ifdef DEBUG
00085   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00086 #endif
00087 
00088   std::ostringstream outputString;
00089 
00090   // Create directory name
00091   outputString << outputDir_ << "/"
00092                << TimeStampYYYYMM();
00093   mkdir(outputString.str().c_str(), 0777);
00094 
00095   outputString << "/" << std::setfill('0') << std::setw(9) << runNumber;
00096   
00097   // Create the directory that will contain the single lumi section file
00098   mkdir(outputString.str().c_str(), 0777);
00099   
00100   // Create file name
00101   outputString << "/"
00102                << outputFilePrefix_
00103                << "_"
00104                << TimeStampYYYYMMDD() 
00105                << "_"
00106                << std::setfill('0') << std::setw(9) << runNumber << "_"
00107                << std::setfill('0') << std::setw(4) << sectionNumber 
00108                << ".root";
00109 #ifdef DEBUG
00110   std::cout << "Output file is " << outputString.str() << std::endl;
00111   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00112 #endif
00113 
00114   return outputString.str();
00115 }
00116 
00117 std::string HCAL_HLX::ROOTFileBase::CreateRunFileName(const unsigned int runNumber, const unsigned int firstSection){
00118 #ifdef DEBUG
00119   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00120 #endif
00121 
00122   std::ostringstream outputString;
00123 
00124   // Create file name
00125   outputString << outputDir_ << "/"
00126                << TimeStampYYYYMM()
00127                << "/"
00128                << outputFilePrefix_
00129                << "_"
00130                << TimeStampYYYYMMDD() 
00131                << "_"
00132                << std::setfill('0') << std::setw(9) << runNumber 
00133                << "_"
00134                << std::setfill('0') << std::setw(4) << firstSection
00135                << ".root";
00136   
00137 #ifdef DEBUG
00138   std::cout << "Output file is " <<  outputString.str() << std::endl;
00139   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00140 #endif
00141   
00142   return  outputString.str();
00143 
00144 }
00145 
00146 void HCAL_HLX::ROOTFileBase::CreateTree(const HCAL_HLX::LUMI_SECTION & localSection){
00147 #ifdef DEBUG
00148   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00149 #endif
00150   
00151   if(fileName_ == ""){
00152     std::cout << "*** File Name was not set ***" << std::endl;
00153     return;
00154   }
00155 
00156   m_file = new TFile(fileName_.c_str(), "RECREATE");  
00157 
00158   if(!m_file){
00159     std::cout << " *** Couldn't make or open file: " << fileName_ << " *** " << std::endl;
00160     exit(1);
00161   } 
00162 
00163   m_file->cd();
00164   
00165   m_tree  = new TTree("LumiTree","");
00166 
00167   m_tree->Bronch("Header.",  "HCAL_HLX::LUMI_SECTION_HEADER", &Header,  1);
00168   m_tree->Bronch("Summary.", "HCAL_HLX::LUMI_SUMMARY",        &Summary, 1);
00169   m_tree->Bronch("Detail.",  "HCAL_HLX::LUMI_DETAIL",         &Detail,  1);
00170 
00171   m_tree->Bronch("Threshold.",        "HCAL_HLX::LUMI_THRESHOLD",   &Threshold, 1);
00172   m_tree->Bronch("Level1_Trigger.",   "HCAL_HLX::LEVEL1_TRIGGER",   &L1Trigger, 1);
00173   m_tree->Bronch("HLT.",              "HCAL_HLX::HLT",              &HLT,       1);
00174   m_tree->Bronch("Trigger_Deadtime.", "HCAL_HLX::TRIGGER_DEADTIME", &TriggerDeadtime, 1);
00175   m_tree->Bronch("HF_Ring_Set.",      "HCAL_HLX::LUMI_HF_RING_SET", &RingSet,1);
00176 
00177   for(int i = 0; i < HCAL_HLX_MAX_HLXS; i++){
00178     EtSumPtr[i] = &EtSum[i];
00179     MakeBranch(localSection.etSum[i], &EtSumPtr[i], i);
00180 
00181     OccupancyPtr[i] = &Occupancy[i];
00182     MakeBranch(localSection.occupancy[i], &OccupancyPtr[i], i);
00183 
00184     LHCPtr[i] = &LHC[i];
00185     MakeBranch(localSection.lhc[i], &LHCPtr[i], i);
00186   }
00187 
00188 #ifdef DEBUG
00189   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00190 #endif
00191 }
00192 
00193 void HCAL_HLX::ROOTFileBase::FillTree(const HCAL_HLX::LUMI_SECTION& localSection){
00194   
00195 #ifdef DEBUG
00196   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00197 #endif
00198 
00199   if(fileName_ == ""){
00200     std::cout << "*** File Name was not set ***" << std::endl;
00201     return;
00202   }
00203   
00204   for(int i = 0; i < HCAL_HLX_MAX_HLXS; i++){
00205     memcpy(&EtSum[i],     &localSection.etSum[i],     sizeof(HCAL_HLX::ET_SUM_SECTION));
00206     memcpy(&Occupancy[i], &localSection.occupancy[i], sizeof(HCAL_HLX::OCCUPANCY_SECTION));
00207     memcpy(&LHC[i],       &localSection.lhc[i],       sizeof(HCAL_HLX::LHC_SECTION));
00208   }
00209 
00210   memcpy(Header,  &localSection.hdr,         sizeof (localSection.hdr));
00211   memcpy(Summary, &localSection.lumiSummary, sizeof(HCAL_HLX::LUMI_SUMMARY));
00212   memcpy(Detail,  &localSection.lumiDetail,  sizeof(HCAL_HLX::LUMI_DETAIL));
00213 
00214   InsertInformation(); // To be modified later.
00215 
00216   m_tree->Fill();
00217 
00218 #ifdef DEBUG
00219   //  m_tree->Print();
00220   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00221 #endif
00222 }
00223 
00224 template< class T >
00225 void HCAL_HLX::ROOTFileBase::MakeBranch(const T &in, T **out, int HLXNum){
00226 
00227 #ifdef DEBUG
00228   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00229 #endif
00230   
00231   const std::string typeName = typeid(T).name();
00232   std::string className;
00233   std::string branchName;
00234   std::ostringstream numString;
00235 
00236   if(typeName == "N8HCAL_HLX11LHC_SECTIONE"){
00237     className = "HCAL_HLX::LHC_SECTION";
00238     branchName = "LHC";
00239   }else if(typeName == "N8HCAL_HLX17OCCUPANCY_SECTIONE"){
00240     className = "HCAL_HLX::OCCUPANCY_SECTION";
00241     branchName = "Occupancy";
00242   }else if(typeName == "N8HCAL_HLX14ET_SUM_SECTIONE"){
00243     className = "HCAL_HLX::ET_SUM_SECTION";
00244     branchName = "ETSum";
00245   }
00246 
00247 #ifdef DEBUG
00248   std::cout << "Class: " << className << std::endl;
00249   std::cout << "Class: " << typeid(T).name() << std::endl;
00250 #endif
00251   
00252   numString << std::setfill('0') << std::setw(2) << HLXNum;
00253   branchName = branchName + numString.str() + ".";
00254   m_tree->Bronch(branchName.c_str(), className.c_str(), out, 1);
00255 
00256 #ifdef DEBUG
00257   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00258 #endif
00259 }
00260 
00261 void HCAL_HLX::ROOTFileBase::InsertInformation(){
00262   // This information will eventually come from the lms cell
00263   Threshold->OccThreshold1Set1 = 51;
00264   Threshold->OccThreshold2Set1 = 52;
00265   Threshold->OccThreshold1Set2 = 53;
00266   Threshold->OccThreshold2Set2 = 54;
00267   Threshold->ETSum             = 55;
00268 
00269   L1Trigger->L1lineNumber  = 71;
00270   L1Trigger->L1Scaler      = 72;
00271   L1Trigger->L1RateCounter = 73;
00272   
00273   HLT->TriggerPath    = 81;
00274   HLT->InputCount     = 82;
00275   HLT->AcceptCount    = 83;
00276   HLT->PrescaleFactor = 84;
00277 
00278   TriggerDeadtime->TriggerDeadtime = 91;
00279 
00280   RingSet->Set1Rings = "33L,34L";
00281   RingSet->Set2Rings = "35S,36S";
00282   RingSet->EtSumRings = "33L,34L,35S,36S";
00283 }
00284 
00285 void HCAL_HLX::ROOTFileBase::CloseTree(){
00286 #ifdef DEBUG
00287   std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00288 #endif
00289 
00290   if(fileName_ == ""){
00291     std::cout << "*** File Name was not set ***" << std::endl;
00292     return;
00293   }
00294 
00295   m_file->Write();
00296   m_file->Close();
00297 
00298   //delete m_tree; // NO!!! root does this when you delete m_file
00299   if(m_file != NULL){
00300     delete m_file;
00301     m_file = NULL;
00302     m_tree = NULL;
00303   }
00304 
00305 #ifdef DEBUG
00306   std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00307 #endif
00308 }

Generated on Tue Jun 9 17:44:04 2009 for CMSSW by  doxygen 1.5.4