ATLAS Offline Software
Loading...
Searching...
No Matches
SCT_SimpleHisto.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3/*
4 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5*/
6
14#ifndef SCT_SimpleHisto_h
15#define SCT_SimpleHisto_h
16
17#include <map>
18#include <string>
19#include <sstream>
20
22 typedef std::map<float, unsigned int> S_t;
23
25 bool
26 init(S_t& s, const float xlo, const float xhi, const unsigned int nbins) {
27 if ((xhi <= xlo) or (nbins<2)) return false;
28 float increment{(xhi-xlo)/nbins};
29 float acc{xlo};
30 unsigned int init{0};
31 for (unsigned int i{0}; i!=nbins; ++i) {
32 s[acc]=init;
33 acc+=increment;
34 }
35 return (s.size()==nbins); //should be true now
36 }//initHisto function
37
38 //increment a bin
39 int
40 fill(S_t& s, const float value) {
41 S_t::const_iterator start{s.begin()};
42 S_t::const_iterator end{s.end()};
43 unsigned int nbins{static_cast<unsigned int>(s.size())};
44 if (value * nbins > ((--end)->first - start->first) * (nbins+1)) return 1;
45 S_t::iterator i=s.upper_bound(value);
46 if (i==start) return -1;
47 ++((--i)->second);
48 return 0;
49 }//fill function
50
51 //return xml string representation
52 std::string
53 asXmlString(const S_t& s) {
54 std::ostringstream os;
55 os<<"<histogram>\n";
56 for (S_t::const_iterator i{s.begin()}; i!=s.end(); ++i) {
57 os<<"<b x=\""<<i->first<<"\">"<<i->second<<"</b>\n";
58 }
59 os<<"</histogram>\n";
60 return os.str();
61 }//asXmlString
62
63 std::string
64 xmlHeader(const std::string& version="1.0", const std::string& encoding="UTF-8") {
65 return "<?xml version=\""+version+"\" encoding=\""+encoding+"\" standalone=\"yes\"?>";
66 }
67 std::string
68 stylesheet(const std::string& path) {
69 return "<?xml-stylesheet type=\"text/xsl\" href=\""+path+"\"?>";
70 }
71
72}//namespace
73
74#endif // SCT_SimpleHisto_h
SCT_SimpleHisto.h C++ projects.
std::string xmlHeader(const std::string &version="1.0", const std::string &encoding="UTF-8")
bool init(S_t &s, const float xlo, const float xhi, const unsigned int nbins)
Initialize a map to be used as a histogram.
std::map< float, unsigned int > S_t
int fill(S_t &s, const float value)
std::string stylesheet(const std::string &path)
std::string asXmlString(const S_t &s)