Anp::HistMan Class Reference

#include <HistMan.h>

List of all members.

Public Types

typedef std::map< std::string,
HistInfo
InfoMap
typedef InfoMap::const_iterator InfoIter
typedef std::map< std::string,
InfoMap
DirMap
typedef DirMap::const_iterator DirIter

Public Member Functions

bool ReadFile (const std::string file)
bool KeyExists (int key, const std::string &dir=".") const
bool KeyExists (const std::string &key, const std::string &dir=".") const
unsigned int NMiss () const
const Hist1d< double > CreateHist1d (int key, const std::string &dir=".")
const Hist1d< double > CreateHist1d (const std::string &key, const std::string &dir=".")
TH1 * CreateTH1 (int key, const std::string &dir=".")
TH2 * CreateTH2 (int key, const std::string &dir=".")
TH1 * CreateTH1 (const std::string &key, const std::string &dir=".")
TH2 * CreateTH2 (const std::string &key, const std::string &dir=".")
TH2 * GetTH2 (int xkey, int ykey, const std::string &dir=".")
TH2 * GetTH2 (const std::string &xkey, const std::string &ykey, const std::string &dir=".")
void Print (std::ostream &o=std::cout) const

Static Public Member Functions

static HistManInstance ()

Private Member Functions

 HistMan ()
 ~HistMan ()
 HistMan (const HistMan &)
const HistManoperator= (const HistMan &)
const Hist1d< double > CreateHist1d (const HistInfo &info)
TH1 * CreateTH1 (const HistInfo &info, const std::string &opt="double")
TH2 * CreateTH2 (const HistInfo &info, const std::string &opt="float")
bool ReadHistogramBlock (TiXmlElement &hist)
bool ReadHistogram (const TiXmlElement &hist, HistInfo &info)
bool ReadAxis (const TiXmlElement &hist, AxisInfo &info)
const std::vector< int > GetIntVec (const std::string &list) const
const std::vector< std::string > GetStringVec (const std::string &list) const
const std::string Convert (int key, int width=0) const
unsigned short ComputeWidth (int key) const

Private Attributes

unsigned int fNMiss
unsigned short fKeyWidth
DirMap fDirMap

Static Private Attributes

static HistManfgInstance = 0
 do not stream
static Mutex fMutex
 do not stream

Detailed Description

Definition at line 88 of file PhysicsNtuple/HEAD/Hist/HistMan.h.


Member Typedef Documentation

typedef DirMap::const_iterator Anp::HistMan::DirIter

Definition at line 95 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

typedef std::map<std::string, InfoMap> Anp::HistMan::DirMap

Definition at line 94 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

typedef InfoMap::const_iterator Anp::HistMan::InfoIter

Definition at line 93 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

typedef std::map<std::string, HistInfo> Anp::HistMan::InfoMap

Definition at line 92 of file PhysicsNtuple/HEAD/Hist/HistMan.h.


Constructor & Destructor Documentation

HistMan::HistMan (  )  [private]

Definition at line 56 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

Referenced by Instance().

00057    :fNMiss(0),
00058     fKeyWidth(4)
00059 {
00060 }

HistMan::~HistMan (  )  [private]

Definition at line 63 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

00064 {
00065 }

Anp::HistMan::HistMan ( const HistMan  )  [private]

Member Function Documentation

unsigned short HistMan::ComputeWidth ( int  key  )  const [private]

Definition at line 865 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

Referenced by Convert(), and ReadHistogramBlock().

00866 {
00867    //
00868    // Compute key width
00869    //
00870    return static_cast<unsigned int>(std::floor(std::log10(double(std::abs(key) + 1)))) + 1;
00871 }

const string HistMan::Convert ( int  key,
int  width = 0 
) const [private]

Definition at line 849 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

References ComputeWidth().

00850 {
00851    stringstream strkey;
00852    if(width == 0)
00853    {
00854       strkey << setw(ComputeWidth(key)) << key;
00855    }
00856    else
00857    {
00858       strkey << setfill('0') << setw(width) << key;
00859    }
00860 
00861    return strkey.str();
00862 }

const Anp::Hist1d< double > HistMan::CreateHist1d ( const HistInfo info  )  [private]

Definition at line 643 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

References Anp::AxisInfo::GetBins(), Anp::AxisInfo::GetMax(), Anp::AxisInfo::GetMin(), Anp::AxisInfo::GetNbins(), and Anp::HistInfo::GetXaxis().

00644 {
00645    const AxisInfo &xaxis = info.GetXaxis();
00646 
00647    if(!xaxis.GetBins().empty())
00648    {
00649       return Hist1d<double>(xaxis.GetBins());
00650    }
00651 
00652    return Hist1d<double>(xaxis.GetNbins(), xaxis.GetMin(), xaxis.GetMax());      
00653 }

const Hist1d<double> Anp::HistMan::CreateHist1d ( const std::string &  key,
const std::string &  dir = "." 
)
const Hist1d<double> Anp::HistMan::CreateHist1d ( int  key,
const std::string &  dir = "." 
)
TH1* Anp::HistMan::CreateTH1 ( const HistInfo info,
const std::string &  opt = "double" 
) [private]
TH1* Anp::HistMan::CreateTH1 ( const std::string &  key,
const std::string &  dir = "." 
)
TH1* Anp::HistMan::CreateTH1 ( int  key,
const std::string &  dir = "." 
)
TH2* Anp::HistMan::CreateTH2 ( const HistInfo info,
const std::string &  opt = "float" 
) [private]
TH2* Anp::HistMan::CreateTH2 ( const std::string &  key,
const std::string &  dir = "." 
)
TH2* Anp::HistMan::CreateTH2 ( int  key,
const std::string &  dir = "." 
)
const std::vector<int> Anp::HistMan::GetIntVec ( const std::string &  list  )  const [private]

Referenced by ReadHistogramBlock().

const std::vector<std::string> Anp::HistMan::GetStringVec ( const std::string &  list  )  const [private]

Referenced by ReadHistogramBlock().

TH2* Anp::HistMan::GetTH2 ( const std::string &  xkey,
const std::string &  ykey,
const std::string &  dir = "." 
)
TH2* Anp::HistMan::GetTH2 ( int  xkey,
int  ykey,
const std::string &  dir = "." 
)

Referenced by Anp::Plot::Data::Get().

Anp::HistMan & HistMan::Instance ( void   )  [static]
bool Anp::HistMan::KeyExists ( const std::string &  key,
const std::string &  dir = "." 
) const
bool Anp::HistMan::KeyExists ( int  key,
const std::string &  dir = "." 
) const
unsigned int HistMan::NMiss (  )  const

Definition at line 874 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

References fMutex, and fNMiss.

Referenced by Anp::PmtHit::Init(), and Anp::PlotTrackGeom::Set().

00875 {
00876    Anp::Lock<Mutex> lock(fMutex);
00877    return fNMiss;
00878 }

const HistMan& Anp::HistMan::operator= ( const HistMan  )  [private]
void Anp::HistMan::Print ( std::ostream &  o = std::cout  )  const
bool HistMan::ReadAxis ( const TiXmlElement hist,
AxisInfo info 
) [private]

Definition at line 373 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

References TiXmlElement::Attribute(), Anp::AxisInfo::fBins, Anp::AxisInfo::fMax, Anp::AxisInfo::fMin, Anp::AxisInfo::fNbins, Anp::AxisInfo::fTitle, Anp::AxisInfo::fValid, max, min, TiXmlElement::QueryFloatAttribute(), TiXmlElement::QueryIntAttribute(), size, UtilString::StringTok(), and TIXML_SUCCESS.

Referenced by ReadHistogram().

00374 {
00375    bool read_bin = false, read_bins = false;
00376  
00377    int bin = 0;
00378    float min = 0.0, max = 0.0;
00379 
00380    if(axis.QueryIntAttribute("bin", &bin)   == TIXML_SUCCESS &&
00381       axis.QueryFloatAttribute("min", &min) == TIXML_SUCCESS &&
00382       axis.QueryFloatAttribute("max", &max) == TIXML_SUCCESS)
00383    {
00384       read_bin = true;
00385    }
00386    
00387    const char *xtitle = axis.Attribute("title");
00388    if(xtitle)
00389    {
00390       info.fTitle = xtitle;
00391    }
00392    
00393    vector<double> bvec;
00394    
00395    const char *bins = axis.Attribute("bins");
00396    if(bins)
00397    {
00398       vector<string> datavec;
00399       UtilString::StringTok(datavec, bins, ", ");
00400       
00401       for(vector<string>::const_iterator sit = datavec.begin(); sit != datavec.end(); ++sit)
00402       {
00403          if(sit -> size() > 0)
00404          {
00405            bvec.push_back(std::atof(sit -> c_str()));
00406          }
00407       }
00408 
00409       std::sort(bvec.begin(), bvec.end());
00410 
00411       //for(unsigned int i = 0; i < bvec.size(); ++i)
00412       //{
00413       //cout << "bin = " << i << " edge = " << bvec[i] << endl;
00414       //}
00415 
00416       if(!bvec.empty())
00417       {
00418          read_bins = true;
00419       }
00420    }
00421    
00422    if(read_bin)
00423    {
00424       info.fNbins = bin;
00425       info.fMin = min;
00426       info.fMax = max;
00427       info.fValid = true;
00428    }
00429    else if(read_bins)
00430    {
00431       info.fBins = bvec;
00432    }
00433   
00434    return true;
00435 }

bool Anp::HistMan::ReadFile ( const std::string  file  ) 
bool HistMan::ReadHistogram ( const TiXmlElement hist,
HistInfo info 
) [private]

Definition at line 279 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

References TiXmlElement::Attribute(), TiXmlNode::ELEMENT, TiXmlNode::FirstChild(), Anp::HistInfo::fKey, Anp::HistInfo::fName, Anp::AxisInfo::fTitle, Anp::HistInfo::fTitle, Anp::HistInfo::fXaxis, Anp::HistInfo::fYaxis, ReadAxis(), and TiXmlNode::Value().

Referenced by ReadHistogramBlock().

00280 {
00281    if(hist.Value() && strcmp(hist.Value(), "h1d") != 0)
00282    {
00283       return false;
00284    }
00285 
00286    AxisInfo xaxis, yaxis;
00287  
00288    bool axis1dok = false, axis2dok = false;
00289    for(const TiXmlNode *pChild = hist.FirstChild(); pChild != 0; pChild = pChild -> NextSibling()) 
00290    {
00291       if(!pChild)
00292       {
00293          cerr << "Null child..." << endl;
00294          continue;
00295       }
00296 
00297       if(pChild -> Type() != TiXmlNode::ELEMENT)
00298       {
00299          continue;
00300       }
00301       
00302       const TiXmlElement* axis = pChild -> ToElement();
00303       if(!axis)
00304       {
00305          cerr << "Failed to cast xml node to element..." << endl;
00306          continue;
00307       }
00308       
00309       if(!axis -> Value())
00310       {
00311          cerr << "Node has no value" << endl;
00312          continue;
00313       }
00314 
00315       if(strcmp(axis -> Value(), "axis1d") == 0 && ReadAxis(*axis, xaxis))
00316       {
00317          axis1dok = true;
00318       }
00319       if(strcmp(axis -> Value(), "axis2d") == 0 && ReadAxis(*axis, yaxis))
00320       {
00321          axis2dok = true;
00322       }
00323    }
00324 
00325    if(!axis1dok)
00326    {
00327       cerr << "Failed to read 1d axis info" << endl;
00328       return false;
00329    }
00330    else
00331    {
00332       info.fXaxis = xaxis;
00333    }
00334 
00335    if(axis2dok)
00336    {
00337       info.fYaxis = yaxis;
00338    }
00339 
00340    const char *key = hist.Attribute("key");
00341    if(key)
00342    {
00343       info.fKey = string(key);
00344    }
00345    else
00346    {
00347       cerr << "HistMan::ReadHistogram() - key string is empty" << endl;
00348       return false;
00349    }   
00350 
00351    const char *name = hist.Attribute("name");
00352    if(name)
00353    {
00354       info.fName = name;
00355    }
00356 
00357    const char *title = hist.Attribute("title");
00358    if(title)
00359    {
00360       info.fTitle = title;
00361    }
00362 
00363    const char *axis = hist.Attribute("axis");
00364    if(axis)
00365    {
00366       info.fXaxis.fTitle = axis;
00367    }
00368 
00369    return true;
00370 }

bool HistMan::ReadHistogramBlock ( TiXmlElement hist  )  [private]

Definition at line 121 of file PhysicsNtuple/HEAD/Hist/HistMan.cxx.

References base, ComputeWidth(), TiXmlNode::ELEMENT, fDirMap, TiXmlNode::FirstChild(), Anp::HistInfo::fKey, fKeyWidth, GetIntVec(), Anp::HistInfo::GetKey(), GetStringVec(), Anp::HistInfo::GetTitle(), info, it, ReadHistogram(), and Munits::second.

00122 {
00123    InfoMap hmap;
00124    vector<vector<int> > basevec;
00125    vector<string> dirvec;
00126 
00127    for(TiXmlNode *pChild = block.FirstChild(); pChild != 0; pChild = pChild -> NextSibling()) 
00128    {
00129       if(!pChild)
00130       {
00131          cerr << "HistMan::ReadFile - null child..." << endl;
00132          continue;
00133       }
00134 
00135       if(pChild -> Type() != TiXmlNode::ELEMENT)
00136       {
00137          continue;
00138       }
00139       
00140       TiXmlElement* hist = pChild -> ToElement();
00141       if(!hist)
00142       {
00143          cerr << "HistMan::ReadFile - failed to cast xml node to element..." << endl;
00144          continue;
00145       }
00146 
00147       if(!hist -> Value())
00148       {
00149          cerr << "HistMan::ReadFile - node has no value" << endl;
00150          continue;
00151       }
00152 
00153       if(strcmp(hist -> Value(), "keybase") == 0 && hist -> Attribute("keys"))
00154       {
00155          const vector<int> ivec = GetIntVec(hist -> Attribute("keys"));
00156          if(!ivec.empty())
00157          {
00158             basevec.push_back(ivec);
00159          }
00160          
00161          continue;
00162       }
00163 
00164       if(strcmp(hist -> Value(), "dirbase") == 0 && hist -> Attribute("dirs"))
00165       {
00166          const vector<string> svec = GetStringVec(hist -> Attribute("dirs"));
00167          if(!svec.empty())
00168          {
00169             dirvec.insert(dirvec.end(), svec.begin(), svec.end());
00170          }
00171 
00172          continue;
00173       }
00174 
00175       HistInfo info;      
00176       if(strcmp(hist -> Value(), "h1d") != 0 || !ReadHistogram(*hist, info))
00177       {
00178          continue;
00179       }
00180 
00181       if(!hmap.insert(InfoMap::value_type(info.GetKey(), info)).second)
00182       {
00183          cerr << "HistMan::ReadFile - histogram key " << info.GetKey() << " already exists" << endl;
00184       }
00185 
00186       const int intkey = std::atoi(info.GetKey().c_str());
00187       if(intkey != 0)
00188       {
00189          const unsigned int width = ComputeWidth(intkey);
00190          fKeyWidth = max<int>(width, fKeyWidth);
00191 
00192          if(width != info.GetKey().size())
00193          {
00194            cerr << "HistMan::ReadFile - integer width and string size are not equal for key " <<info.GetKey()<<" "<<info.GetTitle()<< endl;
00195          }
00196       }
00197    }
00198    
00199    vector<int> allvec;
00200    for(vector<vector<int> >::const_iterator bit = basevec.begin(); bit != basevec.end(); ++bit)
00201    {
00202       const vector<int> prevvec = allvec;
00203       const vector<int> &currvec = *bit;
00204       
00205       for(vector<int>::const_iterator cit = currvec.begin(); cit != currvec.end(); ++cit)
00206       {
00207          if(prevvec.empty())
00208          {
00209             allvec.push_back(*cit);
00210          }
00211          else
00212          {
00213             for(vector<int>::const_iterator pit = prevvec.begin(); pit != prevvec.end(); ++pit)
00214             {
00215                allvec.push_back(*cit + *pit);
00216             }
00217          } 
00218       }
00219    }
00220 
00221    InfoMap nmap = hmap;
00222 
00223    for(vector<int>::const_iterator it = allvec.begin(); it != allvec.end(); ++it)
00224    {
00225       const int base = *it;
00226 
00227       for(InfoMap::const_iterator hit = hmap.begin(); hit != hmap.end(); ++hit)
00228       {
00229          const int oldkey = std::atoi((hit -> first).c_str());
00230          if(oldkey == 0)
00231          {
00232             continue;
00233          }
00234 
00235          const int newkey = base + oldkey;
00236          HistInfo info = hit -> second;
00237 
00238          stringstream strkey;
00239          strkey << newkey;
00240          info.fKey = strkey.str();
00241 
00242          if(!nmap.insert(InfoMap::value_type(info.GetKey(), info)).second)
00243          {
00244             cerr << "HistMan::ReadFile - histogram key " << info.GetKey() << " already exists" << endl;
00245          }
00246 
00247          const unsigned int width = ComputeWidth(newkey);
00248          fKeyWidth = max<int>(width, fKeyWidth);
00249 
00250          if(width != info.GetKey().size())
00251          {
00252             cerr << "HistMan::ReadFile - integer width and string size are not equal" << endl;
00253          }
00254       }
00255    }
00256 
00257    if(dirvec.empty())
00258    {
00259       dirvec.push_back(".");
00260    }
00261 
00262    for(vector<string>::const_iterator dit = dirvec.begin(); dit != dirvec.end(); ++dit)
00263    {
00264       InfoMap &imap = fDirMap[*dit];
00265 
00266       for(InfoMap::const_iterator nit = nmap.begin(); nit != nmap.end(); ++nit)
00267       {
00268          if(!imap.insert(InfoMap::value_type(*nit)).second)
00269          {
00270             cerr << "HistMan::ReadFile - failed to add " << nit -> first << " to " << *dit << endl;
00271          }
00272       }
00273    }
00274 
00275    return true;
00276 }


Member Data Documentation

Definition at line 153 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

Referenced by ReadHistogramBlock().

Anp::HistMan * HistMan::fgInstance = 0 [static, private]

do not stream

Definition at line 155 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

Referenced by Instance().

unsigned short Anp::HistMan::fKeyWidth [private]

Definition at line 151 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

Referenced by ReadHistogramBlock().

Anp::Mutex HistMan::fMutex [static, private]

do not stream

Definition at line 157 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

Referenced by Instance(), and NMiss().

unsigned int Anp::HistMan::fNMiss [private]

Definition at line 149 of file PhysicsNtuple/HEAD/Hist/HistMan.h.

Referenced by NMiss().


The documentation for this class was generated from the following files:

Generated on 15 Jul 2018 for loon by  doxygen 1.6.1