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
/*
 *  See header file for a description of this class.
 *
 *  \author G. Mila - INFN Torino
 */

#include "CalibMuon/DTCalibration/test/DBTools/FakeTTrig.h"

// Framework
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "CalibMuon/DTCalibration/test/DBTools/DTCalibrationMap.h"
#include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"

// Geometry
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/DTGeometry/interface/DTSuperLayer.h"
#include "DataFormats/GeometryVector/interface/GlobalPoint.h"
#include "Geometry/DTGeometry/interface/DTTopology.h"

// Database
#include "CondFormats/DTObjects/interface/DTTtrig.h"
#include "CondFormats/DataRecord/interface/DTTtrigRcd.h"

//Random generator
#include "FWCore/AbstractServices/interface/RandomNumberGenerator.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "CLHEP/Random/RandGaussQ.h"

// DTDigitizer
#include "CalibMuon/DTDigiSync/interface/DTTTrigSyncFactory.h"
#include "CalibMuon/DTDigiSync/interface/DTTTrigBaseSync.h"

using namespace std;
using namespace edm;

FakeTTrig::FakeTTrig(const ParameterSet& pset) : dataBaseWriteWasDone(false) {
  cout << "[FakeTTrig] Constructor called! " << endl;

  // further configurable smearing
  smearing = pset.getUntrackedParameter<double>("smearing");
  ttrigToken_ =
      esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", pset.getUntrackedParameter<string>("dbLabel")));
  dtGeomToken_ = esConsumes<edm::Transition::BeginRun>();
  // get random engine
  edm::Service<edm::RandomNumberGenerator> rng;
  if (!rng.isAvailable()) {
    throw cms::Exception("Configuration") << "RandomNumberGeneratorService for DTFakeTTrigDB missing in cfg file";
  }
  ps = pset;
}

FakeTTrig::~FakeTTrig() { cout << "[FakeTTrig] Destructor called! " << endl; }

void FakeTTrig::beginRun(const edm::Run&, const EventSetup& setup) {
  cout << "[FakeTTrig] entered into beginRun! " << endl;
  muonGeom = setup.getHandle(dtGeomToken_);

  // Get the tTrig reference map
  if (ps.getUntrackedParameter<bool>("readDB", true))
    tTrigMapRef = setup.getHandle(ttrigToken_);
}

void FakeTTrig::beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
  if (!dataBaseWriteWasDone) {
    dataBaseWriteWasDone = true;

    cout << "[FakeTTrig] entered into beginLuminosityBlock! " << endl;

    edm::Service<edm::RandomNumberGenerator> rng;
    CLHEP::HepRandomEngine* engine = &rng->getEngine(lumi.index());

    // Get the superlayers and layers list
    vector<const DTSuperLayer*> dtSupLylist = muonGeom->superLayers();
    // Create the object to be written to DB
    DTTtrig tTrigMap;

    for (auto sl = dtSupLylist.begin(); sl != dtSupLylist.end(); sl++) {
      // get the time of fly
      double timeOfFly = tofComputation(*sl);
      // get the time of wire propagation
      double timeOfWirePropagation = wirePropComputation(*sl);
      // get the gaussian smearing
      double gaussianSmearing = CLHEP::RandGaussQ::shoot(engine, 0., smearing);
      // get the fake tTrig pedestal
      double pedestral = ps.getUntrackedParameter<double>("fakeTTrigPedestal", 500);

      if (ps.getUntrackedParameter<bool>("readDB", true)) {
        tTrigMapRef->get((*sl)->id(), tTrigRef, tTrigRMSRef, kFactorRef, DTTimeUnits::ns);
        // pedestral = tTrigRef;
        pedestral = tTrigRef + kFactorRef * tTrigRMSRef;
      }

      DTSuperLayerId slId = (*sl)->id();
      // if the FakeTtrig has to be smeared with a Gaussian
      double fakeTTrig = pedestral + timeOfFly + timeOfWirePropagation + gaussianSmearing;
      // if the FakeTtrig is scaled of a number of bunch crossing
      //  double fakeTTrig = pedestral - 75.;
      tTrigMap.set(slId, fakeTTrig, 0, 0, DTTimeUnits::ns);
    }

    // Write the object in the DB
    cout << "[FakeTTrig] Writing ttrig object to DB!" << endl;
    string record = "DTTtrigRcd";
    DTCalibDBUtils::writeToDB<DTTtrig>(record, tTrigMap);
  }
}

void FakeTTrig::endJob() { cout << "[FakeTTrig] entered into endJob! " << endl; }

double FakeTTrig::tofComputation(const DTSuperLayer* superlayer) {
  double tof = 0;
  const double cSpeed = 29.9792458;  // cm/ns

  if (ps.getUntrackedParameter<bool>("useTofCorrection", true)) {
    LocalPoint localPos(0, 0, 0);
    double flight = superlayer->surface().toGlobal(localPos).mag();
    tof = flight / cSpeed;
  }

  return tof;
}

double FakeTTrig::wirePropComputation(const DTSuperLayer* superlayer) {
  double delay = 0;
  double theVPropWire = ps.getUntrackedParameter<double>("vPropWire", 24.4);  // cm/ns

  if (ps.getUntrackedParameter<bool>("useWirePropCorrection", true)) {
    DTLayerId lId = DTLayerId(superlayer->id(), 1);
    float halfL = superlayer->layer(lId)->specificTopology().cellLenght() / 2;
    delay = halfL / theVPropWire;
  }

  return delay;
}