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

/*
 *  See header file for a description of this class.
 *
 *  \author S. Bolognesi - INFN Torino
 */

#include "DTT0Analyzer.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "CondFormats/DTObjects/interface/DTT0.h"
#include "CondFormats/DataRecord/interface/DTT0Rcd.h"
#include <iostream>
#include "TFile.h"
#include "TH1D.h"
#include "TString.h"

using namespace edm;
using namespace std;

DTT0Analyzer::DTT0Analyzer(const ParameterSet& pset) {
  // The root file which will contain the histos
  string rootFileName = pset.getUntrackedParameter<string>("rootFileName");
  theFile = new TFile(rootFileName.c_str(), "RECREATE");
  theFile->cd();
  t0Token_ = esConsumes<edm::Transition::BeginRun>();
  dtGeomToken_ = esConsumes<edm::Transition::BeginRun>();
}

DTT0Analyzer::~DTT0Analyzer() { theFile->Close(); }

void DTT0Analyzer::beginRun(const edm::Run&, const edm::EventSetup& eventSetup) {
  //Get the t0 map from the DB
  ESHandle<DTT0> t0 = eventSetup.getHandle(t0Token_);
  tZeroMap = &*t0;

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

void DTT0Analyzer::endJob() {
  // Loop over DB entries
  for (DTT0::const_iterator tzero = tZeroMap->begin(); tzero != tZeroMap->end(); ++tzero) {
    // @@@ NEW DTT0 FORMAT
    //    DTWireId wireId((*tzero).first.wheelId,
    //		    (*tzero).first.stationId,
    //		    (*tzero).first.sectorId,
    //		    (*tzero).first.slId,
    //		    (*tzero).first.layerId,
    //		    (*tzero).first.cellId);
    int channelId = tzero->channelId;
    if (channelId == 0)
      continue;
    DTWireId wireId(channelId);
    // @@@ NEW DTT0 END
    float t0mean;
    float t0rms;
    // t0s and rms are TDC counts
    tZeroMap->get(wireId, t0mean, t0rms, DTTimeUnits::counts);
    cout << "Wire: " << wireId << endl
         << " T0 mean (TDC counts): " << t0mean << " T0_rms (TDC counts): " << t0rms << endl;

    DTLayerId layerId = wireId.layerId();
    const int nWires = dtGeom->layer(layerId)->specificTopology().channels();

    //Define an histo for means and an histo for sigmas for each layer
    TH1D* hT0Histo = theMeanHistoMap[layerId];
    if (hT0Histo == 0) {
      theFile->cd();
      TString name = getHistoName(layerId).c_str();
      hT0Histo = new TH1D(name + "_t0Mean",
                          "mean T0 from pulses by Channel",
                          nWires,
                          dtGeom->layer(layerId)->specificTopology().firstChannel(),
                          dtGeom->layer(layerId)->specificTopology().firstChannel() + nWires);
      theMeanHistoMap[layerId] = hT0Histo;
    }

    TH1D* hSigmaT0Histo = theSigmaHistoMap[layerId];
    if (hSigmaT0Histo == 0) {
      theFile->cd();
      TString name = getHistoName(layerId).c_str();
      hSigmaT0Histo = new TH1D(name + "_t0Sigma",
                               "sigma T0 from pulses by Channel",
                               nWires,
                               dtGeom->layer(layerId)->specificTopology().firstChannel(),
                               dtGeom->layer(layerId)->specificTopology().firstChannel() + nWires);
      theSigmaHistoMap[layerId] = hSigmaT0Histo;
    }

    //Fill the histos
    int nBin = hT0Histo->GetXaxis()->FindFixBin(wireId.wire());
    hT0Histo->SetBinContent(nBin, t0mean);
    hSigmaT0Histo->SetBinContent(nBin, t0rms);
  }

  //Write histos in a .root file
  theFile->cd();
  for (map<DTLayerId, TH1D*>::const_iterator lHisto = theMeanHistoMap.begin(); lHisto != theMeanHistoMap.end();
       ++lHisto) {
    (*lHisto).second->Write();
  }

  for (map<DTLayerId, TH1D*>::const_iterator lHisto = theSigmaHistoMap.begin(); lHisto != theSigmaHistoMap.end();
       ++lHisto) {
    (*lHisto).second->Write();
  }
}

string DTT0Analyzer::getHistoName(const DTLayerId& lId) const {
  string histoName;
  stringstream theStream;
  theStream << "Ch_" << lId.wheel() << "_" << lId.station() << "_" << lId.sector() << "_SL" << lId.superlayer() << "_L"
            << lId.layer();
  theStream >> histoName;
  return histoName;
}