ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/dhidas/OSUAnalysis/Tools/interface/HistHelpers/THCollection.h
Revision: 1.1.1.1 (vendor branch)
Committed: Thu Dec 1 16:28:48 2011 UTC (13 years, 5 months ago) by dhidas
Content type: text/plain
Branch: dhidas, MAIN
CVS Tags: START, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
osu copy modified

File Contents

# Content
1 /*
2 * THCollection.h
3 *
4 * Created on: 9 Aug 2010
5 * Author: kreczko
6 */
7
8 #ifndef THCOLLECTION_H_
9 #define THCOLLECTION_H_
10 #include <string>
11
12 #include "TFile.h"
13 #include "TDirectory.h"
14 #include "TH1.h"
15 #include "TH2.h"
16
17 #include <boost/unordered_map.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/foreach.hpp>
20 #include <iostream>
21 #include "StringOperations.h"
22
23 using namespace boost;
24 namespace BAT {
25
26 template<class histType>
27 class THCollection {
28 typedef shared_ptr<histType> histPointer;
29 typedef unordered_map<std::string, histPointer> map;
30
31 protected:
32 boost::shared_ptr<TFile> histogramFile;
33 std::string path;
34 std::vector<std::string> directories;
35 map histMap;
36 std::string prefix, suffix;
37 public:
38 // THCollection() :
39 // histogramFile(), directories(), histMap() {
40 //
41 // }
42
43 THCollection(std::string virtualPath = "") :
44 histogramFile(),
45 path(virtualPath),
46 directories(getDirectoriesFromPath(path)),
47 histMap(),
48 prefix(""),
49 suffix("") {
50
51 }
52
53 ~THCollection() {
54
55 }
56
57 void add(std::string name, std::string title, unsigned int numberOfBins, float xmin, float xmax);
58 void add(std::string name, std::string title, unsigned int numberOfXBins, float xmin, float xmax,
59 unsigned int numberOfYBins, float ymin, float ymax);
60
61 void add(std::string name, boost::shared_ptr<histType> hist){
62 histMap[name] = hist;
63 }
64
65 boost::shared_ptr<histType> get(std::string name) {
66 histPointer histp = histMap[name];
67 if (histp == NULL)
68 cout << "\n **** Histogram " << name << " not defined. **** \n\n";
69 return histMap[name];
70 }
71
72 unsigned int size() const {
73 return histMap.size();
74 }
75
76 void writeToFile(boost::shared_ptr<TFile> histFile) {
77 histogramFile = histFile;
78 histogramFile->cd();
79 writeDirectories();
80 histogramFile->Cd(path.c_str());
81 writeHistograms();
82 }
83
84 void setPrefix(std::string pre){
85 prefix = pre;
86 }
87
88 void setSuffix(std::string suf){
89 suffix = suf;
90 }
91
92 map getAllHistograms(){
93 return histMap;
94 }
95
96 private:
97
98 void writeDirectories() {
99 std::string currentPath = "";
100
101 for (unsigned short index = 0; index < directories.size(); ++index) {
102 const std::string dir = directories.at(index);
103
104 if (index == 0) {
105 histogramFile->mkdir(dir.c_str());
106 currentPath = dir;
107 } else {
108 TDirectory* currentDir = (TDirectory*) histogramFile->Get(currentPath.c_str());
109 assert(currentDir != 0);
110 currentDir->mkdir(dir.c_str());
111 currentPath += "/" + dir;
112 }
113 }
114 }
115 void writeHistograms() {
116 for (typename map::const_iterator iter = histMap.begin(); iter != histMap.end(); ++iter) {
117 std::string newName(iter->second->GetName());
118 if (prefix != "") {
119 newName = prefix + "_" + newName;
120 }
121 if (suffix != "")
122 newName = newName + "_" + suffix;
123 iter->second->Write(newName.c_str());
124 }
125 }
126
127 };
128
129 typedef THCollection<TH1> TH1Collection;
130 typedef THCollection<TH2> TH2Collection;
131 typedef boost::shared_ptr<THCollection<TH1> > TH1CollectionRef;
132 typedef boost::shared_ptr<THCollection<TH2> > TH2CollectionRef;
133
134 }
135
136 #endif /* THCOLLECTION_H_ */