GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
bin2root.cc
Go to the documentation of this file.
1
2// ./bin2root rootspe.root (J.M. Allmond - ORNL - 3/2018) //
3// //
4// Creates a *.root file from all *.mat, *.spn, and *.sec //
5// files in current directory //
7
8#include <assert.h>
9#include <dirent.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <fstream>
13#include <iomanip>
14#include <iostream>
15#include <math.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include "TCutG.h"
22#include "TFile.h"
23#include "TH1.h"
24#include "TH2.h"
25#include "TObject.h"
26#include "TROOT.h"
27
28using namespace std;
29
30TH1S *mat1d;
31TH2S *mat2d;
32TH1I *spn1d, *sec1d;
33TH2I *spn2d, *sec2d;
34
35int main(int argc, char **argv)
36{
37
38 char str1[127], str2[127], *stra, *strb;
39
40 FILE *fp;
41 short mat[4096] = {0};
42 int spn[4096] = {0};
43 int sec[8192] = {0};
44 int ydim;
45
46 if (argc < 2) {
47 std::cout << "No input file specified" << std::endl;
48 return -1;
49 }
50
51 // create root file
52 TString RootName;
53 if (argc > 2) {
54 RootName = argv[2];
55 printf("writing to %s\n", argv[2]);
56 } else {
57 RootName = "bin2root.root";
58 printf("writing to bin2root.root\n");
59 }
60 TFile *fRoot = new TFile(RootName, "RECREATE");
61
63 // get binary spectra and put into root file
65
66 // read in files from current directory
67 DIR *current_dir;
68 struct dirent *dir;
69 current_dir = opendir(".");
70
71 if (current_dir) {
72 while ((dir = readdir(current_dir)) != NULL) {
73
74 // get name and extension
75 strcpy(str1, dir->d_name);
76 stra = strtok(str1, ".");
77 strb = strtok(NULL, ".");
78
79 // replace special characters in name with '_'
80 for (int i = 0; i < (int)strlen(str1); i++) {
81 if (str1[i] == '-' || str1[i] == ' ') {
82 str2[i] = '_';
83 } else {
84 str2[i] = str1[i];
85 }
86 }
87 str2[(int)strlen(str1)] = '\0';
88
89 // read binary files if name and ext is good, create root spe, and fill 1
90 // at a time (slow but needed for right weight)
91 if (stra != NULL && strb != NULL) {
92
94 // read in *.mat files
95 if (strcmp(strb, "mat") == 0) {
96 if ((fp = fopen(dir->d_name, "r")) == NULL) {
97 fprintf(stderr, "Error, cannot open input file %s\n", dir->d_name);
98 return 1;
99 }
100
101 // get file size --> ydim
102 fseek(fp, 0L, SEEK_END);
103 ydim = (ftell(fp) / sizeof(short int)) / 4096; // still need to check if it's an integer (should be)
104 rewind(fp);
105
106 // make root spe
107 if (ydim == 1) {
108 mat1d = new TH1S(Form("ID%s",str2), Form("ID%s",str2), 4096, 0, 4096);
109 } else if (ydim > 1) {
110 mat2d = new TH2S(Form("ID%s",str2), Form("ID%s",str2), 4096, 0, 4096, ydim, 0, ydim);
111 } else {
112 printf("no data in %s\n", dir->d_name);
113 continue;
114 }
115
116 // read file and fill root spe
117 for (int i = 0; i < ydim; i++) {
118 if (fread(mat, 4096 * sizeof(short int), 1, fp) == 1) {
119 for (int j = 0; j < 4096; j++) {
120 if (mat[j] > 0 ) {
121 std::cout << "filling " << j << " " << i << " " << mat[j] << std::endl;
122 }
123 for (int k = 0; k < mat[j]; k++) { // must do one fill at a time
124 // or it will change "weight"
125 if (ydim == 1)
126 mat1d->Fill(j+0.5);
127 if (ydim > 1)
128 mat2d->Fill(j+0.5, i+0.5);
129 }
130 }
131 } else {
132 printf("Error in reading file\n");
133 exit(0);
134 }
135 }
136 if (ydim == 1)
137 mat1d->Write();
138 if (ydim > 1)
139 mat2d->Write();
140 fclose(fp);
141 printf("read in %s, 4096 row width, short int : y = %d rows\n", dir->d_name, ydim);
142 }
143
145 // read in *.spn files
146 if (strcmp(strb, "spn") == 0) {
147 if ((fp = fopen(dir->d_name, "r")) == NULL) {
148 fprintf(stderr, "Error, cannot open input file %s\n", dir->d_name);
149 return 1;
150 }
151
152 // get file size --> ydim
153 fseek(fp, 0L, SEEK_END);
154 ydim = (ftell(fp) / sizeof(int)) / 4096; // still need to check if it's an integer (should be)
155 rewind(fp);
156
157 // make root spe
158 if (ydim == 1) {
159 spn1d = new TH1I(Form("ID%s",str2), Form("ID%s",str2), 4096, 0, 4096);
160 } else if (ydim > 1) {
161 spn2d = new TH2I(Form("ID%s",str2), Form("ID%s",str2), 4096, 0, 4096, ydim, 0, ydim);
162 } else {
163 printf("no data in %s\n", dir->d_name);
164 continue;
165 }
166
167 // read file and fill root spe
168 for (int i = 0; i < ydim; i++) {
169 if (fread(spn, 4096 * sizeof(int), 1, fp) == 1) {
170 for (int j = 0; j < 4096; j++) {
171 for (int k = 0; k < spn[j]; k++) { // must do one fill at a time
172 // or it will change "weight"
173 if (ydim == 1)
174 spn1d->Fill(j);
175 if (ydim > 1)
176 spn2d->Fill(j, i);
177 }
178 }
179 } else {
180 printf("Error in reading file\n");
181 exit(0);
182 }
183 }
184 if (ydim == 1)
185 spn1d->Write();
186 if (ydim > 1)
187 spn2d->Write();
188 fclose(fp);
189 printf("read in %s, 4096 row width, int : y = %d rows\n", dir->d_name, ydim);
190 }
191
193 // read in *.sec files
194 if (strcmp(strb, "sec") == 0) {
195 if ((fp = fopen(dir->d_name, "r")) == NULL) {
196 fprintf(stderr, "Error, cannot open input file %s\n", dir->d_name);
197 return 1;
198 }
199
200 // get file size --> ydim
201 fseek(fp, 0L, SEEK_END);
202 ydim = (ftell(fp) / sizeof(int)) / 8192; // still need to check if it's an integer (should be)
203 rewind(fp);
204
205 // make root spe
206 if (ydim == 1) {
207 sec1d = new TH1I(Form("ID%s",str2), Form("ID%s",str2), 8192, 0, 8192);
208 } else if (ydim > 1) {
209 sec2d = new TH2I(Form("ID%s",str2), Form("ID%s",str2), 8192, 0, 8192, ydim, 0, ydim);
210 } else {
211 printf("no data in %s\n", dir->d_name);
212 continue;
213 }
214
215 // read file and fill root spe
216 for (int i = 0; i < ydim; i++) {
217 if (fread(sec, 8192 * sizeof(int), 1, fp) == 1) {
218 for (int j = 0; j < 8192; j++) {
219 for (int k = 0; k < sec[j]; k++) { // must do one fill at a time
220 // or it will change "weight"
221 if (ydim == 1)
222 sec1d->Fill(j);
223 if (ydim > 1)
224 sec2d->Fill(j, i);
225 }
226 }
227 } else {
228 printf("Error in reading file\n");
229 exit(0);
230 }
231 }
232 if (ydim == 1)
233 sec1d->Write();
234 if (ydim > 1)
235 sec2d->Write();
236 fclose(fp);
237 printf("read in %s, 8192 row width, int : y = %d rows\n", dir->d_name, ydim);
238 }
239 }
240 }
241 closedir(current_dir);
242 }
243
244 fRoot->Close();
245 return 0;
246}
TSpectrum * fp(TCanvas *canvas, double sigma, Option_t *option, double threshold)
TH1I * sec1d
Definition bin2root.cc:32
TH2S * mat2d
Definition bin2root.cc:31
int main(int argc, char **argv)
Definition bin2root.cc:35
TH2I * spn2d
Definition bin2root.cc:33
TH2I * sec2d
Definition bin2root.cc:33
TH1S * mat1d
Definition bin2root.cc:30
TH1I * spn1d
Definition bin2root.cc:32