AnalyzeTiming

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
#ifndef AnalyzeTiming_h_
#define AnalyzeTiming_h_

#include <TH1F.h>
#include <TFile.h>

#include "DataFormats/HLTReco/interface/ModuleTiming.h"

#include <map>
#include <string>
#include <iostream>

// class for analyzing the timing of the trigger modules
class AnalyzeTiming {
private:
  // times are given in secs; convert to msecs here
  static const double secs2msecs;

public:
  // create object with # of bins and range for histogram (in msecs)
  AnalyzeTiming(unsigned NBINS, double Xlow, double Xmax) : tot_time(0) {
    Nbins = NBINS;
    xlow = Xlow;
    xmax = Xmax;
    moduleTimes.clear();
    tot_time = createHistogram("full_event");
  }
  //
  ~AnalyzeTiming() {
    if (tot_time)
      delete tot_time;

    for (cIt it = moduleTimes.begin(); it != moduleTimes.end(); ++it)
      delete it->second;
    moduleTimes.clear();
  }

  // call this method for every event
  void analyze(const edm::EventTime &evtTime) {
    unsigned size = evtTime.size();
    for (unsigned int i = 0; i != size; ++i) {  // loop over all modules of event

      std::string module_name = evtTime.name(i);
      TH1F *tmp = 0;
      cIt it = moduleTimes.find(module_name);
      if (it != moduleTimes.end())
        tmp = it->second;
      else {
        // first time module is encountered; create new histogram
        tmp = createHistogram(module_name);

        // add entry to map

        /* this for some reason creates duplicate entries in moduleTimes... */
        //	    moduleTimes[module_name] = tmp;

        moduleTimes[module_name.c_str()] = tmp;
      }

      tmp->Fill(evtTime.time(i) * secs2msecs);

    }  // loop over all modules of event

    tot_time->Fill(evtTime.tot_time() * secs2msecs);
  }

  // call this after all events have been processed
  void getResults() {
    TFile *out_file = new TFile("HLT_timing.root", "RECREATE");
    for (cIt it = moduleTimes.begin(); it != moduleTimes.end(); ++it)
      (it->second)->Write();

    tot_time->Write();
    //    out_file->Write();
    out_file->Close();
    delete out_file;
  }

private:
  TH1F *createHistogram(std::string name) {
    std::cout << " Creating histogram for newly added module \"" << name << "\"" << std::endl;
    char title[1024];
    snprintf(title, 1024, "Processing times for %s", name.c_str());
    TH1F *newHistogram = new TH1F(name.c_str(), title, Nbins, xlow, xmax);
    newHistogram->GetXaxis()->SetTitle("t (ms)");
    // will do our own memory management
    newHistogram->SetDirectory(0);
    return newHistogram;
  }

  // time distributions
  // (a) per module
  std::map<std::string, TH1F *> moduleTimes;
  typedef std::map<std::string, TH1F *>::const_iterator cIt;
  // (b) per event
  TH1F *tot_time;

  // histogram parameters (common for all histograms)
  unsigned int Nbins;  // # of bins
  double xlow, xmax;   // histogram range
};

const double AnalyzeTiming::secs2msecs = 1000;

#endif  // #define AnalyzeTiming_h_