Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:56

0001 #ifndef ALIGNMENT_OFFLINEVALIDATION_JETHTANALYZER_SMARTSELECTIONMONITOR_H
0002 #define ALIGNMENT_OFFLINEVALIDATION_JETHTANALYZER_SMARTSELECTIONMONITOR_H
0003 
0004 // system include files
0005 #include <iostream>
0006 #include <string>
0007 #include <map>
0008 #include <algorithm>
0009 #include <vector>
0010 #include <memory>
0011 #include <unordered_map>
0012 
0013 // user include files
0014 #include "TH1D.h"
0015 #include "TH2D.h"
0016 #include "TProfile.h"
0017 #include "TString.h"
0018 #include "TROOT.h"
0019 
0020 class SmartSelectionMonitor {
0021 public:
0022   SmartSelectionMonitor() {}
0023   ~SmartSelectionMonitor() = default;
0024 
0025   typedef std::unordered_map<std::string, std::map<std::string, TH1*>*> Monitor_t;
0026 
0027   //short getters
0028   inline Monitor_t& getAllMonitors() { return allMonitors_; }
0029 
0030   //checks if base Histo Exist
0031   inline bool hasBaseHisto(std::string histo) {
0032     if (allMonitors_.find(histo) == allMonitors_.end())
0033       return false;
0034     return true;
0035   }
0036 
0037   //checks if tag Exist for a given histo
0038   inline bool hasTag(std::map<std::string, TH1*>* map, std::string tag) {
0039     if (map->find(tag) != map->end())
0040       return true;
0041     if (map->find("all") == map->end())
0042       return false;
0043 
0044     TH1* base = (*map)["all"];
0045     TString allName = base->GetName();
0046     TString name = tag + "_" + allName.Data();
0047     TH1* h = (TH1*)base->Clone(name);
0048     h->SetName(name);
0049     h->SetTitle(name);
0050     h->Reset("ICE");
0051     h->SetDirectory(gROOT);
0052     (*map)[tag] = h;
0053     return true;
0054   }
0055 
0056   //checks if tag Exist for a given histo
0057   inline bool hasTag(std::string histo, std::string tag) {
0058     if (!hasBaseHisto(histo))
0059       return false;
0060     std::map<std::string, TH1*>* map = allMonitors_[histo];
0061     return hasTag(map, tag);
0062   }
0063 
0064   //get histo
0065   inline TH1* getHisto(std::string histo, std::string tag = "all") {
0066     if (!hasBaseHisto(histo))
0067       return nullptr;
0068     std::map<std::string, TH1*>* map = allMonitors_[histo];
0069     if (!hasTag(map, tag))
0070       return nullptr;
0071     return (*map)[tag];
0072   }
0073 
0074   //write all histo
0075   inline void Write() {
0076     for (Monitor_t::iterator it = allMonitors_.begin(); it != allMonitors_.end(); it++) {
0077       std::map<std::string, TH1*>* map = it->second;
0078       bool neverFilled = true;
0079 
0080       for (std::map<std::string, TH1*>::iterator h = map->begin(); h != map->end(); h++) {
0081         if (!(h->second)) {
0082           printf("histo = %30s %15s IS NULL", it->first.c_str(), h->first.c_str());
0083           continue;
0084         }
0085         if (h->second->GetEntries() > 0)
0086           neverFilled = false;
0087 
0088         if (h->first == "all") {
0089           h->second->SetName(Form("%s_%s", h->first.c_str(), h->second->GetName()));
0090         }
0091         h->second->Write();
0092       }
0093 
0094       if (neverFilled) {
0095         printf(
0096             "SmartSelectionMonitor: histo = '%s' is empty for all categories, you may want to cleanup your project to "
0097             "remove this histogram\n",
0098             it->first.c_str());
0099       }
0100     }
0101   }
0102 
0103   //scale all histo by w
0104   inline void Scale(double w) {
0105     for (Monitor_t::iterator it = allMonitors_.begin(); it != allMonitors_.end(); it++) {
0106       std::map<std::string, TH1*>* map = it->second;
0107       for (std::map<std::string, TH1*>::iterator h = map->begin(); h != map->end(); h++) {
0108         if (!(h->second)) {
0109           continue;
0110         }
0111         h->second->Scale(w);
0112       }
0113     }
0114   }
0115 
0116   //takes care of filling an histogram
0117   bool fillHisto(std::string name, std::string tag, double valx, double weight, bool useBinWidth = false);
0118   bool fillHisto(std::string name, std::string tag, double valx, double valy, double weight, bool useBinWidth = false);
0119   bool fillProfile(std::string name, std::string tag, double valx, double valy, double weight);
0120 
0121   bool fillHisto(std::string name, std::vector<std::string> tags, double valx, double weight, bool useBinWidth = false);
0122   bool fillHisto(std::string name,
0123                  std::vector<std::string> tags,
0124                  double valx,
0125                  double valy,
0126                  double weight,
0127                  bool useBinWidth = false);
0128   bool fillProfile(std::string name, std::vector<std::string> tags, double valx, double valy, double weight);
0129 
0130   bool fillHisto(std::string name,
0131                  std::vector<std::string> tags,
0132                  double valx,
0133                  std::vector<double> weights,
0134                  bool useBinWidth = false);
0135   bool fillHisto(std::string name,
0136                  std::vector<std::string> tags,
0137                  double valx,
0138                  double valy,
0139                  std::vector<double> weights,
0140                  bool useBinWidth = false);
0141   bool fillProfile(
0142       std::string name, std::vector<std::string> tags, double valx, double valy, std::vector<double> weights);
0143 
0144   //short inits the monitor plots for a new step
0145   void initMonitorForStep(std::string tag);
0146 
0147   //short add new histogram
0148   TH1* addHistogram(TH1* h, std::string tag);
0149   TH1* addHistogram(TH1* h);
0150 
0151 private:
0152   //all the selection step monitors
0153   Monitor_t allMonitors_;
0154 };
0155 
0156 #endif