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
/*
 *  See header file for a description of this class.
 *
 *  \author S. Bolognesi
 */

#include "CalibMuon/DTCalibration/plugins/DTTTrigWriter.h"
#include "CalibMuon/DTCalibration/interface/DTTimeBoxFitter.h"
#include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"

#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "DataFormats/MuonDetId/interface/DTSuperLayerId.h"
#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/Records/interface/MuonGeometryRecord.h"

#include "CondFormats/DTObjects/interface/DTTtrig.h"
#include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
#include "CondFormats/DTObjects/interface/DTStatusFlag.h"

/* C++ Headers */
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "TFile.h"
#include "TH1.h"

using namespace std;
using namespace edm;

// Constructor
DTTTrigWriter::DTTTrigWriter(const ParameterSet& pset) : dtGeomToken_(esConsumes()) {
  // get selected debug option
  debug = pset.getUntrackedParameter<bool>("debug", false);

  // Open the root file which contains the histos
  theRootInputFile = pset.getUntrackedParameter<string>("rootFileName");
  theFile = new TFile(theRootInputFile.c_str(), "READ");
  theFile->cd();
  theFitter = new DTTimeBoxFitter();
  if (debug)
    theFitter->setVerbosity(1);

  double sigmaFit = pset.getUntrackedParameter<double>("sigmaTTrigFit", 10.);
  theFitter->setFitSigma(sigmaFit);

  // the kfactor to be uploaded in the ttrig DB
  kFactor = pset.getUntrackedParameter<double>("kFactor", -0.7);

  // Create the object to be written to DB
  tTrig = new DTTtrig();

  if (debug)
    cout << "[DTTTrigWriter]Constructor called!" << endl;
}

// Destructor
DTTTrigWriter::~DTTTrigWriter() {
  if (debug)
    cout << "[DTTTrigWriter]Destructor called!" << endl;
  theFile->Close();
  delete theFitter;
}

// Do the job
void DTTTrigWriter::analyze(const Event& event, const EventSetup& eventSetup) {
  if (debug)
    cout << "[DTTTrigWriter]Analyzer called!" << endl;

  // Get the DT Geometry
  dtGeom = eventSetup.getHandle(dtGeomToken_);

  // Get all the sls from the setup
  const vector<const DTSuperLayer*> superLayers = dtGeom->superLayers();

  // Loop over all SLs
  for (auto sl = superLayers.begin(); sl != superLayers.end(); sl++) {
    // Get the histo from file
    DTSuperLayerId slId = (*sl)->id();
    TH1F* histo = (TH1F*)theFile->Get((getTBoxName(slId)).c_str());
    if (histo) {  // Check that the histo exists
      // Compute mean and sigma of the rising edge
      pair<double, double> meanAndSigma = theFitter->fitTimeBox(histo);

      // Write them in DB object
      tTrig->set(slId, meanAndSigma.first, meanAndSigma.second, kFactor, DTTimeUnits::ns);
      if (debug) {
        cout << " SL: " << slId << " mean = " << meanAndSigma.first << " sigma = " << meanAndSigma.second << endl;
      }
    }
  }
}

// Write objects to DB
void DTTTrigWriter::endJob() {
  if (debug)
    cout << "[DTTTrigWriter]Writing ttrig object to DB!" << endl;

  // FIXME: to be read from cfg?
  string tTrigRecord = "DTTtrigRcd";

  // Write the object to DB
  DTCalibDBUtils::writeToDB(tTrigRecord, *tTrig);
}

// Compute the name of the time box histo
string DTTTrigWriter::getTBoxName(const DTSuperLayerId& slId) const {
  string histoName = "Ch_" + std::to_string(slId.wheel()) + "_" + std::to_string(slId.station()) + "_" +
                     std::to_string(slId.sector()) + "_SL" + std::to_string(slId.superlayer()) + "_hTimeBox";
  return histoName;
}