CMS 3D CMS Logo

DTTimeBoxFitter Class Reference

Fit the rising edge of the time box with the integral of a gaussian returning the mean value and the sigma. More...

#include <CalibMuon/DTCalibration/interface/DTTimeBoxFitter.h>

List of all members.

Public Member Functions

 DTTimeBoxFitter (const TString &debugFileName="")
 Constructor.
std::pair< double, double > fitTimeBox (TH1F *hTimeBox)
 Fit the rising edge of the time box returning mean value and sigma (first and second respectively).
void getFitSeeds (TH1F *hTBox, double &mean, double &sigma, double &tBoxMax, double &xFitMin, double &xFitMax)
 Automatically compute the seeds the range to be used for time box fit.
void getInteractiveFitSeeds (TH1F *hTBox, double &mean, double &sigma, double &tBoxMax, double &xFitMin, double &xFitMax)
 Ask the user to provide the seeds.
void setFitSigma (double sigma)
void setInteractiveFit (bool isInteractive)
 Switch to interactive fit.
void setRebinning (int reb)
 Set the rebin.
void setVerbosity (unsigned int lvl)
 Set the verbosity of the output: 0 = silent, 1 = info, 2 = debug.
virtual ~DTTimeBoxFitter ()
 Destructor.

Private Attributes

TFile * hDebugFile
bool interactiveFit
int rebin
double theSigma
unsigned int theVerbosityLevel


Detailed Description

Fit the rising edge of the time box with the integral of a gaussian returning the mean value and the sigma.

Date
2008/06/06 17:09:51
Revision
1.6
Author:
G. Cerminara - INFN Torino

Definition at line 20 of file DTTimeBoxFitter.h.


Constructor & Destructor Documentation

DTTimeBoxFitter::DTTimeBoxFitter ( const TString &  debugFileName = ""  ) 

Constructor.

Definition at line 23 of file DTTimeBoxFitter.cc.

References hDebugFile, interactiveFit, and rebin.

00023                                                              : hDebugFile(0),
00024                                                                  theVerbosityLevel(0),
00025                                                                  theSigma(10.) {
00026   // Create a root file for debug output only if needed
00027   if(debugFileName != "") hDebugFile = new TFile(debugFileName.Data(), "RECREATE");
00028   interactiveFit = false;
00029   rebin = 1;
00030 }

DTTimeBoxFitter::~DTTimeBoxFitter (  )  [virtual]

Destructor.

Definition at line 34 of file DTTimeBoxFitter.cc.

References hDebugFile.

00034                                   {
00035   if(hDebugFile != 0) hDebugFile->Close();
00036 }


Member Function Documentation

pair< double, double > DTTimeBoxFitter::fitTimeBox ( TH1F *  hTimeBox  ) 

Fit the rising edge of the time box returning mean value and sigma (first and second respectively).

Compute the ttrig (in ns) from the Time Box.

Definition at line 42 of file DTTimeBoxFitter.cc.

References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), getFitSeeds(), getInteractiveFitSeeds(), interactiveFit, intGauss(), mean(), btag::LeptonSelector::option(), rebin, and theVerbosityLevel.

Referenced by DTTTrigWriter::analyze(), DTTTrigCalibration::endJob(), and DTtTrigCalibrationTest::endLuminosityBlock().

00042                                                                {
00043 
00044     hTimeBox->Rebin(rebin);
00045   
00046   // Check if the histo contains any entry
00047   if(hTimeBox->GetEntries() == 0) {
00048     cout << "[DTTimeBoxFitter]***Error: the time box contains no entry!" << endl;
00049     return make_pair(-1, -1);
00050   }
00051 
00052   if(hTimeBox->GetEntries() < 10000) {
00053     hTimeBox->Rebin(2);
00054   }
00055 
00056   // Get seeds for the fit
00057   // The TimeBox range to be fitted (the rising edge)
00058   double xFitMin=0;     // Min value for the fit
00059   double xFitMax=0;     // Max value for the fit 
00060   double xValue=0;      // The mean value of the gaussian
00061   double xFitSigma=0;   // The sigma of the gaussian
00062   double tBoxMax=0;     // The max of the time box, it is used as seed for gaussian integral
00063 
00064   //hTimeBox->Rebin(2); //FIXME: Temporary for low statistics
00065 
00066   TH1F *hTimeBoxForSeed = (TH1F*) hTimeBox->Clone(); //FIXME: test
00067   if(!interactiveFit) {
00068   getFitSeeds(hTimeBoxForSeed, xValue, xFitSigma, tBoxMax, xFitMin, xFitMax);
00069   } else {
00070     getInteractiveFitSeeds(hTimeBoxForSeed, xValue, xFitSigma, tBoxMax, xFitMin, xFitMax);
00071   }
00072 
00073   // Define the fitting function and use fit seeds
00074   TF1 *fIntGaus = new TF1("IntGauss", intGauss, xFitMin, xFitMax, 3); 
00075   fIntGaus->SetParName(0, "Constant");
00076   fIntGaus->SetParameter(0, tBoxMax);
00077   fIntGaus->SetParName(1, "Mean");
00078   fIntGaus->SetParameter(1, xValue);
00079   fIntGaus->SetParName(2, "Sigma");
00080   fIntGaus->SetParameter(2, xFitSigma);
00081   fIntGaus->SetLineColor(kRed);
00082 
00083 
00084   // Fit the histo
00085   char *option = "Q";
00086   if(theVerbosityLevel >= 2)
00087     option = "";
00088 
00089   hTimeBox->Fit("IntGauss", option, "",xFitMin, xFitMax);
00090 
00091   // Get fitted parameters
00092   double mean =  fIntGaus->GetParameter("Mean");
00093   double sigma = fIntGaus->GetParameter("Sigma");
00094   //   double constant = fIntGaus->GetParameter("Constant");
00095   double chiSquare = fIntGaus->GetChisquare()/fIntGaus->GetNDF();
00096   
00097   if(theVerbosityLevel >= 1) {
00098     cout << " === Fit Results: " << endl;
00099     cout << "     Fitted mean = " << mean << endl;
00100     cout << "     Fitted sigma = " << sigma << endl;
00101     cout << "     Reduced Chi Square = " << chiSquare << endl;
00102   }
00103   return make_pair(mean, sigma);
00104 }

void DTTimeBoxFitter::getFitSeeds ( TH1F *  hTBox,
double &  mean,
double &  sigma,
double &  tBoxMax,
double &  xFitMin,
double &  xFitMax 
)

Automatically compute the seeds the range to be used for time box fit.

Definition at line 136 of file DTTimeBoxFitter.cc.

References funct::abs(), edmNew::copy(), GenMuonPlsPt100GeV_cfg::cout, diff, lat::endl(), hDebugFile, i, int, j, theSigma, theVerbosityLevel, and dimuonsSequences_cff::threshold.

Referenced by fitTimeBox().

00137                                                                       {
00138   if(theVerbosityLevel >= 1)
00139     cout << " === Looking for fit seeds in Time Box:" << endl;
00140 
00141 
00142   // The approximate width of the time box
00143   static const int tBoxWidth = 400; //FIXE: tune it
00144 
00145   int nBins = hTBox->GetNbinsX();
00146   const int xMin = (int)hTBox->GetXaxis()->GetXmin();
00147   const int xMax = (int)hTBox->GetXaxis()->GetXmax();
00148   const int nEntries =  (int)hTBox->GetEntries();
00149 
00150   double binValue = (double)(xMax-xMin)/(double)nBins;
00151 
00152   // Compute a threshold for TimeBox discrimination
00153   const double threshold = binValue*nEntries/(double)(tBoxWidth*3.);
00154   if(theVerbosityLevel >= 2)
00155     cout << "   Threshold for logic time box is (# entries): " <<  threshold << endl;
00156     
00157   int nRebins = 0; // protection for infinite loop
00158   while(threshold > hTBox->GetMaximum()/2. && nRebins < 5) {
00159     cout << " Rebinning!" << endl;
00160     hTBox->Rebin(2);
00161     nBins = hTBox->GetNbinsX();
00162     binValue = (double)(xMax-xMin)/(double)nBins;
00163     nRebins++;
00164   }
00165 
00166   if(hDebugFile != 0) hDebugFile->cd();
00167   TString hLName = TString(hTBox->GetName())+"L";
00168   TH1F hLTB(hLName.Data(), "Logic Time Box", nBins, xMin, xMax);
00169   // Loop over all time box bins and discriminate them accordigly to the threshold
00170   for(int i = 1; i <= nBins; i++) {
00171     if(hTBox->GetBinContent(i) > threshold)
00172       hLTB.SetBinContent(i, 1);
00173   }
00174   if(hDebugFile != 0) hLTB.Write();
00175   
00176   // Look for the time box in the "logic histo" and save beginning and lenght of each plateau
00177   vector< pair<int, int> > startAndLenght;
00178   if(theVerbosityLevel >= 2)
00179     cout << "   Look for rising and folling edges of logic time box: " << endl;
00180   int start = -1;
00181   int lenght = -1;
00182   for(int j = 1; j < nBins;j++) {
00183     int diff = (int)hLTB.GetBinContent(j+1)-(int)hLTB.GetBinContent(j);
00184     if(diff == 1) { // This is a rising edge
00185       start = j;
00186       lenght = 1;
00187       if(theVerbosityLevel >= 2) {
00188         cout << "     >>>----" << endl;
00189         cout << "      bin: " << j << " is a rising edge" << endl;
00190       }
00191     } else if(diff == -1) { // This is a falling edge
00192       startAndLenght.push_back(make_pair(start, lenght));
00193       if(theVerbosityLevel >= 2) {
00194         cout << "      bin: " << j << " is a falling edge, lenght is: " << lenght << endl;
00195         cout << "     <<<----" << endl;
00196       }
00197       start = -1;
00198       lenght = -1;
00199     } else if(diff == 0 && (int)hLTB.GetBinContent(j) == 1) { // This is a bin within the plateau
00200       lenght ++;
00201     }
00202   }
00203 
00204   if(theVerbosityLevel >= 2)
00205     cout << "   Add macro intervals: " << endl;
00206   // Look for consecutive plateau: 2 plateau are consecutive if the gap is < 5 ns
00207   vector<  pair<int, int> > superIntervals;
00208   for(vector< pair<int, int> >::const_iterator interval = startAndLenght.begin();
00209       interval != startAndLenght.end();
00210       ++interval) {
00211     pair<int, int> theInterval = (*interval);
00212     vector< pair<int, int> >::const_iterator next = interval;
00213     while(++next != startAndLenght.end()) {
00214       int gap = (*next).first - (theInterval.first+theInterval.second);
00215       double gabInNs = binValue*gap;
00216       if(theVerbosityLevel >= 2)
00217         cout << "      gap: " << gabInNs << "(ns)" << endl;
00218       if(gabInNs > 20) {
00219         break;
00220       } else {
00221         theInterval = make_pair(theInterval.first, theInterval.second+gap+(*next).second);
00222         superIntervals.push_back(theInterval);
00223         if(theVerbosityLevel >= 2)
00224           cout << "          Add interval, start: " << theInterval.first
00225                << " width: " << theInterval.second << endl;
00226       }
00227     }
00228   }
00229 
00230   // merge the vectors of intervals
00231   copy(superIntervals.begin(), superIntervals.end(), back_inserter(startAndLenght));
00232 
00233   // Look for the plateau of the right lenght
00234   if(theVerbosityLevel >= 2)
00235     cout << "    Look for the best interval:" << endl;
00236   int delta = 999999;
00237   int beginning = -1;
00238   int tbWidth = -1;
00239   for(vector< pair<int, int> >::const_iterator stAndL = startAndLenght.begin();
00240       stAndL != startAndLenght.end();
00241       stAndL++) {
00242     if(abs((*stAndL).second - tBoxWidth) < delta) {
00243       delta = abs((*stAndL).second - tBoxWidth);
00244       beginning = (*stAndL).first;
00245       tbWidth = (*stAndL).second;
00246       if(theVerbosityLevel >= 2)
00247         cout << "   Candidate: First: " <<  beginning
00248              << ", width: " << tbWidth
00249              << ", delta: " << delta << endl;
00250     }
00251   }
00252 
00253   mean = xMin + beginning*binValue;
00254   sigma = theSigma; //FIXME: estimate it!
00255 
00256   tBoxMax = hTBox->GetMaximum();
00257 
00258   // Define the fit range
00259   xFitMin = mean-5.*sigma;
00260   xFitMax = mean+5.*sigma;
00261 
00262   if(theVerbosityLevel >= 1) {
00263     cout << "      Time Box Rising edge: " << mean << endl;
00264     cout << "      Time Box Width: " << tbWidth*binValue << endl;
00265     cout << "    = Seeds and range for fit:" << endl;
00266     cout << "       Seed mean = " << mean << endl;
00267     cout << "       Seed sigma = " << sigma << endl;
00268     cout << "       Fitting from = " << xFitMin << " to " << xFitMax << endl << endl;
00269   }
00270 }

void DTTimeBoxFitter::getInteractiveFitSeeds ( TH1F *  hTBox,
double &  mean,
double &  sigma,
double &  tBoxMax,
double &  xFitMin,
double &  xFitMax 
)

Ask the user to provide the seeds.

Definition at line 109 of file DTTimeBoxFitter.cc.

References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), theSigma, and theVerbosityLevel.

Referenced by fitTimeBox().

00110                                                                       {
00111   if(theVerbosityLevel >= 1)
00112     cout << " === Insert seeds for the Time Box fit:" << endl;
00113   
00114   cout << "Inser the fit mean:" << endl;
00115   cin >> mean;
00116 
00117   sigma = theSigma; //FIXME: estimate it!
00118 
00119   tBoxMax = hTBox->GetMaximum();
00120 
00121   // Define the fit range
00122   xFitMin = mean-5.*sigma;
00123   xFitMax = mean+5.*sigma;
00124 
00125   if(theVerbosityLevel >= 1) {
00126     cout << "      Time Box Rising edge: " << mean << endl;
00127     cout << "    = Seeds and range for fit:" << endl;
00128     cout << "       Seed mean = " << mean << endl;
00129     cout << "       Seed sigma = " << sigma << endl;
00130     cout << "       Fitting from = " << xFitMin << " to " << xFitMax << endl << endl;
00131   }
00132 }

void DTTimeBoxFitter::setFitSigma ( double  sigma  )  [inline]

Definition at line 58 of file DTTimeBoxFitter.h.

References theSigma.

Referenced by DTTTrigCalibration::DTTTrigCalibration(), and DTTTrigWriter::DTTTrigWriter().

00058                                  {
00059     theSigma = sigma;
00060   }

void DTTimeBoxFitter::setInteractiveFit ( bool  isInteractive  )  [inline]

Switch to interactive fit.

Definition at line 49 of file DTTimeBoxFitter.h.

References interactiveFit.

00049                                              {
00050     interactiveFit = isInteractive;
00051   }

void DTTimeBoxFitter::setRebinning ( int  reb  )  [inline]

Set the rebin.

Definition at line 54 of file DTTimeBoxFitter.h.

References rebin.

00054                              {
00055     rebin = reb;
00056   }

void DTTimeBoxFitter::setVerbosity ( unsigned int  lvl  )  [inline]

Set the verbosity of the output: 0 = silent, 1 = info, 2 = debug.

Definition at line 44 of file DTTimeBoxFitter.h.

References theVerbosityLevel.

Referenced by DTTTrigCalibration::DTTTrigCalibration(), and DTTTrigWriter::DTTTrigWriter().

00044                                       {
00045     theVerbosityLevel = lvl;
00046   }


Member Data Documentation

TFile* DTTimeBoxFitter::hDebugFile [private]

Definition at line 66 of file DTTimeBoxFitter.h.

Referenced by DTTimeBoxFitter(), getFitSeeds(), and ~DTTimeBoxFitter().

bool DTTimeBoxFitter::interactiveFit [private]

Definition at line 69 of file DTTimeBoxFitter.h.

Referenced by DTTimeBoxFitter(), fitTimeBox(), and setInteractiveFit().

int DTTimeBoxFitter::rebin [private]

Definition at line 70 of file DTTimeBoxFitter.h.

Referenced by DTTimeBoxFitter(), fitTimeBox(), and setRebinning().

double DTTimeBoxFitter::theSigma [private]

Definition at line 71 of file DTTimeBoxFitter.h.

Referenced by getFitSeeds(), getInteractiveFitSeeds(), and setFitSigma().

unsigned int DTTimeBoxFitter::theVerbosityLevel [private]

Definition at line 68 of file DTTimeBoxFitter.h.

Referenced by fitTimeBox(), getFitSeeds(), getInteractiveFitSeeds(), and setVerbosity().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:19:08 2009 for CMSSW by  doxygen 1.5.4