CMS 3D CMS Logo

HPDNoiseReader.cc

Go to the documentation of this file.
00001 // --------------------------------------------------------
00002 // Engine to read HPD noise events from the library
00003 // Project: HPD noise library
00004 // Author: F.Ratnikov UMd, Jan. 15, 2008
00005 // $Id: HPDNoiseReader.cc,v 1.2 2008/01/17 23:35:53 fedor Exp $
00006 // --------------------------------------------------------
00007 
00008 #include "SimCalorimetry/HcalSimAlgos/interface/HPDNoiseReader.h"
00009 
00010 #include "TFile.h"
00011 #include "TTree.h"
00012 #include "TBranch.h"
00013 
00014 #include <iostream>
00015 
00016 #include "SimCalorimetry/HcalSimAlgos/interface/HPDNoiseData.h"
00017 #include "SimCalorimetry/HcalSimAlgos/interface/HPDNoiseDataCatalog.h"
00018 
00019 HPDNoiseReader::HPDNoiseReader (const std::string& fFileName) {
00020   mFile = new TFile (fFileName.c_str(), "READ");
00021   mBuffer = new HPDNoiseData ();
00022   HPDNoiseDataCatalog* catalog;
00023   mFile->GetObject ("HPDNoiseDataCatalog;1", catalog);
00024   if (catalog) {
00025     // initiate trees
00026     const std::vector<std::string> names = catalog->allNames();
00027     for (size_t i = 0; i < names.size(); ++i) {
00028       TTree* tree = (TTree*) mFile->Get (names[i].c_str());
00029       if (tree) {
00030         mTrees.push_back (tree);
00031         mRates.push_back (catalog->getRate (i));
00032         mCurrentEntry.push_back (0);
00033       }
00034       else {
00035         std::cerr << "HPDNoiseReader::HPDNoiseReader-> Can not open tree " << names[i] << " in file " << fFileName << std::endl;
00036       }
00037     }
00038   }
00039   else {
00040     std::cerr << "HPDNoiseReader::HPDNoiseReader-> Can not open catalog infile " << fFileName << std::endl;
00041   }
00042 }
00043 
00044 HPDNoiseReader::~HPDNoiseReader () {
00045   for (size_t i = 0; i < mTrees.size(); ++i) {
00046     delete mTrees[i];
00047   }
00048   delete mFile;
00049   delete mBuffer;
00050 }
00051 
00052 
00053 std::vector<std::string> HPDNoiseReader::allNames () const {
00054   std::vector<std::string> result;
00055   for (size_t i = 0; i < mTrees.size(); ++i) result.push_back (mTrees[i]->GetName ());
00056   return result;
00057 }
00058 
00059 HPDNoiseReader::Handle HPDNoiseReader::getHandle (const std::string& fName) {
00060   for (size_t i = 0; i < mTrees.size(); ++i) {
00061     if (std::string (mTrees[i]->GetName ()) == fName) return i;
00062   }
00063   return -1;
00064 }
00065 
00066 float HPDNoiseReader::rate (Handle fHandle) const {
00067   if (!valid (fHandle)) return 0;
00068   return mRates[fHandle];
00069 }
00070 
00071 unsigned long HPDNoiseReader::totalEntries (Handle fHandle) const {
00072   if (!valid (fHandle)) return 0;
00073   return mTrees[fHandle]->GetEntries ();
00074 }
00075 
00076 void HPDNoiseReader::grabEntry (Handle fHandle, unsigned long fEntry) {
00077   if (!valid (fHandle)) return;
00078   TBranch* branch = mTrees[fHandle]->GetBranch ("HPDNoiseData");
00079   branch->SetAddress (&mBuffer);
00080   branch->GetEntry (fEntry);
00081   mCurrentEntry [fHandle] = fEntry;
00082 }
00083 
00084 void HPDNoiseReader::getEntry (Handle fHandle, HPDNoiseData** fData) {
00085   if (!valid (fHandle)) return;
00086   unsigned int entry = mCurrentEntry [fHandle];
00087   if (++entry >= totalEntries (fHandle)) entry = 0; // roll over  
00088   grabEntry (fHandle, entry);
00089   *fData = mBuffer;
00090 }
00091 
00092 void HPDNoiseReader::getEntry (Handle fHandle, unsigned long fEntry, HPDNoiseData** fData) {
00093   grabEntry (fHandle, fEntry);
00094   *fData = mBuffer;
00095 }
00096 

Generated on Tue Jun 9 17:46:23 2009 for CMSSW by  doxygen 1.5.4