00001 #ifndef smartselectionmonitor_hh
00002 #define smartselectionmonitor_hh
00003
00004
00005 #include<iostream>
00006 #include<string>
00007 #include<map>
00008 #include<unordered_map>
00009 #include<algorithm>
00010 #include<vector>
00011 #include<memory>
00012
00013
00014 #include "TH1D.h"
00015 #include "TProfile.h"
00016 #include "TH2D.h"
00017 #include "TString.h"
00018 #include "TROOT.h"
00019
00020 namespace std{
00021 template<> struct hash< TString >{ size_t operator()( const TString& x ) const{ return hash<std::string>()( x.Data() ); } };
00022 }
00023
00024 class SmartSelectionMonitor {
00025
00026 public:
00027
00028 SmartSelectionMonitor(){}
00029 ~SmartSelectionMonitor() { }
00030
00031
00032
00033 typedef std::unordered_map<TString, std::map<TString, TH1*>* > Monitor_t;
00034
00035
00036
00037 inline Monitor_t &getAllMonitors() { return allMonitors_; }
00038
00039
00040 inline bool hasBaseHisto(TString histo){
00041 if(allMonitors_.find(histo) == allMonitors_.end())return false;
00042 return true;
00043 }
00044
00045
00046 inline bool hasTag(std::map<TString, TH1*>* map, TString tag, bool useBinWidth = false){
00047 if( map->find(tag) != map->end() )return true;
00048 if( map->find("all") == map->end() )return false;
00049
00050 TH1* base = (*map)["all"];
00051 TString allName = base->GetName();
00052 TString name = allName + "_" + tag;
00053 TH1* h = (TH1*) base->Clone(name);
00054 h->SetName(name);
00055 h->SetTitle(name);
00056 if(useBinWidth){
00057 TString yaxisTitle = h->GetYaxis()->GetTitle();
00058 if(!yaxisTitle.Contains("bin")) yaxisTitle += " / GeV";
00059 h->GetYaxis()->SetTitle(yaxisTitle);
00060 }
00061 h->Reset("ICE");
00062 h->SetDirectory(gROOT);
00063 (*map)[tag] = h;
00064
00065 return true;
00066 }
00067
00068
00069 inline TH1 *getHisto(TString histo,TString tag, bool useBinWidth = false){
00070 if( !hasBaseHisto(histo) )return NULL;
00071 std::map<TString, TH1*>* map = allMonitors_[histo];
00072 if( !hasTag(map, tag, useBinWidth) )return NULL;
00073 return (*map)[tag];
00074 }
00075
00076
00077 inline void Write(){
00078 for(Monitor_t::iterator it =allMonitors_.begin(); it!= allMonitors_.end(); it++){
00079 std::map<TString, TH1*>* map = it->second;
00080 bool neverFilled = true;
00081
00082 for(std::map<TString, TH1*>::iterator h =map->begin(); h!= map->end(); h++){
00083 if(!(h->second)){printf("histo = %30s %15s IS NULL",it->first.Data(), h->first.Data());continue;}
00084 if(h->second->GetEntries()>0)neverFilled = false;
00085
00086 if(h->first=="all"){h->second->SetName(h->first+"_"+h->second->GetName());}
00087
00088 if(h->first!="all") h->second->Write();
00089 }
00090
00091 if(neverFilled){printf("SmartSelectionMonitor: histo = '%s' is empty for all categories, you may want to cleanup your project to remove this histogram\n",it->first.Data());}
00092 }
00093 }
00094
00095
00096 inline void Scale(double w){
00097 for(Monitor_t::iterator it =allMonitors_.begin(); it!= allMonitors_.end(); it++){
00098 std::map<TString, TH1*>* map = it->second;
00099 for(std::map<TString, TH1*>::iterator h =map->begin(); h!= map->end(); h++){
00100 if(!(h->second)){continue;}
00101 h->second->Scale(w);
00102 }
00103 }
00104 }
00105
00106
00107
00108 bool fillHisto (TString name, TString tag, double valx, double weight, bool useBinWidth=false);
00109 bool fillHisto (TString name, TString tag, double valx, double valy, double weight, bool useBinWidth=false);
00110 bool fillProfile(TString name, TString tag, double valx, double valy, double weight);
00111
00112 bool fillHisto(TString name, std::vector<TString> tags, double valx, double weight, bool useBinWidth=false);
00113 bool fillHisto(TString name, std::vector<TString> tags, double valx, double valy, double weight, bool useBinWidth=false);
00114 bool fillProfile(TString name, std::vector<TString> tags, double valx, double valy, double weight);
00115
00116 bool fillHisto(TString name, std::vector<TString> tags, double valx, std::vector<double> weights, bool useBinWidth=false);
00117 bool fillHisto(TString name, std::vector<TString> tags, double valx, double valy, std::vector<double> weights, bool useBinWidth=false);
00118 bool fillProfile(TString name, std::vector<TString> tags, double valx, double valy, std::vector<double> weights);
00119
00120 bool setBinContentAndError(TString name, TString tag, double bin, double content, double error=0, bool useBinWidth=false);
00121
00122
00123 void initMonitorForStep(TString tag);
00124
00125
00126 TH1 * addHistogram(TH1 *h, TString tag);
00127 TH1 * addHistogram(TH1 *h);
00128
00129 public:
00130
00131
00132 Monitor_t allMonitors_;
00133 };
00134
00135 #endif