CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DQM/DTMonitorClient/src/DTFineDelayCorr.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2011/03/07 11:58:15 $
00005  *  $Revision: 1.4 $
00006  *  \author M. Giunta, C. Battilana 
00007  */
00008 
00009 
00010 // This class header
00011 #include "DQM/DTMonitorClient/src/DTFineDelayCorr.h"
00012 
00013 // Framework headers
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 #include "DQMServices/Core/interface/MonitorElement.h"
00017 #include "DQMServices/Core/interface/DQMStore.h"
00018 
00019 // L1Trigger
00020 #include "L1TriggerConfig/DTTPGConfig/interface/DTConfigTrigUnit.h"
00021 
00022 // Geometry
00023 #include "DQM/DTMonitorModule/interface/DTTrigGeomUtils.h"
00024 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00025 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00026 
00027 // DB & Calib
00028 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00029 
00030 // Root
00031 #include "TF1.h"
00032 #include "TProfile.h"
00033 
00034 
00035 //C++ headers
00036 using namespace edm;
00037 using namespace std;
00038 #include <iostream>
00039 #include <sstream>
00040 #include <fstream>
00041 #include <iterator>
00042 #include <vector>
00043 #include <map>
00044 
00045 DTFineDelayCorr::DTFineDelayCorr(const ParameterSet& ps) {
00046 
00047   setConfig(ps,"DTFineDelayCorr");  // sets parameter values and name used in log file 
00048   baseFolderDCC = "DT/90-LocalTriggerSynch/";
00049   baseFolderDDU = "DT/90-LocalTriggerSynch/";
00050  
00051 }
00052 
00053 
00054 DTFineDelayCorr::~DTFineDelayCorr(){
00055 
00056 }
00057 
00058 
00059 void DTFineDelayCorr::beginJob(){
00060 
00061   // Tag for Hardware Source (DDU or DCC)
00062   hwSource = parameters.getParameter<string>("hwSource");
00063   // Tag for the t0Mean Histograms
00064   t0MeanHistoTag = parameters.getParameter<string>("t0MeanHistoTag");
00065   // Read old delays from file or from Db
00066   readOldFromDb = parameters.getParameter<bool>("readOldFromDb");
00067   // Input file name for old delays
00068   oldDelaysInputFile = parameters.getParameter<string>("oldDelaysInputFile"),
00069   // Write new delays to file or to Db
00070   writeDB = parameters.getParameter<bool>("writeDB");
00071   // Output File Name
00072   outputFileName = parameters.getParameter<string>("outputFile");
00073   // Choose to use Hist Mean or Gaussian Fit Mean
00074   gaussMean =  parameters.getParameter<bool>("gaussMean");
00075   // Require Minimum Number Of Entries in the t0Mean Histogram
00076   minEntries =  parameters.getUntrackedParameter<int>("minEntries",5);
00077 
00078 }
00079 
00080 void DTFineDelayCorr::beginRun(const Run& run, const EventSetup& evSU){
00081 
00082   DTLocalTriggerBaseTest::beginRun(run,evSU);
00083   evSU.get< DTConfigManagerRcd >().get(dtConfig);
00084   evSU.get< DTTPGParametersRcd >().get(worstPhaseMap);
00085 
00086 }
00087 
00088 void DTFineDelayCorr::runClientDiagnostic() {
00089   int coarseDelay = -999;
00090   float oldFineDelay = -999;
00091   if(!readOldFromDb) { // read old delays from txt file
00092     // **  Open and read old delays input file  ** 
00093     ifstream oldDelaysFile(oldDelaysInputFile.c_str());
00094     string line;
00095 
00096     while (getline(oldDelaysFile, line)) {
00097       if( line == "" || line[0] == '#' ) continue; 
00098       stringstream linestr;
00099       int wheelKey,sectorKey, stationKey;      
00100       linestr << line;
00101       
00102       linestr >> wheelKey
00103               >> sectorKey
00104               >> stationKey
00105               >> coarseDelay
00106               >> oldFineDelay;
00107       
00108       pair<int,float> oldDelays = make_pair(coarseDelay,oldFineDelay);
00109       DTChamberId oldDelayKey = DTChamberId(wheelKey,stationKey,sectorKey);
00110       oldDelayMap.insert(make_pair(oldDelayKey,oldDelays));
00111     }
00112   }
00113 
00114   //  ** Loop over the chambers ** 
00115   vector<DTChamber*>::const_iterator chambIt  = muonGeom->chambers().begin();
00116   vector<DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
00117   for (; chambIt!=chambEnd; ++chambIt) { 
00118     DTChamberId chId = (*chambIt)->id();
00119     uint32_t indexCh = chId.rawId();
00120     int wheel = chId.wheel();
00121     int sector = chId.sector();
00122     int station = chId.station();
00123     
00124     // ** Compute corrected values and write them to file or database **
00125     vector<float> newDelays;
00126 
00127     // **  Retrieve Delays Loaded in MiniCrates ** 
00128     if(readOldFromDb) {    // read from db 
00129       DTConfigPedestals *pedestals = dtConfig->getDTConfigPedestals();
00130       const DTLayer *layer = muonGeom->layer(DTLayerId(chId,1,1));
00131       float delay = pedestals->getOffset(DTWireId(layer->id(),layer->specificTopology().firstChannel())); 
00132       coarseDelay = int(delay/25.);
00133       oldFineDelay = delay - coarseDelay * 25.;
00134     }
00135     else {                 // read from map created from txt file
00136       coarseDelay = oldDelayMap[chId].first;
00137       oldFineDelay = oldDelayMap[chId].second;
00138     }
00139 
00140     // ** Retrieve t0Mean histograms **
00141     TH1F *t0H = getHisto<TH1F>(dbe->get(getMEName(t0MeanHistoTag,"", chId)));
00142     float newFineDelay = -999;   // initialize to dummy number
00143     cout <<"MG: " << getMEName(t0MeanHistoTag,"", chId) << " entries: " << t0H->GetEntries() << endl; 
00144     if (t0H->GetEntries() > minEntries) {
00145       Double_t mean;
00146       Double_t sigma;
00147       // ** Find Mean Value of the distribution ** 
00148       if(gaussMean) {
00149         TF1 *funct = t0H->GetFunction("gaus");
00150         mean = funct->GetParameter(1);
00151         sigma = funct->GetParameter(2);
00152       }
00153       else {
00154         mean = t0H->GetMean();
00155         sigma = t0H->GetRMS();
00156       }
00157       
00158       // ** Retrieve Worst Phase values **
00159       int wpCoarseDelay;     
00160       float wpFineDelay;
00161       worstPhaseMap->get(chId, wpCoarseDelay,  wpFineDelay, DTTimeUnits::ns);
00162 //       cout << "wpFineDelay, oldFineDelay, mean: " << wpFineDelay << " " 
00163 //         << oldFineDelay << " " << mean << endl;
00164       float bpFineDelay = (wpFineDelay < 12.5)? (wpFineDelay + 12.5) : (wpFineDelay - 12.5);  // Best Phase: half BX far from the worst phase 
00165       // ** Calculate correction **
00166       float diffFineDelays  = oldFineDelay + (mean - bpFineDelay); // positive mean shift implies positive delay correction
00167       int bxDiff = (int) (diffFineDelays / 25);
00168       coarseDelay += bxDiff;
00169       newFineDelay = fmodf(diffFineDelays, 25);
00170 //       cout << "diffFineDelays, newFineDelay, bxDiff, coarseDelay: " << diffFineDelays 
00171 //         << " "<< newFineDelay << " " << bxDiff << " " << coarseDelay << endl;
00172     }
00173     else {
00174       LogProblem(category()) << "[" << testName << "Test]:  Not enough entries in hist for Chamber "  
00175                              << indexCh << endl;
00176     }
00177 
00178     newDelays.push_back(wheel);
00179     newDelays.push_back(sector);
00180     newDelays.push_back(station);
00181     newDelays.push_back(coarseDelay);
00182     newDelays.push_back(newFineDelay);    
00183     pair< DTChamberId,  vector<float> > chDelays;
00184     chDelays.first = chId; 
00185     chDelays.second = newDelays;
00186     delayMap.insert(chDelays);
00187    }
00188 }
00189 
00190 void DTFineDelayCorr::endJob(){
00191 
00192   DTLocalTriggerBaseTest::endJob();
00193 
00194    if (writeDB) {
00195      // to be added if needed
00196    }
00197    else { // write txt file
00198      // ** Open output file **
00199      ofstream outFile(outputFileName.c_str());
00200      for(map< DTChamberId,  vector<float> >::const_iterator iter = delayMap.begin();
00201          iter != delayMap.end() ; iter++) {
00202        // writing
00203        ostream_iterator<float> oit(outFile, " ");    
00204        copy((*iter).second.begin(), (*iter).second.end(), oit);
00205        outFile << endl;
00206      }   
00207      outFile.close();
00208    }
00209 }