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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
/*
 *  Class:DQMRivetClient 
 *
 *
 * 
 *  \author Junghwan Goh - SungKyunKwan University
 */

#include "GeneratorInterface/RivetInterface/interface/DQMRivetClient.h"

#include "DQMServices/Core/interface/DQMStore.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include <TH1F.h>
#include <TClass.h>
#include <TString.h>
#include <TPRegexp.h>

#include <cmath>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace edm;

typedef DQMRivetClient::MonitorElement ME;

DQMRivetClient::DQMRivetClient(const ParameterSet& pset) {
  typedef std::vector<edm::ParameterSet> VPSet;
  typedef std::vector<std::string> vstring;
  typedef boost::escaped_list_separator<char> elsc;

  usesResource("DQMStore");

  elsc commonEscapes("\\", " \t", "\'");

  // Parse Normalization commands
  vstring normCmds = pset.getUntrackedParameter<vstring>("normalizationToIntegral", vstring());
  for (vstring::const_iterator normCmd = normCmds.begin(); normCmd != normCmds.end(); ++normCmd) {
    if (normCmd->empty())
      continue;
    boost::tokenizer<elsc> tokens(*normCmd, commonEscapes);

    vector<string> args;
    for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
      if (iToken->empty())
        continue;
      args.push_back(*iToken);
    }

    if (args.empty() or args.size() > 2) {
      LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
      continue;
    }

    NormOption opt;
    opt.name = args[0];
    opt.normHistName = args.size() == 2 ? args[1] : args[0];

    normOptions_.push_back(opt);
  }

  VPSet normSets = pset.getUntrackedParameter<VPSet>("normalizationToIntegralSets", VPSet());
  for (VPSet::const_iterator normSet = normSets.begin(); normSet != normSets.end(); ++normSet) {
    NormOption opt;
    opt.name = normSet->getUntrackedParameter<string>("name");
    opt.normHistName = normSet->getUntrackedParameter<string>("normalizedTo", opt.name);

    normOptions_.push_back(opt);
  }

  //normalize to lumi
  vstring lumiCmds = pset.getUntrackedParameter<vstring>("normalizationToLumi", vstring());
  for (vstring::const_iterator lumiCmd = lumiCmds.begin(); lumiCmd != lumiCmds.end(); ++lumiCmd) {
    if (lumiCmd->empty())
      continue;
    boost::tokenizer<elsc> tokens(*lumiCmd, commonEscapes);

    vector<string> args;
    for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
      if (iToken->empty())
        continue;
      args.push_back(*iToken);
    }

    if (args.size() != 2) {
      LogInfo("DQMRivetClient") << "Wrong input to lumiCmds\n";
      continue;
    }

    DQMRivetClient::LumiOption opt;
    opt.name = args[0];
    opt.normHistName = args[1];
    opt.xsection = pset.getUntrackedParameter<double>("xsection", -1.);
    //opt.xsection = atof(args[2].c_str());

    //std::cout << opt.name << " " << opt.normHistName << " " << opt.xsection << std::endl;
    lumiOptions_.push_back(opt);
  }

  //multiply by a number
  vstring scaleCmds = pset.getUntrackedParameter<vstring>("scaleBy", vstring());
  for (vstring::const_iterator scaleCmd = scaleCmds.begin(); scaleCmd != scaleCmds.end(); ++scaleCmd) {
    if (scaleCmd->empty())
      continue;
    boost::tokenizer<elsc> tokens(*scaleCmd, commonEscapes);

    vector<string> args;
    for (boost::tokenizer<elsc>::const_iterator iToken = tokens.begin(); iToken != tokens.end(); ++iToken) {
      if (iToken->empty())
        continue;
      args.push_back(*iToken);
    }

    if (args.empty() or args.size() > 2) {
      LogInfo("DQMRivetClient") << "Wrong input to normCmds\n";
      continue;
    }

    ScaleFactorOption opt;
    opt.name = args[0];
    opt.scale = atof(args[1].c_str());
    scaleOptions_.push_back(opt);
  }

  outputFileName_ = pset.getUntrackedParameter<string>("outputFileName", "");
  subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
}

void DQMRivetClient::endRun(const edm::Run& r, const edm::EventSetup& c) {
  typedef vector<string> vstring;

  // Update 2009-09-23
  // Migrated all code from endJob to this function
  // endJob is not necessarily called in the proper sequence
  // and does not necessarily book histograms produced in
  // that step.
  // It more robust to do the histogram manipulation in
  // this endRun function

  theDQM = nullptr;
  theDQM = Service<DQMStore>().operator->();

  if (!theDQM) {
    LogInfo("DQMRivetClient") << "Cannot create DQMStore instance\n";
    return;
  }

  // Process wildcard in the sub-directory
  set<string> subDirSet;

  for (vstring::const_iterator iSubDir = subDirs_.begin(); iSubDir != subDirs_.end(); ++iSubDir) {
    string subDir = *iSubDir;

    if (subDir[subDir.size() - 1] == '/')
      subDir.erase(subDir.size() - 1);

    subDirSet.insert(subDir);
  }

  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
    const string& dirName = *iSubDir;
    for (vector<NormOption>::const_iterator normOption = normOptions_.begin(); normOption != normOptions_.end();
         ++normOption) {
      normalizeToIntegral(dirName, normOption->name, normOption->normHistName);
    }
  }

  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
    const string& dirName = *iSubDir;
    for (vector<LumiOption>::const_iterator lumiOption = lumiOptions_.begin(); lumiOption != lumiOptions_.end();
         ++lumiOption) {
      normalizeToLumi(dirName, lumiOption->name, lumiOption->normHistName, lumiOption->xsection);
    }
  }

  for (set<string>::const_iterator iSubDir = subDirSet.begin(); iSubDir != subDirSet.end(); ++iSubDir) {
    const string& dirName = *iSubDir;
    for (vector<ScaleFactorOption>::const_iterator scaleOption = scaleOptions_.begin();
         scaleOption != scaleOptions_.end();
         ++scaleOption) {
      scaleByFactor(dirName, scaleOption->name, scaleOption->scale);
    }
  }

  if (!outputFileName_.empty())
    theDQM->save(outputFileName_);
}

void DQMRivetClient::endJob() {
  // Update 2009-09-23
  // Migrated all code from here to endRun

  LogTrace("DQMRivetClient") << "inside of ::endJob()" << endl;
}

void DQMRivetClient::normalizeToIntegral(const std::string& startDir,
                                         const std::string& histName,
                                         const std::string& normHistName) {
  if (!theDQM->dirExists(startDir)) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Cannot find sub-directory " << startDir << endl;
    return;
  }

  theDQM->cd();

  ME* element = theDQM->get(startDir + "/" + histName);
  ME* normME = theDQM->get(startDir + "/" + normHistName);

  if (!element) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "No such element '" << histName << "' found\n";
    return;
  }

  if (!normME) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "No such element '" << normHistName << "' found\n";
    return;
  }

  TH1F* hist = element->getTH1F();
  if (!hist) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Cannot create TH1F from ME\n";
    return;
  }

  TH1F* normHist = normME->getTH1F();
  if (!normHist) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Cannot create TH1F from ME\n";
    return;
  }

  const double entries = normHist->Integral();
  if (entries != 0) {
    hist->Scale(1. / entries, "width");
  } else {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Zero entries in histogram\n";
  }

  return;
}

void DQMRivetClient::normalizeToLumi(const std::string& startDir,
                                     const std::string& histName,
                                     const std::string& normHistName,
                                     double xsection) {
  normalizeToIntegral(startDir, histName, normHistName);
  theDQM->cd();
  ME* element = theDQM->get(startDir + "/" + histName);
  TH1F* hist = element->getTH1F();
  if (!hist) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Cannot create TH1F from ME\n";
    return;
  }
  hist->Scale(xsection);
  return;
}

void DQMRivetClient::scaleByFactor(const std::string& startDir, const std::string& histName, double factor) {
  if (!theDQM->dirExists(startDir)) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Cannot find sub-directory " << startDir << endl;
    return;
  }

  theDQM->cd();

  ME* element = theDQM->get(startDir + "/" + histName);

  if (!element) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "No such element '" << histName << "' found\n";
    return;
  }

  TH1F* hist = element->getTH1F();
  if (!hist) {
    LogInfo("DQMRivetClient") << "normalizeToEntries() : "
                              << "Cannot create TH1F from ME\n";
    return;
  }
  hist->Scale(factor);
}

#include "FWCore/Framework/interface/MakerMacros.h"

DEFINE_FWK_MODULE(DQMRivetClient);