CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/SimCalorimetry/HcalSimAlgos/src/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.4 2008/08/04 22:07:08 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::objectName(), 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         mDischargeRate.push_back (catalog->getDischargeRate (i));
00032         mIonFeedbackFirstPeakRate.push_back(catalog->getIonFeedbackFirstPeakRate(i));
00033         mIonFeedbackSecondPeakRate.push_back(catalog->getIonFeedbackSecondPeakRate(i));
00034         mElectronEmissionRate.push_back(catalog->getElectronEmissionRate(i));
00035         mCurrentEntry.push_back (0);
00036       }
00037       else {
00038         std::cerr << "HPDNoiseReader::HPDNoiseReader-> Can not open tree " << names[i] << " in file " << fFileName << std::endl;
00039       }
00040     }
00041   }
00042   else {
00043     std::cerr << "HPDNoiseReader::HPDNoiseReader-> Can not open catalog infile " << fFileName << std::endl;
00044   }
00045 }
00046 
00047 HPDNoiseReader::~HPDNoiseReader () {
00048   for (size_t i = 0; i < mTrees.size(); ++i) {
00049     delete mTrees[i];
00050   }
00051   delete mFile;
00052   delete mBuffer;
00053 }
00054 
00055 
00056 std::vector<std::string> HPDNoiseReader::allNames () const {
00057   std::vector<std::string> result;
00058   for (size_t i = 0; i < mTrees.size(); ++i) result.push_back (mTrees[i]->GetName ());
00059   return result;
00060 }
00061 
00062 HPDNoiseReader::Handle HPDNoiseReader::getHandle (const std::string& fName) {
00063   for (size_t i = 0; i < mTrees.size(); ++i) {
00064     if (std::string (mTrees[i]->GetName ()) == fName) return i;
00065   }
00066   return -1;
00067 }
00068 
00069 float HPDNoiseReader::dischargeRate (Handle fHandle) const {
00070   if (!valid (fHandle)) return 0;
00071   return mDischargeRate[fHandle];
00072 }
00073 float HPDNoiseReader::ionFeedbackFirstPeakRate (Handle fHandle) const {
00074   if (!valid (fHandle)) return 0;
00075   return mIonFeedbackFirstPeakRate[fHandle];
00076 }
00077 float HPDNoiseReader::ionFeedbackSecondPeakRate (Handle fHandle) const {
00078   if (!valid (fHandle)) return 0;
00079   return mIonFeedbackSecondPeakRate[fHandle];
00080 }
00081 float HPDNoiseReader::emissionRate (Handle fHandle) const {
00082   if (!valid (fHandle)) return 0;
00083   return mElectronEmissionRate[fHandle];
00084 }
00085 
00086 unsigned long HPDNoiseReader::totalEntries (Handle fHandle) const {
00087   if (!valid (fHandle)) return 0;
00088   return mTrees[fHandle]->GetEntries ();
00089 }
00090 
00091 void HPDNoiseReader::grabEntry (Handle fHandle, unsigned long fEntry) {
00092   if (!valid (fHandle)) return;
00093   TBranch* branch = mTrees[fHandle]->GetBranch (HPDNoiseData::branchName());
00094   branch->SetAddress (&mBuffer);
00095   branch->GetEntry (fEntry);
00096   mCurrentEntry [fHandle] = fEntry;
00097 }
00098 
00099 void HPDNoiseReader::getEntry (Handle fHandle, HPDNoiseData** fData) {
00100   if (!valid (fHandle)) return;
00101   unsigned int entry = mCurrentEntry [fHandle];
00102   if (++entry >= totalEntries (fHandle)) entry = 0; // roll over  
00103   grabEntry (fHandle, entry);
00104   *fData = mBuffer;
00105 }
00106 
00107 void HPDNoiseReader::getEntry (Handle fHandle, unsigned long fEntry, HPDNoiseData** fData) {
00108   grabEntry (fHandle, fEntry);
00109   *fData = mBuffer;
00110 }
00111