GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Utilities.cc
Go to the documentation of this file.
1/* General computational utility functions */
2/* Tim Gray - timothy.gray@anu.edu.au */
3
4#include <chrono>
5#include <iostream>
6
7#include <TMarker.h>
8
9#include "Utilities.hh"
10
11namespace GamR {
12 namespace Utils {
18 double Fac10(int n)
19 {
20 /* implemented from DCOSUBS.f90
21 calculates n!/10^n */
22 double fac = 1.0;
23 if (n == 0) {
24 return 1;
25 } else if (n > 79) {
26 std::cout << "Can't calculate n! for n>79" << std::endl;
27 return -1;
28 } else if (n < 0) {
29 std::cout << "Can't calculate n! for n<0" << std::endl;
30 return -1;
31 }
32 for (int i = 1; i <= n; ++i) {
33 fac = fac * (double)i / 10.0;
34 }
35 return fac;
36 }
37
38 /*
39 void GetClick(TVirtualPad *canvas)
40 {
41 canvas->GetCanvas()->FeedbackMode(kTRUE);
42
43 int event = canvas->GetEvent();
44
45 if (event != 1) {
46 return;
47 }
48 double x = canvas->AbsPixeltoX(canvas->GetEventX());
49 double y = canvas->AbsPixeltoY(canvas->GetEventY());
50
51 double xmin;
52 double ymin;
53 double xmax;
54 double ymax;
55 canvas->GetRangeAxis(xmin, ymin, xmax, ymax);
56
57 if (x <= xmin || x >= xmax || y <= ymin || y >= ymax) {
58 return;
59 } else {
60 TMarker *marker = new TMarker(x, y, 0);
61 marker->Draw();
62 }
63 }
64 */
65
66 void Clicker::GetClick(int event, int x, int y, TObject *selected) {
67 if (waiting==true) { return; }
68 if ( event != 1 ) { return; }
69 TCanvas *c = (TCanvas*)gTQSender;
70 px = x;
71 py = y;
72 cx = c->AbsPixeltoX(x);
73 cy = c->AbsPixeltoY(y);
74
75 double xmin;
76 double ymin;
77 double xmax;
78 double ymax;
79 c->GetRangeAxis(xmin, ymin, xmax, ymax);
80
81 if (cx <= xmin || cx >= xmax || cy <= ymin || cy >= ymax) {
82 return;
83 } else {
84 waiting=true; //guarantee that only one marker is created per "WaitPrimitive" call
85 TMarker *marker = new TMarker(cx, cy, 0);
86 marker->Draw();
87 }
88 }
89
90 void Clicker::GetDrawClick(int event, int x, int y, TObject *selected) {
91 if (waiting==true) { return; }
92 TCanvas *c = (TCanvas*)gTQSender;
93 if ( event == 51 ) {
94 int n = line->GetN();
95 if (n > 0) {
96 line->SetPoint(n-1, c->AbsPixeltoX(x), c->AbsPixeltoY(y));
97 c->Modified(kTRUE);
98 c->Update();
99 }
100 }
101 if ( event != 1 ) { return; }
102 px = x;
103 py = y;
104 cx = c->AbsPixeltoX(x);
105 cy = c->AbsPixeltoY(y);
106 line->AddPoint(cx,cy);
107 c->Modified(kTRUE);
108 c->Update();
109 //this is the next point which will be modified
110 line->AddPoint(cx,cy);
111
112 double xmin;
113 double ymin;
114 double xmax;
115 double ymax;
116 c->GetRangeAxis(xmin, ymin, xmax, ymax);
117
118 if (cx <= xmin || cx >= xmax || cy <= ymin || cy >= ymax) {
119 return;
120 } else {
121 waiting=true; //guarantee that only one marker is created per "WaitPrimitive" call
122 TMarker *marker = new TMarker(cx, cy, 0);
123 marker->Draw();
124 }
125 }
126
127 int Clicker::GetClicks(TVirtualPad *canvas, int n, std::vector<std::string> &messages, int draw/*=0*/, int print/*=0*/) {
128 if (draw==1) {
129 line = new TGraph();
130 line->SetLineColor(kRed);
131 line->SetMarkerStyle(kFullCircle);
132 line->SetMarkerColor(kRed);
133 line->Draw("same LP");
134 }
135 xs.clear();
136 ys.clear();
137 if (draw) {
138 canvas->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "GamR::Utils::Clicker", this, "GetDrawClick(Int_t,Int_t,Int_t,TObject*)");
139 }
140 else {
141 canvas->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "GamR::Utils::Clicker", this, "GetClick(Int_t,Int_t,Int_t,TObject*)");
142 }
143 TObject *obj = NULL;
144 int exit = 0;
145 int i=-1;
146 while (true) {
147 ++i;
148 if (n>0 && i>=n) { break; }
149 if (exit == 1 ) { break; }
150 if (n>0 && i<messages.size()) {
151 std::cout << messages[i] << std::endl;
152 }
153 else if (n<=0 && messages.size()>0 ) {
154 std::cout << messages[0] << std::flush;
155 }
156 while (true) {
157 waiting=false;
158 obj = canvas->WaitPrimitive();
159 if (!obj) { exit = 1; break; }
160 if (strncmp(obj->ClassName(),"TMarker",7)==0) {
161 delete obj;
162 xs.push_back(cx);
163 ys.push_back(cy);
164 if (print) {
165 std::cout << "(" << cx << "," << cy << ")" << std::endl;
166 }
167 break;
168 }
169 }
170 }
171 if (draw) {
172 line->RemovePoint(line->GetN());
173 canvas->Update();
174 canvas->Disconnect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", this, "GetDrawClick(Int_t,Int_t,Int_t,TObject*)");
175 }
176 else {
177 canvas->Disconnect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", this, "GetClick(Int_t,Int_t,Int_t,TObject*)");
178 }
179 return exit;
180 }
181
182 TH1D *GetHist1D(TVirtualPad *canvas) {
183 int nHists = 0;
184 TH1D *hist = NULL;
185 TList *list = canvas->GetListOfPrimitives();
186 for (int i = 0; i < list->GetEntries(); i++) {
187 if (strncmp(list->At(i)->ClassName(), "TH1", 3) == 0) {
188 if (nHists == 0) {
189 hist = (TH1D *)list->At(i);
190 }
191 nHists = nHists + 1;
192 }
193 }
194 if (!hist) {
195 //std::cout << "no histogram found in Canvas!" << std::endl;
196 return NULL;
197 }
198 // if (nHists > 1) {
199 // std::cout << "Warning: Canvas has more than one histogram!" << std::endl;
200 // }
201 return hist;
202 }
203
204 std::vector<TH1D*> GetHists1D(TVirtualPad *canvas) {
205 int nHists = 0;
206 std::vector<TH1D*> hists;
207 TList *list = canvas->GetListOfPrimitives();
208 for (int i = 0; i < list->GetEntries(); i++) {
209 if (strncmp(list->At(i)->ClassName(), "TH1", 3) == 0) {
210 hists.push_back((TH1D *)list->At(i));
211 nHists = nHists + 1;
212 }
213 }
214 if (hists.size() == 0) {
215 //std::cout << "no histogram found in Canvas!" << std::endl;
216 return hists;
217 }
218 return hists;
219 }
220
221 TH2D *GetHist2D(TVirtualPad *canvas) {
222 int nHists = 0;
223 TH2D *hist = NULL;
224 TList *list = canvas->GetListOfPrimitives();
225 for (int i = 0; i < list->GetEntries(); i++) {
226 if (strncmp(list->At(i)->ClassName(), "TH2", 3) == 0) {
227 hist = (TH2D *)list->At(i);
228 nHists = nHists + 1;
229 }
230 }
231 if (!hist) {
232 //std::cout << "no histogram found in Canvas!" << std::endl;
233 return NULL;
234 }
235 if (nHists > 1) {
236 //std::cout << "Warning: Canvas has more than one histogram!" << std::endl;
237 }
238 return hist;
239 }
240
241 int wrresult(char *out, float value, float err, int minlen)
242 {
243 int n;
244 char *c;
245
246 /* append nicely-formatted " value(error)" to string out
247 minlen = requested minimum length of out at completion
248 returns new length of out string */
249 /* from radware */
250
251 c = out + strlen(out);
252 if (err < 0.00005f) {
253 sprintf(c, " %.4f", value);
254 c = out + strlen(out);
255 while (*(c - 1) == '0' && *(c - 2) == '0')
256 c--;
257 strcpy(c, "(0)");
258 } else if (err < 0.0025f) {
259 sprintf(c, " %.4f(%.0f)", value, err * 10000.0f);
260 } else if (err < 0.025f) {
261 sprintf(c, " %.3f(%.0f)", value, err * 1000.0f);
262 } else if (err < 0.25f) {
263 sprintf(c, " %.2f(%.0f)", value, err * 100.0f);
264 } else if (err < 2.5f) {
265 sprintf(c, " %.1f(%.0f)", value, err * 10.0f);
266 } else {
267 sprintf(c, " %.0f(%.0f)", value, err);
268 }
269 for (n = strlen(out); n < minlen; n++) {
270 strcat(out, " ");
271 }
272 return n;
273 } /* wrresult */
274
275 std::string wrresult(double value, double error) {
276 char out_char[2048];
277 *out_char = '\0';
278 wrresult(out_char, value, error, 0);
279 std::string out(out_char);
280 return out;
281 }
282
283 // Routine to convert a C++ string into a fortran character array. Don't forget to delete memory after use.
284 char *c_to_f_str(std::string strin)
285 {
286 char *inp = new char[80];
287 for(int x=0;x<80;x++) inp[x] = ' ';
288 if(strin.size()<80)
289 {
290 for(int x=0;x<strin.size();x++) inp[x] = strin[x];
291 }
292 else
293 {
294 for(int x=0;x<80;x++) inp[x] = strin[x];
295 }
296
297 return inp;
298 }
299
300 //This computes the Simpson's-rule integral cross an array of values
301 double Simps(double *y, int n, double dx)
302 {
303 double yint = 0;
304 yint = y[0] + y[n];
305 for(int i=1; i<n; i++) yint += y[i] * double(i%2 + 1) * 2.;
306 yint *= dx/3.;
307
308 return yint;
309 }
310 std::complex<double> **Simps(std::complex<double> ***rho, int n, double dx)
311 {
312 std::complex<double> **rhoint = new std::complex<double>*[3];
313 for(int k=0; k<3; k++) rhoint[k] = new std::complex<double>[5];
314 for(int k=0; k<3; k++)
315 {
316 for(int q=0; q<=k*2; q++) {
317 rhoint[k][q] = std::complex<double>(0,0);
318 rhoint[k][q] = rho[0][k][q] + rho[n][k][q];
319 for(int i=1; i<n; i++) {
320 rho[i][k][q] *= double(i%2 + 1) * 2.;
321 rhoint[k][q] += rho[i][k][q];
322 }
323 rhoint[k][q] *= dx/3.;
324 }
325 }
326
327 return rhoint;
328 }
329
330 std::string getline(std::ifstream &f)
331 {
332 if(f.fail()) return "";
333 else {
334 std::string ans;
335 f >> ans;
336 return ans;
337 }
338 }
339
340 int catcherr(std::string inp, double &val, bool require_positive /* = true */) {
341 try { val = std::stod(inp); }
342 catch(const std::invalid_argument& ia) {
343 std::cout << "\tInvalid entry. Must be numerical." << std::endl;
344 return 1;
345 }
346 if(val<0 && require_positive) {
347 std::cout << "\tInvalid entry. Must be >0." << std::endl;
348 return 2;
349 }
350 return 0;
351 }
352
353 int catcherr(std::string inp, float &val, bool require_positive /* = true */) {
354 try { val = std::stof(inp); }
355 catch(const std::invalid_argument& ia) {
356 std::cout << "\tInvalid entry. Must be numerical." << std::endl;
357 return 1;
358 }
359 if(val<0 && require_positive) {
360 std::cout << "\tInvalid entry. Must be >0." << std::endl;
361 return 2;
362 }
363 return 0;
364 }
365
366 int catcherr(std::string inp, int &val, bool require_positive /* = true */) {
367 try { val = std::stoi(inp); }
368 catch(const std::invalid_argument& ia) {
369 std::cout << "\tInvalid entry. Must be numerical." << std::endl;
370 return 1;
371 }
372 if(val<0 && require_positive) {
373 std::cout << "\tInvalid entry. Must be >0." << std::endl;
374 return 2;
375 }
376 return 0;
377 }
378
379 int GetInput(std::string prompt, double &val, bool require_positive /* = true */) {
380 std::string inp;
381 while (true) {
382 std::cout << prompt << " [" << val << "]: ";
383 std::getline(std::cin, inp);
384 if(inp.empty()) { break; }
385 if(!catcherr(inp, val, require_positive)) { break; }
386 }
387 return 0;
388 }
389
390 int GetInput(std::string prompt, float &val, bool require_positive /* = true */) {
391 std::string inp;
392 while (true) {
393 std::cout << prompt << " [" << val << "]: ";
394 std::getline(std::cin, inp);
395 if(inp.empty()) { break; }
396 if(!catcherr(inp, val, require_positive)) { break; }
397 }
398 return 0;
399 }
400 int GetInput(std::string prompt, int &val, bool require_positive /* = true */) {
401 std::string inp;
402 while (true) {
403 std::cout << prompt << " [" << val << "]: ";
404 std::getline(std::cin, inp);
405 if(inp.empty()) { break; }
406 if(!catcherr(inp, val, require_positive)) { break; }
407 }
408 return 0;
409 }
410
411 int GetNPads(TVirtualPad *pad) {
412 //count the number of pads in pad
413 if (!pad) return 0;
414 Int_t npads = 0;
415 TObject *obj;
416 TIter next(pad->GetListOfPrimitives());
417 while ((obj = next())) {
418 if (obj->InheritsFrom(TVirtualPad::Class())) npads++;
419 }
420
421 return npads;
422 }
423
424 } // namespace Utils
425} // namespace GamR
std::vector< double > xs
Definition Utilities.hh:38
std::vector< double > ys
Definition Utilities.hh:39
void GetClick(Int_t, Int_t, Int_t, TObject *)
Definition Utilities.cc:66
void GetDrawClick(Int_t, Int_t, Int_t, TObject *)
Definition Utilities.cc:90
int GetClicks(TVirtualPad *canvas, int n, std::vector< std::string > &messages, int draw=0, int print=0)
Definition Utilities.cc:127
std::string getline(std::ifstream &f)
Definition Utilities.cc:330
int GetNPads(TVirtualPad *pad)
Definition Utilities.cc:411
TH1D * GetHist1D(TVirtualPad *canvas)
Definition Utilities.cc:182
char * c_to_f_str(std::string strin)
Definition Utilities.cc:284
double Fac10(int n)
Definition Utilities.cc:18
std::vector< TH1D * > GetHists1D(TVirtualPad *canvas)
Definition Utilities.cc:204
int wrresult(char *out, float value, float err, int minlen)
Definition Utilities.cc:241
double Simps(double *y, int n, double dx)
Definition Utilities.cc:301
int catcherr(std::string inp, double &val, bool require_positive)
Definition Utilities.cc:340
TH2D * GetHist2D(TVirtualPad *canvas)
Definition Utilities.cc:221
int GetInput(std::string prompt, double &val, bool require_positive)
Definition Utilities.cc:379
Definition Gain.cc:19