Crombie Tools
HistWriter.cc
Go to the documentation of this file.
1 /**
2  @file HistWriter.cc
3  Defines the HistWriter class.
4  @author Daniel Abercrombie <dabercro@mit.edu>
5 */
6 
7 #include <fstream>
8 #include <iostream>
9 #include "TFile.h"
10 #include "TH1F.h"
11 #include "TH2F.h"
12 
13 #include "HistWriter.h"
14 
16 
17 //--------------------------------------------------------------------
18 HistWriter::HistWriter(TString fileName, TString histName) :
19  fFileName(fileName),
20  fHistName(histName)
21 { }
22 
23 //--------------------------------------------------------------------
25 { }
26 
27 //--------------------------------------------------------------------
28 
29 /**
30  Reads the configuration file given by configName. The configuration
31  file consists only of alternating bin edge locations and bin contents.
32  The first and last number of the file should be a bin edge.
33  (The number of edges is the number of bins + 1).
34  These can be separated by spaces or new lines.
35 */
36 
37 void HistWriter::MakeHist(TString configName)
38 {
39  std::vector<Float_t> xVals;
40  std::vector<Float_t> binVals;
41 
42  std::ifstream configFile;
43  configFile.open(configName.Data());
44  TString xVal;
45  TString binVal;
46  Bool_t reading = true;
47  while (reading) {
48  configFile >> xVal >> binVal;
49  xVals.push_back(xVal.Atof());
50  if (binVal != "")
51  binVals.push_back(binVal.Atof());
52  else
53  reading = false;
54  }
55 
56  TFile* outFile = new TFile(fFileName, "UPDATE");
57  TH1F* outHist = new TH1F(fHistName, fHistName, binVals.size(), xVals.data());
58 
59  for (UInt_t iBin = 0; iBin != binVals.size(); ++iBin)
60  outHist->SetBinContent(iBin + 1, binVals[iBin]);
61 
62  outFile->WriteTObject(outHist, fHistName, "Overwrite");
63  outFile->Close();
64 
65  configFile.close();
66 }
67 
68 void HistWriter::Make2DHist(TString configName, unsigned numX, unsigned numY)
69 {
70  if (not numX or not numY)
71  exit(40);
72 
73  std::vector<Float_t> xVals;
74  std::vector<Float_t> yVals;
75  std::vector<Float_t> binVals;
76 
77  std::ifstream configFile;
78  configFile.open(configName.Data());
79  TString axisVal;
80  TString binVal;
81  // Read X values
82  for (unsigned xBin = 0; xBin < numX; xBin++) {
83  configFile >> axisVal;
84  xVals.push_back(axisVal.Atof());
85  }
86 
87  Bool_t reading = true;
88  while (reading) {
89  configFile >> axisVal;
90  yVals.push_back(axisVal.Atof());
91 
92  for (unsigned xBin = 1; xBin < numX; xBin++) {
93  configFile >> binVal;
94  if (binVal != "")
95  binVals.push_back(binVal.Atof());
96  else {
97  reading = false;
98  break;
99  }
100  }
101  }
102 
103  TFile* outFile = new TFile(fFileName, "UPDATE");
104  TH2F* outHist = new TH2F(fHistName, fHistName, numX - 1, xVals.data(), numY - 1, yVals.data());
105 
106  for (UInt_t xBin = 1; xBin < numX; ++xBin) {
107  for (UInt_t yBin = 1; yBin < numY; ++yBin)
108  outHist->SetBinContent(xBin, yBin, binVals[xBin - 1 + (numX - 1) * (yBin - 1)]);
109  }
110 
111  outFile->WriteTObject(outHist, fHistName, "Overwrite");
112  outFile->Close();
113 
114  configFile.close();
115 }