-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cc
288 lines (236 loc) · 7.95 KB
/
common.cc
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <vector>
#include <string>
#include <iostream>
#include <sys/stat.h>
#include "common.h"
#include "TMath.h"
#include "TROOT.h"
#include "TStyle.h"
bool CheckFile(string fileName)
{
char fileNameCopy[100];
struct stat fileStat;
strcpy(fileNameCopy,fileName.c_str());
int status = lstat(fileNameCopy,&fileStat);
if( status == -1 )
{
cout << "-----------------------------------------------------" << endl;
cout << "File " << fileName << " does not exist." << endl;
cout << "-----------------------------------------------------" << endl;
return false;
}
cout << "-----------------------------------------------------" << endl;
cout << "Checking " << fileName << " (" << fileStat.st_size << " bytes)." << endl;
if( fileStat.st_size == 0 )
{
cout << "file size = 0 !!" << endl << endl;
return false;
}
cout << "-----------------------------------------------------" << endl;
return true;
}
const int kNFields = 12;
const string kEventFields[kNFields] =
{
"1 Event Id",
"2 Theta",
"3 Error on Theta",
"4 Phi",
"5 Error on Phi",
"6 Galactic longitude",
"7 Galactic latitude",
"8 Right Ascension",
"9 Declination",
"10 UTC time",
"11 TCore",
"12 Energy"
};
void DumpFields()
{
cout << "------------------------------------------------------------------------------" << endl;
cout << "Data fields in event file" << endl;
cout << "------------------------------------------------------------------------------" << endl;
int count = 0;
for(int i = 0; i < kNFields; i++)
{
printf("%-30s,", kEventFields[i].c_str());
count++;
if( count == 2 ) {cout << endl; count = 0;}
}
cout << endl << "-------------------------------------------------------------------------------" << endl;
}
// Code from Xavier Bertou
TProgressBar::TProgressBar() {Zero();}
void TProgressBar::Zero()
{
fCurrentPercentage = 0;
fReached = 0;
fBegin = 0;
fEnd = 0;
}
void TProgressBar::InitPercent()
{
fprintf(stderr,"[ ]\r[");
fCurrentPercentage = 0;
fReached = 0;
}
void TProgressBar::EndPercent() const
{
fprintf(stderr,"\r[##################################################]\n");
}
void TProgressBar::PrintPercent(unsigned int value)
{
fReached=value;
unsigned int newper=fReached*100/(fEnd-fBegin);
// step of 2%
while( newper>fCurrentPercentage+2 )
{
fprintf(stderr,"#");
fflush(stderr);
fCurrentPercentage+=2;
}
}
template<typename T> vector< vector<unsigned int> > GetContiguousPoints(unsigned int size, const T *xx, T ignore)
{
unsigned int xi(0), xstart;
vector< vector<unsigned int> > good;
// The idea is to find in xx contiguous portions of data.These portions are separated by the value ignore.
// For instance, for : 0 1 2 3 -1 4 5 6 -1 7 8 9
// the function return 3 vectors of indices corresponding to the elements 0 1 2 3 then 4 5 6 and finally 7 8 9.
// This function is used to correctly draw the isolongitude and isolatitude lines. Indeed, if you try to connect
// points lying inside your map with points lying outside then you will obtain large staright lines. This is what
// you will if you try to connect the -90 latitude with the 90 latitude
// find first == ignore
while( xx[xi] != ignore && xi < size ) xi++;
if( xi == size )
{
// only good data
vector<unsigned int> tmp(size);
for( unsigned int i = 0; i < size; i++ ) tmp[i] = i;
// then return all indices as they are in the original vector
good.push_back(tmp);
return good;
}
xstart = xi;
xi++; // skip detected ignore point
while( xi != xstart ) // have to scan the complete vector until we are back to first ignore
{
// find first != ignore
while( xx[xi] == ignore && xi != xstart ) xi=(xi+1+size)%size;
// find next == ignore
vector<unsigned int> contiguous;
while( xx[xi] != ignore && xi != xstart )
{
contiguous.push_back(xi);
xi = (xi+1+size)%size;
}
if( contiguous.size() ) good.push_back(contiguous);
}
return good;
}
void GetHisto(TH1F* Histo, const vector<double>& variable)
{
// Fill with input values
unsigned int sz = variable.size();
if( !sz )
{
cout << "GetHisto: No data to make histogram. Returning." << endl;
return;
}
for(unsigned int i = 0; i < sz; i++) Histo->Fill(variable[i]);
Histo->Sumw2(); Histo->Scale(1./Histo->Integral());
}
void DrawHisto(TCanvas* cHisto, TH1F* Histo, string Xaxis, string Yaxis, string save)
{
// X axis options
Histo->SetXTitle(Xaxis.c_str());
Histo->GetXaxis()->SetTitleFont(132);
Histo->GetXaxis()->SetLabelSize(0.03);
Histo->GetXaxis()->SetLabelFont(132);
Histo->GetXaxis()->SetTitleSize(0.035);
Histo->GetXaxis()->SetTitleOffset(1.1);
// Y axis options
Histo->SetYTitle(Yaxis.c_str());
Histo->GetYaxis()->SetTitleFont(132);
Histo->GetYaxis()->SetLabelSize(0.03);
Histo->GetYaxis()->SetLabelFont(132);
Histo->GetYaxis()->SetTitleSize(0.035);
Histo->GetYaxis()->SetTitleOffset(1.4);
// General
Histo->SetStats(0);
Histo->SetMarkerStyle(kFullCircle);
Histo->SetMarkerSize(1.);
if(save != "")
{
Histo->Draw("e1p");
cHisto->Update();
cHisto->SaveAs(save.c_str());
}
}
void PlotXY(TCanvas* cPlot, TGraphErrors* Plot, double xMin, double xMax, string name, string Xaxis, string Yaxis)
{
Plot->SetTitle(name.c_str());
// X axis options
Plot->GetHistogram()->SetXTitle(Xaxis.c_str());
Plot->GetHistogram()->SetAxisRange(xMin,xMax);
Plot->GetXaxis()->SetTitleFont(132);
Plot->GetXaxis()->SetLabelSize(0.03);
Plot->GetXaxis()->SetLabelFont(132);
Plot->GetXaxis()->SetTitleSize(0.035);
Plot->GetXaxis()->SetTitleOffset(1.1);
// Y axis options
Plot->GetHistogram()->SetYTitle(Yaxis.c_str());
Plot->GetYaxis()->SetTitleFont(132);
Plot->GetYaxis()->SetLabelSize(0.03);
Plot->GetYaxis()->SetLabelFont(132);
Plot->GetYaxis()->SetTitleSize(0.035);
Plot->GetYaxis()->SetTitleOffset(1.4);
// General
Plot->SetMarkerStyle(kFullCircle);
Plot->SetMarkerColor(kBlack);
Plot->SetMarkerSize(1.);
}
void PaletteOrange(int nContours)
{
const int nStops = 3;
double stops[nStops] = { 0.00, 0.5, 1.00 };
double r[nStops] = { 1.00, 1.00, 0.00 };
double g[nStops] = { 1.00, 0.50, 0.00 };
double b[nStops] = { 1.00, 0.00, 0.00 };
TColor::CreateGradientColorTable(nStops, stops, r, g, b, nContours);
gStyle->SetNumberContours(nContours);
}
void PaletteBlue(int nContours)
{
const int nStops = 3;
double stops[nStops] = { 0.00, 0.5, 1.00 };
double r[nStops] = { 1.00, 0.25, 0.00 };
double g[nStops] = { 1.00, 0.25, 0.00 };
double b[nStops] = { 1.00, 1.00, 0.00 };
TColor::CreateGradientColorTable(nStops, stops, r, g, b, nContours);
gStyle->SetNumberContours(nContours);
}
void PaletteRGB(int nContours)
{
const int nStops = 5;
double stops[nStops] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
double r[nStops] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
double g[nStops] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
double b[nStops] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
TColor::CreateGradientColorTable(nStops, stops, r, g, b, nContours);
gStyle->SetNumberContours(nContours);
}
void PaletteBlueAndRed(int nContours)
{
const int nStops = 3;
double stops[nStops] = { 0.00, 0.5, 1.00 };
double r[nStops] = { 0.00, 1.00, 1.00 };
double g[nStops] = { 0.00, 1.00, 0.00 };
double b[nStops] = { 1.00, 1.00, 0.00 };
TColor::CreateGradientColorTable(nStops, stops, r, g, b, nContours);
gStyle->SetNumberContours(nContours);
}
//! Instanciation of templates
template vector< vector<unsigned int> > GetContiguousPoints(unsigned int size, const double * xx, double ignore_value);
template vector< vector<unsigned int> > GetContiguousPoints(unsigned int size, const float * xx, float ignore_value);
template vector< vector<unsigned int> > GetContiguousPoints(unsigned int size, const int * xx, int ignore_value);