-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat2root.cpp
174 lines (119 loc) · 3.85 KB
/
mat2root.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// mat2root.cpp
//
//
// Created by Liam Gaffney on 21/12/2015.
//
//
#include "TFile.h"
#include "TMath.h"
#include "TH2.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
void mat2root( string matfile, short size, float binx, float biny ) {
// test
cout << matfile << " " << size << " " << binx << " " << biny << endl;
// Somewhere to read our matrix in to!
string filename = matfile.substr( 0, matfile.find_last_of( "." ) ) + ".root";
TFile *rootfile = new TFile( filename.c_str(), "RECREATE" );
TH2D *rootmat = new TH2D( "mat", "mat", size, -0.5*binx, (size-0.5)*binx,
size, -0.5*biny, (size-0.5)*biny );
// Read the matrix file
ifstream infile;
infile.open( matfile.c_str(), ios::binary );
// Test that it's open!
if( !infile.is_open() ) {
cout << matfile.c_str() << " not opened...\n";
return;
}
// Get file length
infile.seekg ( 0, ios::end );
const int length = infile.tellg();
infile.seekg ( 0, ios::beg );
// Read file as a block
char *buf = new char[2*size];
short dat[size];
// Do some processing
short counts;
int evt = 0;
for( int i = 0; i < size; i++ ) {
infile.read( buf, 2*size ); // 2 bytes per element
memcpy( dat, buf, 2*size ); // 2 bytes per element
for( int j = 0; j < size; j++ ) {
counts = (short)dat[ j ];
rootmat->SetBinContent( i+1, j+1, (float)counts );
evt++;
if( evt > length/2 ) {
cout << "Reading too many events... Check size\n";
return;
}
}
}
cout << "Successfully read and processed " << matfile.c_str() << endl;
rootmat->Write();
rootfile->Close();
return;
}
void print_usage( string progname ){
cout << "-----------------------------------------------------------\n";
cout << " mat2root : a program to convert .mat file \n";
cout << " to TH2 root histrogram \n\n";
cout << " usage : " << progname << " <file> <size> <binx> <biny>\n";
cout << " where <file> is the .mat file, <size> is the number of\n";
cout << " channels and <binx/y> are the bin widths in x/y direction\n";
cout << "-----------------------------------------------------------\n";
return;
}
int main( int argc, char *argv[] ) {
string file;
stringstream ss;
int size;
float binx, biny;
if ( argc == 2 ) {
file = argv[1];
size = 4096;
binx = 1;
biny = 1;
mat2root( file, size, binx, biny );
}
else if ( argc == 3 ) {
file = argv[1];
ss.clear();
ss.str( argv[2] );
ss >> size;
binx = 1;
biny = 1;
mat2root( file, size, binx, biny );
}
else if ( argc == 4 ) {
file = argv[1];
ss.clear();
ss.str( argv[2] );
ss >> size;
ss.clear();
ss.str( argv[3] );
ss >> binx;
mat2root( file, size, binx, biny );
}
else if ( argc == 5 ) {
file = argv[1];
ss.clear();
ss.str( argv[2] );
ss >> size;
ss.clear();
ss.str( argv[3] );
ss >> binx;
ss.clear();
ss.str( argv[4] );
ss >> biny;
mat2root( file, size, binx, biny );
}
else {
print_usage( argv[0] );
return 0;
}
}