GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
his2root.cc
Go to the documentation of this file.
1#include <fstream>
2#include <iostream>
3#include <sstream>
4#include <string>
5
6#include <TFile.h>
7#include <TH1.h>
8#include <TH2.h>
9
18int main(int argc, char *argv[])
19{
20 int nHists = 0; // counter for number of histograms processed
21 const char *cHisFileName = argv[1];
22 std::cout << "Converting file " << cHisFileName << std::endl;
23 int big = 0;
24 if (argc > 2) {
25 big = std::atoi(argv[2]);
26 }
27 std::stringstream ss;
28 std::string line, sListFileName, sHisFileName(cHisFileName), sRootFileName;
29 sListFileName = sHisFileName.substr(0, sHisFileName.length() - 3);
30 sListFileName.append("list");
31 sRootFileName = sHisFileName.substr(0, sHisFileName.length() - 3);
32 sRootFileName.append("root");
33 std::ifstream listFile;
34 listFile.open(sListFileName);
35 std::string first;
36 int details = 0; // flag to mark if we're in the "detailed info" part of the
37 // list file or not
38
39 int HID; /*histogram ID*/
40 int dim; /* dimension of histogram: assuming 1 or 2 */
41 int hwpc; /* half words per channel */
42 // int len; /* length in characters of array */
43 // int compr; /* compression, not used */
44 int minX; /* minimum (X) value of array */
45 int maxX; /* max value (X) */
46 int minY; /* minimum (Y) value of array */
47 int maxY; /* max value (Y) */
48 std::string title; /* title of histogram */
49 uint32_t datum; /* 64 bit integer to store a single channel */
50
51 FILE *hisFile;
52 int read;
53 hisFile = std::fopen(cHisFileName, "rb");
54 TFile *outFile = new TFile(sRootFileName.c_str(), "recreate");
55
56 while (std::getline(listFile, line)) {
57 ss.clear();
58 ss.str(line);
59 ss >> first;
60 if (details == 1) { /* if past the list of IDs */
61 std::cout << "processing line: " << line << std::endl;
62 HID = stoi(line.substr(0, 5));
63 dim = stoi(line.substr(5, 5));
64 hwpc = stoi(line.substr(10, 4));
65 // compr = stoi(line.substr(23, 8));
66 minX = stoi(line.substr(31, 6));
67 maxX = stoi(line.substr(37, 6));
68 title = line.substr(54);
69
70 if (hwpc > 2) {
71 std::cout << "warning! more than 32 bit integer, expect bad behaviour" << std::endl;
72 }
73 if (dim == 2) { /* 2D histogram */
74 std::getline(listFile, line);
75 ss.clear();
76 ss.str(line);
77 minY = stoi(line.substr(31, 6));
78 maxY = stoi(line.substr(37, 6));
79
80 /* make the histogram */
81 TString sHistName;
82 sHistName.Form("ID%d", HID);
83 TH2D *hist = new TH2D(sHistName.Data(), title.c_str(), maxX - minX + 1, minX, maxX + 1, maxY - minY + 1,
84 minY, maxY + 1);
85
86 /* fill the histogram */
87 for (int iyChannel = 0; iyChannel <= maxY; iyChannel++) {
88 int bpc = hwpc*2;
89 uint8_t data[bpc*(maxX+1)];
90 std::fread(&data[0], (size_t)1, (size_t)(bpc*(maxX+1)), hisFile);
91
92 for (int ixChannel = 0; ixChannel <= maxX; ixChannel++) {
93 /* changer order of bytes */
94 uint32_t val = 0;
95 for (int i=0; i<bpc; ++i) {
96 if (big) {
97 val += data[bpc*ixChannel+bpc-i-1]<<(8*i);
98 } else {
99 val += data[bpc*ixChannel+i]<<(8*i);
100 }
101 }
102
103 hist->SetBinContent(ixChannel+1, iyChannel+1, val);
104 hist->SetBinError(ixChannel+1, iyChannel+1, std::sqrt(val));
105 }
106 }
107
108 hist->Write();
109 } else { /* 1D histogram */
110 TString sHistName;
111 sHistName.Form("ID%d", HID);
112 TH1D *hist = new TH1D(sHistName.Data(), title.c_str(), maxX - minX + 1, minX, maxX + 1);
113
114 /* fill the histogram */
115 int bpc = hwpc*2;
116 uint8_t data[bpc*(maxX+1)];
117 std::fread(&data[0], (size_t)1, (size_t)(bpc*(maxX+1)), hisFile);
118
119 for (int ixChannel = 0; ixChannel <= maxX; ixChannel++) {
120 /* changer order of bytes */
121 uint32_t val = 0;
122 for (int i=0; i<bpc; ++i) {
123 if (big) {
124 val += data[bpc*ixChannel+bpc-i-1]<<(8*i);
125 } else {
126 val += data[bpc*ixChannel+i]<<(8*i);
127 }
128 }
129
130 hist->SetBinContent(ixChannel+1, val);
131 hist->SetBinError(ixChannel+1, std::sqrt(val));
132 }
133
134 hist->Write();
135 }
136
137 nHists = nHists + 1;
138 }
139 if (first == "HID") {
140 details = 1;
141 }
142
143 } // *.list file loop
144 outFile->Close();
145 return nHists;
146} // his2root
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
int main(int argc, char *argv[])
Definition his2root.cc:18