BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
//____________________________________________________________________
// 
// Module for ADC gain calibration of beam-beam counters 
// 

//____________________________________________________________________
//
// $Id: BrBbGainCalModule.cxx,v 1.8 2002/03/21 15:02:16 ouerdane Exp $
// $Author: ouerdane $
// $Date: 2002/03/21 15:02:16 $
// $Copyright: (C) 2001 BRAHMS Collaboration <brahmlib@rhic.bnl.gov>
//
#ifndef BRAT_BrBbGainCalModule
#include "BrBbGainCalModule.h"
#endif
#ifndef ROOT_TDirectory
#include "TDirectory.h"
#endif
#ifndef ROOT_TF1
#include "TF1.h"
#endif

#include "TH2.h"
#ifndef ROOT_TString
#include "TString.h"
#endif

#ifndef WIN_32
#include <iostream>
#include <iomanip>
#include <fstream>
#else
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#endif

#ifndef BRAT_BrDataTable
#include "BrDataTable.h"
#endif
#ifndef BRAT_BrBbDig
#include "BrBbDig.h"
#endif
#ifndef BRAT_BrRunInfoManager
#include "BrRunInfoManager.h"
#endif

//____________________________________________________________________
ClassImp(BrBbGainCalModule);

//____________________________________________________________________
 BrBbGainCalModule::BrBbGainCalModule()
{
  // Default constructor. DO NOT USE
  SetState(kSetup);
  SetDefaultParameters();
}

//____________________________________________________________________
 BrBbGainCalModule::BrBbGainCalModule(const Char_t* name, const Char_t* title)
  : BrBbCalModule(name, title)
{
  // Named Constructor
  SetState(kSetup);
  SetDefaultParameters();
}

//____________________________________________________________________
 void BrBbGainCalModule::SetDefaultParameters()
{
  // default parameters used for calibration
  SetNoPedWidth(); // default is 10
  SetFitWindow();  // default is 100 ADC chan
}

//____________________________________________________________________
 BrBbGainCalModule::~BrBbGainCalModule()
{
  // destructor
}

//____________________________________________________________________
 void BrBbGainCalModule::DefineHistograms()
{
  // Define histograms. They are:
  // <fill in here>

  if (GetState() != kInit) {
    Stop("DefineHistograms", "Must be called after Init"); 
    return;  
  }

  if (fLoadAscii || fCommitAscii) {
    if (Verbose() > 5)
      Warning("DefineHistograms", "No need to declare histos "
              "if you load or commit calibration from ascii file"); 
    return;  
  }

  TDirectory* saveDir = gDirectory; 
  fHistDir = gDirectory->mkdir(Form("%s_AdcGain", GetName())); 
  fHistDir->cd();
  
  // Make histograms here 
  fHistDir->mkdir("RawAdc");
  fHistDir->cd("RawAdc");
  fRawAdc = new TH1F* [fNoTubes];
  Float_t xMax = 4000.5;
  for(Int_t i = 0; i < fNoTubes; i++) {
    if (i+1 >= fBigT)
      xMax = 8000.5;
    fRawAdc[i] = new  TH1F(Form("rawAdc%02d",i+1), 
			   Form("rawAdc%02d", i+1), 250, 0.5, xMax);
  }
  
  fHistDir->mkdir("CalAdc");
  
  fHistDir->cd();
  
  // create summary histograms
  
  
  hGain0 =
    new TH1F(Form("%sGain0", GetName()),
	     Form("Gain Summary summary for %s", GetName()),
	     fNoTubes, 0.5, fNoTubes+.5);
  hGain0Width =
    new TH1F(Form("%sGain0Width", GetName()),
	     Form("Gain Summary summary for %s", GetName()),
	     fNoTubes, 0.5, fNoTubes+.5);
  
  hCalAdcVsTube =
    new TH2F(Form("%sAdcVsTube", GetName()),
	     Form("Normalized ADCs vs Tube %s", GetName()),
	     fNoTubes, 0.5, fNoTubes+.5,
	     40, 0.0, 10.0);
	     
  gDirectory = saveDir;
}

//____________________________________________________________________
 void BrBbGainCalModule::Init()
{
  // Job-level initialisation
  SetState(kInit);

  //
  // Called once per session
  // Get detectorParameters. Inform the parameterElementManeger about
  // database tables to be used (i.e. filled).

  if(DebugLevel() > 2)
    cout << "Entering BrBbGainCalModule::Init() for " 
	 << GetName() << endl;

  // initializing base class
  BrBbCalModule::Init();
  BrCalibration::EAccessMode mode = BrCalibration::kRead;

  if(fLoadAscii) {
    mode = BrCalibration::kTransitional;
    fCalibration->Use("adcGain0", mode, fNoTubes);
  }
  else {
    // check needed calibration, if already loaded:
    if (!fCalibration->GetAccessMode("pedestal")) {
      mode = BrCalibration::kRead;
      if (DebugLevel() > 3)
        Warning("Init", "Pedestals will be read from the sql DB");      
      fCalibration->Use("pedestal");
      fCalibration->Use("pedestalWidth");
    }

    // if we want to save them (ascii or DB)
    if (fSaveAscii || fCommitAscii) {
      mode = BrCalibration::kWrite;
      fCalibration->Use("adcGain0", mode, fNoTubes);
    }
  }
}

//____________________________________________________________________
 void BrBbGainCalModule::Begin()
{
  // Run-level initialisation
  SetState(kBegin);

  if(fLoadAscii){
    ReadAscii();    
    return;
  }
  
  if (fCommitAscii)
    return;
  
  // check if histos are booked
  if (!HistBooked()) {
    Abort("Begin", "MUST book histograms for calibration to work"); 
    return;
  }

  if (!fCalibration->RevisionExists("*")) {
    Abort("Begin", "No pedestal revision found...aborting");
    return;
  }

  for (Int_t t = 1; t <= fNoTubes; t++) {
    fCalibration->SetAdcGain0(t, BrBbCalibration::kCalException);
    if (!IsValid(fCalibration->GetPedestal(t)) || 
	!IsValid(fCalibration->GetPedestalWidth(t))) {
      fValidTube[t-1] = kFALSE;
      if (Verbose() > 2) 
	cout << " Tube " << t << " will not be calibrated (bad pedestal) " << endl;
    }
  } 
}

//____________________________________________________________________
 void BrBbGainCalModule::Event(BrEventNode* inNode, BrEventNode* outNode)
{
  // Per event method
  SetState(kEvent);

  if (fCommitAscii || fLoadAscii)
    return;

  if(DebugLevel() > 5)
    cout << "Entering Event() in BrBbGainCalModule for " 
	 << GetName() << endl;


  BrDataTable *hits = inNode->GetDataTable(fDigName);

  if (hits == 0) {
    if (DebugLevel() > 2)
      Warning("Event", "No hit table for %s", GetName());
    return;
  }
  
  Int_t nhit = hits->GetEntries();
  
  for(Int_t i = 0; i < nhit; i++) {
    BrBbDig* hit = (BrBbDig*)hits->At(i);
    Int_t tube = hit->GetTubeNo();
    if (!fValidTube[tube-1])
      continue;

    Float_t adc = hit->GetAdc() - fCalibration->GetPedestal(tube);
    Float_t wid = fCalibration->GetPedestalWidth(tube);
    Int_t j = tube - 1;
    if (hit->GetTdc() > 3800 || hit->GetTdc() < 20 || adc < wid*fNoPedWidth) {
      if (DebugLevel() > 10)
	cout << "Hit is not good for Gain" << endl;
      continue;
    }

    fRawAdc[tube-1]->Fill(adc);
  }
}

//____________________________________________________________________
 void BrBbGainCalModule::End()
{
  // Run-level finalisation
  SetState(kEnd);

}

//____________________________________________________________________
 void BrBbGainCalModule::Finish()
{
  // Job-level finalisation
  SetState(kFinish);
  
  if(fLoadAscii)
    return;
  
  if (fCommitAscii) {
    ReadAscii();
    return;
  }
  
  // prepare histos containing results
  TDirectory* saveDir = gDirectory;
  fHistDir->cd("CalAdc");
  
  cout << " Start fitting procedure for " << GetName()
       << " with simple gaussian..."
       << endl;
  
  if (Verbose() > 2)
    cout << " Tube   |   Mean   |   Sigma " << endl;

  
  TF1* fit = new TF1("fit", "gaus", 0, 10000);
  
  for(Int_t i = 0; i < fNoTubes ; i++){
    if (!fValidTube[i])
      continue;
    if (fRawAdc[i]->GetEntries() == 0)
      continue;

    Int_t tube = i + 1;
    
    // fit tube gain with gaussian 
    Int_t binAtMax  = fRawAdc[i]->GetMaximumBin(); // bin number at max
    Axis_t adcAtMax = fRawAdc[i]->GetBinCenter(binAtMax);
    
    if (DebugLevel() > 3) {
      cout << " Max Bin at " << setw(6)  << binAtMax 
	   << " Value: "     << adcAtMax << endl;
    }
    
    fit->SetParameters(fRawAdc[i]->GetMaximum(), adcAtMax, fFitWindow);
    fRawAdc[i]->Fit("fit", "Q0", "", 
		    adcAtMax - fFitWindow, adcAtMax + fFitWindow);
    
    Double_t meanFit  = fit->GetParameter(1);    
    Double_t sigmaFit = fit->GetParameter(2);

    if (Verbose() > 2)
      cout << setw(8)  << tube << " " 
	   << setw(14) << setprecision(4) << meanFit << " "
	   << setw(9)  << setprecision(4) << sigmaFit << endl;
    
    // set parameters for eventual commit into DB
    //

    fCalibration->SetAdcGain0(tube, meanFit);
    hGain0->Fill(tube, meanFit);
    hGain0Width->Fill(tube, sigmaFit);

    if (IsValid(fCalibration->GetAdcGain0(tube)))
      NormalizeHisto(fRawAdc[i], tube);
  }
  
  fCalibration->SetComment("adcGain0", fComment.Data());
  
  gDirectory = saveDir;

  if (fSaveAscii)
    SaveAscii();

}

//____________________________________________________________________
 void BrBbGainCalModule::NormalizeHisto(TH1F* h, Int_t tube)
{
  // take result of calib. and build histogram with calibrated ADC
  
  Int_t minBin = h->GetXaxis()->GetFirst();
  Int_t maxBin = h->GetXaxis()->GetLast();
  Axis_t xmin  = h->GetXaxis()->GetXmin()/fCalibration->GetAdcGain0(tube);  
  Axis_t xmax  = h->GetXaxis()->GetXmax()/fCalibration->GetAdcGain0(tube);
  Float_t factor = (xmax - xmin)/Float_t(h->GetNbinsX());
  
  TH1F* cal = new TH1F(Form("calAdc%02d", tube),
		       Form("Calibrated Adc, tube %d", tube),
		       maxBin - minBin, xmin, xmax);
  
  for (Int_t i = minBin; i <= maxBin; i++) {
    Axis_t x = i*factor;
    cal->Fill(x, h->GetBinContent(i));
    // Have to be more clever here
    hCalAdcVsTube->Fill(tube, x, h->GetBinContent(i));
  }

}

//____________________________________________________________________
 void BrBbGainCalModule::SaveAscii() 
{

  // save pedestal to ascii file

  BrRunInfoManager* runMan = BrRunInfoManager::Instance();
  Int_t* runs = runMan->GetRunNumbers();
  Int_t  nrun = runMan->GetNumberOfRuns();
  
  BrBbCalModule::SaveAscii();

  ofstream file(fCalibFile.Data(), ios::out);
  
  file << "****************************************** " << endl;
  file << "*  Calibration for BB counter " << GetName() << endl;
  file << "*     ADC Gain 0" << endl;
  file << "*     Used events from run(s) ";
  for (Int_t r = 0; r < nrun; r++) file << runs[r] << " ";
  file << endl;
  file << "****************************************** " <<endl;
  file << "*" << endl; 
  file << "* tube  |  ADC Gain 0   " << endl;   
  file << "*  ---------------------" << endl << endl;
  
  
  for (Int_t i = 0; i < fNoTubes; i++) {
    Int_t tube = i + 1;
    file << setw(7) << tube << setw(12) 
	 << fCalibration->GetAdcGain0(tube) << setw(12)
	 << endl;
  }
  
  file << "* ------------------------------------" << endl << endl;
}

//____________________________________________________________________
 void BrBbGainCalModule::ReadAscii() 
{

  // save pedestal to ascii file
  BrBbCalModule::SaveAscii();

  ifstream file(fCalibFile.Data(), ios::in);

  if (!file) {
    Abort("ReadAscii", "File %s was not found", fCalibFile.Data());
    return;
  }

  Float_t ag0;
  Int_t tube;
  Char_t comment[256];
  
  file.getline(comment, 256);
  while(comment[0] == '*') 
    file.getline(comment, 256);

  for (Int_t i = 1; i <= fNoTubes; i++) {
    file >> tube >> ag0;
    if (DebugLevel() > 5) {
      cout << setw(4) << tube 
	   << setw(12) << ag0 << endl; 
    }
    
    fCalibration->SetAdcGain0(tube, ag0);
  }

  fCalibration->SetComment("adcGain0","Generated by "
			   "BrBbGainCalModule: fit with a gaussian function");

}


//____________________________________________________________________
 void BrBbGainCalModule::Print(Option_t* option) const
{
  // Print module information
  // See BrModule::Print for options.
  // In addition this module defines the Option:
  // <fill in here>
  
  TString opt(option);
  opt.ToLower(); 
  
  BrModule::Print(option); 
  if (opt.Contains("d")) 
    cout << endl 
         << "  Original author: Flemming Videbaek" << endl
         << "  Last Modifications: " << endl 
         << "    $Author: ouerdane $" << endl  
         << "    $Date: 2002/03/21 15:02:16 $"   << endl 
         << "    $Revision: 1.8 $ " << endl  
         << endl 
         << "-------------------------------------------------" << endl;
}

//____________________________________________________________________
//
// $Log: BrBbGainCalModule.cxx,v $
// Revision 1.8  2002/03/21 15:02:16  ouerdane
// added fComment member to base class module and method SetComment so that the user can set comments about the calibration at commit time
//
// Revision 1.7  2002/02/14 21:40:48  videbaek
// Add some more diagnostic histograms
//
// Revision 1.6  2001/10/23 20:47:44  ouerdane
// Updated calibration modules for more efficiency, details are mainly technical
//
// Revision 1.5  2001/10/16 01:42:42  ouerdane
// Small change
//
// Revision 1.4  2001/10/15 00:35:07  ouerdane
// Updated all Beam-Beam counter calibration modules (like I did for the TOF
// some weeks ago):
//
// Common changes:
//  added methods Set[Save,Commit,Load]Ascii(Bool_t)
//  removed methods Set[ReadFrom,LoadFrom,SaveTo]File
//
// BrBbGainCalModule:
//  cleaned up a lot the code, added diagnostic histograms showing calibrated
//  ADC after the 1st MIP peak was found.
//  Still at a stage where the 1st MIP peak is the only one to be checked.
//  Later on, will add algorithm to find other peaks.
//
// Added BrBbSlewingCalModule in Makefile.am, etc
// The fit function introduced is dT = a + b/sqrt(dE) + c/dE (where dE = ADC/Gain0)
//
// Revision 1.3  2001/09/24 14:01:02  videbaek
// fix last problems from yesterday
//
// Revision 1.2  2001/09/24 09:46:18  ouerdane
// Fixed some errors in method names:
//   - changed Set[Min,Max]Ped to Set[Min,Max]Adc
//   - changed fCalibration->[Get,Set]Gain0 to [Get,Set]AdcGain0
//
// Revision 1.1  2001/09/24 09:27:28  ouerdane
// Added BrBbGainCalModule with right file names (Flemming's mistake)
//
// Revision 1.1  2001/09/23 21:02:38  videbaek
// Forgot to include - thanks to Djamel
//
//

This page automatically generated by script docBrat by Christian Holm

Copyright ; 2002 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
Last Update on by

Validate HTML
Validate CSS