SmartSelectionMonitor

Macros

Line Code
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
#ifndef ALIGNMENT_OFFLINEVALIDATION_JETHTANALYZER_SMARTSELECTIONMONITOR_H
#define ALIGNMENT_OFFLINEVALIDATION_JETHTANALYZER_SMARTSELECTIONMONITOR_H

// system include files
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <memory>
#include <unordered_map>

// user include files
#include "TH1D.h"
#include "TH2D.h"
#include "TProfile.h"
#include "TString.h"
#include "TROOT.h"

class SmartSelectionMonitor {
public:
  SmartSelectionMonitor() {}
  ~SmartSelectionMonitor() = default;

  typedef std::unordered_map<std::string, std::map<std::string, TH1*>*> Monitor_t;

  //short getters
  inline Monitor_t& getAllMonitors() { return allMonitors_; }

  //checks if base Histo Exist
  inline bool hasBaseHisto(std::string histo) {
    if (allMonitors_.find(histo) == allMonitors_.end())
      return false;
    return true;
  }

  //checks if tag Exist for a given histo
  inline bool hasTag(std::map<std::string, TH1*>* map, std::string tag) {
    if (map->find(tag) != map->end())
      return true;
    if (map->find("all") == map->end())
      return false;

    TH1* base = (*map)["all"];
    TString allName = base->GetName();
    TString name = tag + "_" + allName.Data();
    TH1* h = (TH1*)base->Clone(name);
    h->SetName(name);
    h->SetTitle(name);
    h->Reset("ICE");
    h->SetDirectory(gROOT);
    (*map)[tag] = h;
    return true;
  }

  //checks if tag Exist for a given histo
  inline bool hasTag(std::string histo, std::string tag) {
    if (!hasBaseHisto(histo))
      return false;
    std::map<std::string, TH1*>* map = allMonitors_[histo];
    return hasTag(map, tag);
  }

  //get histo
  inline TH1* getHisto(std::string histo, std::string tag = "all") {
    if (!hasBaseHisto(histo))
      return nullptr;
    std::map<std::string, TH1*>* map = allMonitors_[histo];
    if (!hasTag(map, tag))
      return nullptr;
    return (*map)[tag];
  }

  //write all histo
  inline void Write() {
    for (Monitor_t::iterator it = allMonitors_.begin(); it != allMonitors_.end(); it++) {
      std::map<std::string, TH1*>* map = it->second;
      bool neverFilled = true;

      for (std::map<std::string, TH1*>::iterator h = map->begin(); h != map->end(); h++) {
        if (!(h->second)) {
          printf("histo = %30s %15s IS NULL", it->first.c_str(), h->first.c_str());
          continue;
        }
        if (h->second->GetEntries() > 0)
          neverFilled = false;

        if (h->first == "all") {
          h->second->SetName(Form("%s_%s", h->first.c_str(), h->second->GetName()));
        }
        h->second->Write();
      }

      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.c_str());
      }
    }
  }

  //scale all histo by w
  inline void Scale(double w) {
    for (Monitor_t::iterator it = allMonitors_.begin(); it != allMonitors_.end(); it++) {
      std::map<std::string, TH1*>* map = it->second;
      for (std::map<std::string, TH1*>::iterator h = map->begin(); h != map->end(); h++) {
        if (!(h->second)) {
          continue;
        }
        h->second->Scale(w);
      }
    }
  }

  //takes care of filling an histogram
  bool fillHisto(std::string name, std::string tag, double valx, double weight, bool useBinWidth = false);
  bool fillHisto(std::string name, std::string tag, double valx, double valy, double weight, bool useBinWidth = false);
  bool fillProfile(std::string name, std::string tag, double valx, double valy, double weight);

  bool fillHisto(std::string name, std::vector<std::string> tags, double valx, double weight, bool useBinWidth = false);
  bool fillHisto(std::string name,
                 std::vector<std::string> tags,
                 double valx,
                 double valy,
                 double weight,
                 bool useBinWidth = false);
  bool fillProfile(std::string name, std::vector<std::string> tags, double valx, double valy, double weight);

  bool fillHisto(std::string name,
                 std::vector<std::string> tags,
                 double valx,
                 std::vector<double> weights,
                 bool useBinWidth = false);
  bool fillHisto(std::string name,
                 std::vector<std::string> tags,
                 double valx,
                 double valy,
                 std::vector<double> weights,
                 bool useBinWidth = false);
  bool fillProfile(
      std::string name, std::vector<std::string> tags, double valx, double valy, std::vector<double> weights);

  //short inits the monitor plots for a new step
  void initMonitorForStep(std::string tag);

  //short add new histogram
  TH1* addHistogram(TH1* h, std::string tag);
  TH1* addHistogram(TH1* h);

private:
  //all the selection step monitors
  Monitor_t allMonitors_;
};

#endif