1 #ifndef __HistogramTools__
2 #define __HistogramTools__
7 #include <JANA/JApplication.h>
12 #include <TProfile2D.h>
13 #include <TDirectory.h>
19 #define ansi_escape ((char)0x1b)
20 #define ansi_bold ansi_escape<<"[1m"
21 #define ansi_italic ansi_escape<<"[3m"
22 #define ansi_underline ansi_escape<<"[4m"
23 #define ansi_blink ansi_escape<<"[5m"
24 #define ansi_rapid_blink ansi_escape<<"[6m"
25 #define ansi_reverse ansi_escape<<"[7m"
26 #define ansi_black ansi_escape<<"[30m"
27 #define ansi_red ansi_escape<<"[31m"
28 #define ansi_green ansi_escape<<"[32m"
29 #define ansi_blue ansi_escape<<"[34m"
30 #define ansi_normal ansi_escape<<"[0m"
31 #define ansi_up(A) ansi_escape<<"["<<(A)<<"A"
32 #define ansi_down(A) ansi_escape<<"["<<(A)<<"B"
33 #define ansi_forward(A) ansi_escape<<"["<<(A)<<"C"
34 #define ansi_back(A) ansi_escape<<"["<<(A)<<"D"
38 static vector <TDirectory *> allDirectories;
39 return allDirectories;
42 map<TString, pair<TH1I*, pthread_rwlock_t*> >&
Get1DMap(
void){
43 static map<TString, pair<TH1I*, pthread_rwlock_t*> > TH1IMap;
48 static map<TString, pair<TH1D*, pthread_rwlock_t*> > TH1DMap;
52 map<TString, pair<TH2I*, pthread_rwlock_t*> >&
Get2DMap(
void){
53 static map<TString, pair<TH2I*, pthread_rwlock_t*> > TH2IMap;
58 static map<TString, pair<TH2D*, pthread_rwlock_t*> > TH2DMap;
62 map<TString, pair<TH3I*, pthread_rwlock_t*> >&
Get3DMap(
void){
63 static map<TString, pair<TH3I*, pthread_rwlock_t*> > TH3IMap;
68 static map<TString, pair<TProfile*, pthread_rwlock_t*> > TProfile1DMap;
73 static map<TString, pair<TProfile2D*, pthread_rwlock_t*> > TProfile2DMap;
78 pthread_rwlock_t *thisLock =
new pthread_rwlock_t();
79 pthread_rwlock_init(thisLock, NULL);
83 void Fill1DHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double value,
const char * title ,
int nBins,
double xmin,
double xmax,
bool print =
false){
86 pair<TH1I*, pthread_rwlock_t*> histogramPair;
88 char fullNameChar[500];
89 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
90 TString fullName = TString(fullNameChar);
93 pthread_rwlock_rdlock(mapLock);
94 histogramPair =
Get1DMap().at(fullName);
95 pthread_rwlock_unlock(mapLock);
97 catch (
const std::out_of_range& oor) {
99 pthread_rwlock_unlock(mapLock);
100 pthread_rwlock_wrlock(mapLock);
104 histogramPair =
Get1DMap().at(fullName);
106 catch(
const std::out_of_range& oor) {
108 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 1D Histogram " << fullName <<
ansi_normal << endl;
110 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
111 pthread_rwlock_init(histogramLock, NULL);
116 japp->RootWriteLock();
117 TDirectory *homedir = gDirectory;
119 temp = gDirectory->mkdir(plugin);
121 gDirectory->cd(plugin);
123 gDirectory->cd(directoryName);
124 histogram =
new TH1I( name, title, nBins, xmin, xmax);
125 histogram->Fill(value);
129 Get1DMap()[fullName] = make_pair(histogram, histogramLock);
130 pthread_rwlock_unlock(mapLock);
135 pthread_rwlock_unlock(mapLock);
138 histogram = histogramPair.first;
139 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
140 pthread_rwlock_wrlock(histogramLockPtr);
141 histogram->Fill(value);
142 pthread_rwlock_unlock(histogramLockPtr);
148 void Fill1DHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double value,
const char * title ,
int nBins,
double *xbins) {
151 pair<TH1I*, pthread_rwlock_t*> histogramPair;
153 char fullNameChar[500];
154 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
155 TString fullName = TString(fullNameChar);
158 pthread_rwlock_rdlock(mapLock);
159 histogramPair =
Get1DMap().at(fullName);
160 pthread_rwlock_unlock(mapLock);
162 catch (
const std::out_of_range& oor) {
164 pthread_rwlock_unlock(mapLock);
165 pthread_rwlock_wrlock(mapLock);
169 histogramPair =
Get1DMap().at(fullName);
171 catch(
const std::out_of_range& oor) {
175 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
176 pthread_rwlock_init(histogramLock, NULL);
181 japp->RootWriteLock();
182 TDirectory *homedir = gDirectory;
184 temp = gDirectory->mkdir(plugin);
186 gDirectory->cd(plugin);
188 gDirectory->cd(directoryName);
189 histogram =
new TH1I( name, title, nBins, xbins);
190 histogram->Fill(value);
194 Get1DMap()[fullName] = make_pair(histogram, histogramLock);
195 pthread_rwlock_unlock(mapLock);
200 pthread_rwlock_unlock(mapLock);
203 histogram = histogramPair.first;
204 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
205 pthread_rwlock_wrlock(histogramLockPtr);
206 histogram->Fill(value);
207 pthread_rwlock_unlock(histogramLockPtr);
214 void Fill1DWeightedHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double value,
const double weight,
const char * title ,
int nBins,
double xmin,
double xmax,
bool print =
false){
217 pair<TH1D*, pthread_rwlock_t*> histogramPair;
219 char fullNameChar[500];
220 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
221 TString fullName = TString(fullNameChar);
224 pthread_rwlock_rdlock(mapLock);
226 pthread_rwlock_unlock(mapLock);
228 catch (
const std::out_of_range& oor) {
230 pthread_rwlock_unlock(mapLock);
231 pthread_rwlock_wrlock(mapLock);
237 catch(
const std::out_of_range& oor) {
239 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 1D Weighted Histogram " << fullName <<
ansi_normal << endl;
241 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
242 pthread_rwlock_init(histogramLock, NULL);
247 japp->RootWriteLock();
248 TDirectory *homedir = gDirectory;
250 temp = gDirectory->mkdir(plugin);
252 gDirectory->cd(plugin);
254 gDirectory->cd(directoryName);
255 histogram =
new TH1D( name, title, nBins, xmin, xmax);
256 histogram->Fill(value,weight);
261 pthread_rwlock_unlock(mapLock);
266 pthread_rwlock_unlock(mapLock);
269 histogram = histogramPair.first;
270 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
271 pthread_rwlock_wrlock(histogramLockPtr);
272 histogram->Fill(value,weight);
273 pthread_rwlock_unlock(histogramLockPtr);
278 void Fill2DHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double valueX ,
const double valueY ,
const char * title ,
int nBinsX,
double xmin,
double xmax,
int nBinsY,
double ymin,
double ymax,
bool print =
false){
282 pair<TH2I*, pthread_rwlock_t*> histogramPair;
284 char fullNameChar[500];
285 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
286 TString fullName = TString(fullNameChar);
289 pthread_rwlock_rdlock(mapLock);
290 histogramPair =
Get2DMap().at(fullName);
291 pthread_rwlock_unlock(mapLock);
293 catch (
const std::out_of_range& oor) {
295 pthread_rwlock_unlock(mapLock);
296 pthread_rwlock_wrlock(mapLock);
300 histogramPair =
Get2DMap().at(fullName);
302 catch (
const std::out_of_range& oor) {
303 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 2D Histogram " << name <<
ansi_normal << endl;
305 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
306 pthread_rwlock_init(histogramLock, NULL);
312 japp->RootWriteLock();
313 TDirectory *homedir = gDirectory;
315 temp = gDirectory->mkdir(plugin);
317 gDirectory->cd(plugin);
319 gDirectory->cd(directoryName);
320 histogram =
new TH2I( name, title, nBinsX, xmin, xmax, nBinsY, ymin, ymax);
321 histogram->Fill(valueX, valueY);
325 Get2DMap()[fullName] = make_pair(histogram,histogramLock);
326 pthread_rwlock_unlock(mapLock);
331 pthread_rwlock_unlock(mapLock);
334 histogram = histogramPair.first;
335 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
336 pthread_rwlock_wrlock(histogramLockPtr);
337 histogram->Fill(valueX, valueY);
338 pthread_rwlock_unlock(histogramLockPtr);
344 void Fill2DHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double valueX ,
const double valueY ,
const char * title ,
int nBinsX,
double *xbins,
int nBinsY,
double *ybins,
bool print =
false){
348 pair<TH2I*, pthread_rwlock_t*> histogramPair;
350 char fullNameChar[500];
351 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
352 TString fullName = TString(fullNameChar);
355 pthread_rwlock_rdlock(mapLock);
356 histogramPair =
Get2DMap().at(fullName);
357 pthread_rwlock_unlock(mapLock);
359 catch (
const std::out_of_range& oor) {
361 pthread_rwlock_unlock(mapLock);
362 pthread_rwlock_wrlock(mapLock);
366 histogramPair =
Get2DMap().at(fullName);
368 catch (
const std::out_of_range& oor) {
369 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 2D Histogram " << name <<
ansi_normal << endl;
371 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
372 pthread_rwlock_init(histogramLock, NULL);
378 japp->RootWriteLock();
379 TDirectory *homedir = gDirectory;
381 temp = gDirectory->mkdir(plugin);
383 gDirectory->cd(plugin);
385 gDirectory->cd(directoryName);
386 histogram =
new TH2I( name, title, nBinsX, xbins, nBinsY, ybins);
387 histogram->Fill(valueX, valueY);
391 Get2DMap()[fullName] = make_pair(histogram,histogramLock);
392 pthread_rwlock_unlock(mapLock);
397 pthread_rwlock_unlock(mapLock);
400 histogram = histogramPair.first;
401 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
402 pthread_rwlock_wrlock(histogramLockPtr);
403 histogram->Fill(valueX, valueY);
404 pthread_rwlock_unlock(histogramLockPtr);
409 void Fill2DWeightedHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double valueX ,
const double valueY ,
const double weight ,
const char * title ,
int nBinsX,
double xmin,
double xmax,
int nBinsY,
double ymin,
double ymax,
bool print =
false){
413 pair<TH2D*, pthread_rwlock_t*> histogramPair;
415 char fullNameChar[500];
416 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
417 TString fullName = TString(fullNameChar);
420 pthread_rwlock_rdlock(mapLock);
422 pthread_rwlock_unlock(mapLock);
424 catch (
const std::out_of_range& oor) {
426 pthread_rwlock_unlock(mapLock);
427 pthread_rwlock_wrlock(mapLock);
433 catch (
const std::out_of_range& oor) {
434 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 2D Histogram " << name <<
ansi_normal << endl;
436 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
437 pthread_rwlock_init(histogramLock, NULL);
443 japp->RootWriteLock();
444 TDirectory *homedir = gDirectory;
446 temp = gDirectory->mkdir(plugin);
448 gDirectory->cd(plugin);
450 gDirectory->cd(directoryName);
451 histogram =
new TH2D( name, title, nBinsX, xmin, xmax, nBinsY, ymin, ymax);
452 histogram->Fill(valueX, valueY, weight);
457 pthread_rwlock_unlock(mapLock);
462 pthread_rwlock_unlock(mapLock);
465 histogram = histogramPair.first;
466 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
467 pthread_rwlock_wrlock(histogramLockPtr);
468 histogram->Fill(valueX, valueY, weight);
469 pthread_rwlock_unlock(histogramLockPtr);
474 void Fill3DHistogram (
const char * plugin,
const char * directoryName,
const char * name,
const double valueX ,
const double valueY ,
const double valueZ ,
const char * title ,
int nBinsX,
double xmin,
double xmax,
int nBinsY,
double ymin,
double ymax,
int nBinsZ,
double zmin,
double zmax,
bool print =
false){
478 pair<TH3I*, pthread_rwlock_t*> histogramPair;
480 char fullNameChar[500];
481 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
482 TString fullName = TString(fullNameChar);
485 pthread_rwlock_rdlock(mapLock);
486 histogramPair =
Get3DMap().at(fullName);
487 pthread_rwlock_unlock(mapLock);
489 catch (
const std::out_of_range& oor) {
491 pthread_rwlock_unlock(mapLock);
492 pthread_rwlock_wrlock(mapLock);
496 histogramPair =
Get3DMap().at(fullName);
498 catch (
const std::out_of_range& oor) {
499 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 3D Histogram " << name <<
ansi_normal << endl;
501 pthread_rwlock_t *histogramLock =
new pthread_rwlock_t();
502 pthread_rwlock_init(histogramLock, NULL);
508 japp->RootWriteLock();
509 TDirectory *homedir = gDirectory;
511 temp = gDirectory->mkdir(plugin);
513 gDirectory->cd(plugin);
515 gDirectory->cd(directoryName);
516 histogram =
new TH3I( name, title, nBinsX, xmin, xmax, nBinsY, ymin, ymax, nBinsZ, zmin, zmax);
517 histogram->Fill(valueX, valueY, valueZ);
521 Get3DMap()[fullName] = make_pair(histogram,histogramLock);
522 pthread_rwlock_unlock(mapLock);
527 pthread_rwlock_unlock(mapLock);
530 histogram = histogramPair.first;
531 pthread_rwlock_t *histogramLockPtr = histogramPair.second;
532 pthread_rwlock_wrlock(histogramLockPtr);
533 histogram->Fill(valueX, valueY, valueZ);
534 pthread_rwlock_unlock(histogramLockPtr);
541 void Fill1DProfile (
const char * plugin,
const char * directoryName,
const char * name,
const double valueX ,
const double valueY ,
const char * title ,
int nBinsX,
double xmin,
double xmax,
bool print =
false){
545 pair<TProfile*, pthread_rwlock_t*> profilePair;
547 char fullNameChar[500];
548 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
549 TString fullName = TString(fullNameChar);
551 pthread_rwlock_rdlock(mapLock);
553 pthread_rwlock_unlock(mapLock);
555 catch (
const std::out_of_range& oor) {
557 pthread_rwlock_unlock(mapLock);
558 pthread_rwlock_wrlock(mapLock);
564 catch (
const std::out_of_range& oor) {
565 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 1D Profile " << name <<
ansi_normal << endl;
567 pthread_rwlock_t *profileLock =
new pthread_rwlock_t();
568 pthread_rwlock_init(profileLock, NULL);
574 japp->RootWriteLock();
575 TDirectory *homedir = gDirectory;
577 temp = gDirectory->mkdir(plugin);
579 gDirectory->cd(plugin);
581 gDirectory->cd(directoryName);
582 profile =
new TProfile( name, title, nBinsX, xmin, xmax);
583 profile->Fill(valueX, valueY);
588 pthread_rwlock_unlock(mapLock);
593 pthread_rwlock_unlock(mapLock);
596 profile = profilePair.first;
597 pthread_rwlock_t *profileLockPtr = profilePair.second;
598 pthread_rwlock_wrlock(profileLockPtr);
599 profile->Fill(valueX, valueY);
600 pthread_rwlock_unlock(profileLockPtr);
605 void Fill2DProfile (
const char * plugin,
const char * directoryName,
const char * name,
const double valueX ,
const double valueY ,
const double valueZ,
const char * title ,
int nBinsX,
double xmin,
double xmax,
int nBinsY,
double ymin,
double ymax,
bool print =
false){
607 TProfile2D * profile;
608 pair<TProfile2D*, pthread_rwlock_t*> profilePair;
610 char fullNameChar[500];
611 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
612 TString fullName = TString(fullNameChar);
614 pthread_rwlock_rdlock(mapLock);
616 pthread_rwlock_unlock(mapLock);
618 catch (
const std::out_of_range& oor) {
620 pthread_rwlock_unlock(mapLock);
621 pthread_rwlock_wrlock(mapLock);
627 catch (
const std::out_of_range& oor) {
628 if (print) std::cerr <<
ansi_green << plugin <<
" ===> Making New 2D Profile " << name <<
ansi_normal << endl;
630 pthread_rwlock_t *profileLock =
new pthread_rwlock_t();
631 pthread_rwlock_init(profileLock, NULL);
637 japp->RootWriteLock();
638 TDirectory *homedir = gDirectory;
640 temp = gDirectory->mkdir(plugin);
642 gDirectory->cd(plugin);
644 gDirectory->cd(directoryName);
645 profile =
new TProfile2D( name, title, nBinsX, xmin, xmax, nBinsY, ymin, ymax);
646 profile->Fill(valueX, valueY, valueZ);
651 pthread_rwlock_unlock(mapLock);
656 pthread_rwlock_unlock(mapLock);
659 profile = profilePair.first;
660 pthread_rwlock_t *profileLockPtr = profilePair.second;
661 pthread_rwlock_wrlock(profileLockPtr);
662 profile->Fill(valueX, valueY, valueZ);
663 pthread_rwlock_unlock(profileLockPtr);
669 japp->RootWriteLock();
677 TObject*
GetHistPointer(
const char * plugin,
const char * directoryName,
const char * name){
680 char fullNameChar[500];
681 sprintf(fullNameChar,
"%s/%s/%s", plugin, directoryName, name);
682 TString fullName = TString(fullNameChar);
684 auto iter1I =
Get1DMap().find(fullName);
685 if(iter1I !=
Get1DMap().end())
return (TObject*) (*iter1I).second.first;
688 if(iter1D !=
Get1DWeightedMap().end())
return (TObject*) (*iter1D).second.first;
691 if(iter1P !=
Get1DProfileMap().end())
return (TObject*) (*iter1P).second.first;
693 auto iter2I =
Get2DMap().find(fullName);
694 if(iter2I !=
Get2DMap().end())
return (TObject*) (*iter2I).second.first;
697 if(iter2D !=
Get2DWeightedMap().end())
return (TObject*) (*iter2D).second.first;
700 if(iter2P !=
Get2DProfileMap().end())
return (TObject*) (*iter2P).second.first;
702 auto iter3I =
Get3DMap().find(fullName);
703 if(iter3I !=
Get3DMap().end())
return (TObject*) (*iter3I).second.first;
705 jout <<
"Unable to find histogram " << fullName.Data() << endl;
sprintf(text,"Post KinFit Cut")