BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
//____________________________________________________________________
// 
// Class BrTd1AdcGainCalModule
//
// Calibration of adc gains. Moduleis copied largely following the
// BrTofAdcGainCalModule and the BrBbAdcGainCalModule.
// The modules though has to take into account the gap values ,
// and has a (better?) method for picking up the first peak.
// Note this is almost like the AdcGAinModule for the tof stuff
// and the 'small' changes needed could probably be incorporated
// into those - but for now keep as is.
//
// 
//
// 
//____________________________________________________________________
//
// $Id: BrTd1AdcGainCalModule.cxx,v 1.4 2002/04/23 15:38:48 videbaek Exp $
// $Author: videbaek $
// $Date: 2002/04/23 15:38:48 $
// $Copyright: (C) 2001 BRAHMS Collaboration <brahmlib@rhic.bnl.gov>
//

#ifndef WIN32
#include <iostream>
#include <iomanip>
#include <fstream>
#else
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#endif
#ifndef BRAT_BrTd1AdcGainCalModule
#include "BrTd1AdcGainCalModule.h"
#endif
#ifndef ROOT_TF1
#include "TF1.h"
#endif
#ifndef ROOT_TH1
#include "TH1.h"
#endif
#ifndef ROOT_TH2
#include "TH2.h"
#endif
#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef BRAT_BrEventNode
#include "BrEventNode.h"
#endif
#ifndef BRAT_BrDataTable
#include "BrDataTable.h"
#endif
#ifndef BRAT_BrTofDig
#include "BrTofDig.h"
#endif
#ifndef BRAT_BrRunInfoManager
#include "BrRunInfoManager.h"
#endif
#ifndef BRAT_BrCalibration
#include "BrCalibration.h"
#endif

//____________________________________________________________________
ClassImp(BrTd1AdcGainCalModule);

//____________________________________________________________________
 BrTd1AdcGainCalModule::BrTd1AdcGainCalModule()
  : BrTofCalModule()
{
  // Default constructor. DO NOT USE
  SetState(kSetup);

  SetDefaultParameters();
}

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

//____________________________________________________________________
 void BrTd1AdcGainCalModule::SetDefaultParameters()
{
  SetMinGain(600);  
  SetMaxGain(5000);  
  SetFitStart(); 
  SetFitStop();
  SetAdcCut(300);
  UseGap(kTRUE);
}

//____________________________________________________________________
 void BrTd1AdcGainCalModule::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 "
              "since we only load calibration from ascii file"); 
    return;  
  }

  const Int_t nSlats = fParamsTof->GetNoSlats();

  TDirectory* saveDir = gDirectory; 

  fHistDir = gDirectory->mkdir(Form("%sAdcGain",GetName())); 
  fHistDir->cd();
  
  // Make histograms here
  fHistDir->mkdir("Adc_Bot");
  fHistDir->mkdir("Adc_Top");
  fHistDir->mkdir("Cal_Bot");
  fHistDir->mkdir("Cal_Top");
  
  // histos for calib:
  fHistDir->cd("Adc_Top");
  
  fTopAdc = new TH1F* [nSlats];
  for (Int_t i = 1; i <= nSlats; i++) 
    fTopAdc[i-1] = new TH1F(Form("top%03d", i), "", 400, 0.5, 16000.5);
  
  fHistDir->cd("Adc_Bot");
  
  fBotAdc = new TH1F* [nSlats];
  for (Int_t i = 1; i <= nSlats; i++) 
    fBotAdc[i-1] = new TH1F(Form("bot%03d", i), "", 400, 0.5, 16000.5);
  
  
  fHistDir->cd();
  fTSum = new TH1F("tgains", 
		   Form("%s top tubes: ADC Gains", GetName()),
		   nSlats, 0.5, nSlats + 0.5); 
  fBSum = new TH1F("bgains", 
		   Form("%s bottom tubes: ADC Gains", GetName()),
		   nSlats, 0.5, nSlats + 0.5); 

  gDirectory = saveDir;
}

//____________________________________________________________________
 void BrTd1AdcGainCalModule::Init()
{
  // Job-level initialisation
  SetState(kInit);
  
  //---------------
  // base class initialization (register calibration parameters)
  BrTofCalModule::Init();
  
  Int_t nSlats = fParamsTof->GetNoSlats();
  BrCalibration::EAccessMode mode = BrCalibration::kRead;
  
  // check if we want to load adc gain cal from ascii file
  if (fLoadAscii) {
    mode = BrCalibration::kTransitional;
    fCalibration->Use("topAdcGain", mode, nSlats);
    fCalibration->Use("botAdcGain", mode, nSlats);
  }
  
  else {
    // check needed calibration, if already loaded 
    // if not, try to get them from DB
    if (!fCalibration->GetAccessMode("topPedestal")  ||
	!fCalibration->GetAccessMode("botPedestal")) {
      mode = BrCalibration::kRead;
      if (DebugLevel() > 3)
	Warning("Init", "Pedestals will be read from the SQL database");
      fCalibration->Use("topPedestal", mode, nSlats);
      fCalibration->Use("botPedestal", mode, nSlats);
      fCalibration->Use("topPedestalWidth", mode, nSlats);
      fCalibration->Use("botPedestalWidth", mode, nSlats);
      if(fUseGap){
	fCalibration->Use("topGap", mode, nSlats);
	fCalibration->Use("topGapStart", mode, nSlats);
	fCalibration->Use("botGap", mode, nSlats);
	fCalibration->Use("botGapStart", mode, nSlats);
      }
    }
    
    // if we want to save them (ascii or DB)
    if (fSaveAscii || fCommitAscii) {
      mode = BrCalibration::kWrite;
      fCalibration->Use("topAdcGain", mode, nSlats);
      fCalibration->Use("botAdcGain", mode, nSlats);
      if(!fUseGap){
	fCalibration->Use("topGap", mode, nSlats);
	fCalibration->Use("topGapStart", mode, nSlats);
	fCalibration->Use("botGap", mode, nSlats);
	fCalibration->Use("botGapStart", mode, nSlats);
      }
    }
  }
}


//____________________________________________________________________
 void BrTd1AdcGainCalModule::Begin()
{
  // Run-level initialisation
  SetState(kBegin);
  
  if (fLoadAscii) {
    ReadAscii();
    return;
  }
  
  if (!HistBooked())
    if (!fCommitAscii) {
      Abort("Begin", "Need histos for calibration");
      return;
    }
  
  // check if we got parameter revisions:
  if (!fCalibration->RevisionExists("*")) {
    Abort("Begin", "Some calibration revision are missing!");
    return;
  }
  
  for (Int_t s = 1; s <= fParamsTof->GetNoSlats(); s++) {
    fCalibration->SetTopAdcGain(s, BrTofCalibration::kCalException);
    fCalibration->SetBotAdcGain(s, BrTofCalibration::kCalException);
  }
}

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

  if (fCommitAscii || fLoadAscii)
    return;
  
  
  BrDataTable* hits = inNode->GetDataTable(Form("DigTof %s", GetName()));
  
  if (!hits) {
    if (Verbose() > 3) Warning("Event", Form("No DigTof %s - table", GetName()));
    return;
  }


  
  Int_t nhit = hits->GetEntries();

  for(Int_t i = 0; i < nhit; i++) {
    BrTofDig* hit = (BrTofDig*)hits->At(i);

    Int_t   slat  = hit->GetSlatno();
    Float_t tped  = fCalibration->GetTopPedestal(slat);
    Float_t bped  = fCalibration->GetBotPedestal(slat);
    Float_t tgap  = fCalibration->GetTopAdcGap(slat);
    Float_t tgapst  = fCalibration->GetTopAdcGapStart(slat);
    Float_t bgap    = fCalibration->GetBotAdcGap(slat);
    Float_t bgapst  = fCalibration->GetBotAdcGapStart(slat);



    if (!IsValid(tped) || !IsValid(bped))
      fValidSlat[slat - 1] = kFALSE;
    else
      fValidSlat[slat - 1] = kTRUE;
    
    if (!fValidSlat[slat - 1])
      continue;
    
    // ------- fill calibration histograms here:
    Float_t adcup = hit->GetAdcUp()   - tped;
    Float_t adcdown = hit->GetAdcDown()   - bped;

    if(fUseGap){
      // correct for gaps
      if(adcup > tgapst)
	adcup -= tgap;
      if(adcdown > bgapst)
	adcdown -= bgap;
    }

    if(adcup>fAdcCut)
      fTopAdc[slat-1]->Fill(adcup);
    if(adcdown>fAdcCut)
      fBotAdc[slat-1]->Fill(adcdown);
  }
}

//____________________________________________________________________
 void BrTd1AdcGainCalModule::Finish()
{
  // Job-level finalisation
  SetState(kFinish);

  // if load ascii mode
  if (fLoadAscii) 
    return;
  
  // if commit mode
  if (fCommitAscii) {
    ReadAscii();
    return;
  }
  
  // --------------------------------------------------------------------
  // fit MIP peak with a gaus from peak - fFitStart to peak + fFitStop
  // --------------------------------------------------------------------

  Double_t topGain[fParamsTof->GetNoSlats()];
  Double_t botGain[fParamsTof->GetNoSlats()];
  
  Axis_t peak = 0;
  
  for (Int_t s = 1; s <= fParamsTof->GetNoSlats(); s++) {
    if (!fValidSlat[s - 1]) {
      topGain[s - 1] = BrTofCalibration::kCalException;
      continue;
    }
    
    //
    // The range is set to find peak in expected range
    // This only works for the higher root numbers as written. It cna be done
    // in old methods by multiple calls, but this is simple.
#if ROOT_VERSION_CODE>=ROOT_VERSION(3,2,6)
    fTopAdc[s-1]->GetXaxis()->SetRangeUser(fMinGain, fMaxGain);    
#endif
    peak = fTopAdc[s-1]->GetMaximumBin();
    peak = fTopAdc[s-1]->GetBinCenter(peak);
#if ROOT_VERSION_CODE>=ROOT_VERSION(3,2,6)
    fTopAdc[s-1]->GetXaxis()->SetRange(1, fBotAdc[s-1]->GetNbinsX());
#endif

    fTopAdc[s-1]->Fit("gaus", "Q0", "", peak - fFitStart, peak + fFitStop);
    topGain[s - 1] = ((TF1*)fTopAdc[s-1]->GetListOfFunctions()->At(0))->
      GetParameter(1);
    
    // check width and remove bad fits
    if (topGain[s - 1] < fMinGain || topGain[s - 1] > fMaxGain)
      topGain[s - 1] = BrTofCalibration::kCalException;
  }
  

  // -------------- bottom tubes

  for (Int_t s = 1; s <= fParamsTof->GetNoSlats(); s++) {
    if (!fValidSlat[s - 1]) {
      botGain[s - 1] = BrTofCalibration::kCalException;
      continue;
    }
#if ROOT_VERSION_CODE>=ROOT_VERSION(3,2,6)
    fBotAdc[s-1]->GetXaxis()->SetRangeUser(fMinGain, fMaxGain);    
#endif
    peak = fBotAdc[s-1]->GetMaximumBin();
    peak = fBotAdc[s-1]->GetBinCenter(peak);
#if ROOT_VERSION_CODE>=ROOT_VERSION(3,2,6)
    fBotAdc[s-1]->GetXaxis()->SetRange(1, fBotAdc[s-1]->GetNbinsX());
#endif    
    fBotAdc[s-1]->Fit("gaus", "Q0", "", peak - fFitStart, peak + fFitStop);
    
    botGain[s - 1] = ((TF1*)fBotAdc[s-1]->GetListOfFunctions()->At(0))->
      GetParameter(1);
    
    // check width and remove bad fits
    if (botGain[s - 1] < fMinGain || botGain[s - 1] > fMaxGain)
      botGain[s - 1] = BrTofCalibration::kCalException;
    
  }
  

  // fill histo and calibration parameters
  for (Int_t s = 1; s <= fParamsTof->GetNoSlats(); s++) {
    // set offset in calibration parameter element
    fCalibration->SetTopAdcGain(s, topGain[s - 1]);
    fCalibration->SetBotAdcGain(s, botGain[s - 1]);
    
    // fill summaries
    fTSum ->Fill(s, topGain[s - 1]);
    fBSum ->Fill(s, botGain[s - 1]);
    
    if (Verbose() > 2) 
      cout << " Slat " << s 
	   << " : top -> " << topGain[s - 1] 
	   << "   bot -> " << botGain[s - 1] << endl;
  }
  
  // check results by normalizing the histos by the gain 
  NormalizeHistos();

  // save calibration to ascii file
  if (fSaveAscii)
    SaveAscii();
}

//____________________________________________________________________
 void BrTd1AdcGainCalModule::NormalizeHistos()
{
  // private

  // --------------
  // check results
  // --------------

  TDirectory* saveDir = gDirectory;
  
  // bot tubes
  fHistDir->cd("Cal_Bot");

  for (Int_t s = 1; s <= fParamsTof->GetNoSlats(); s++) {
    if (!IsValid(fCalibration->GetBotAdcGain(s)))
      continue;
    
    Int_t minBin = fBotAdc[s-1]->GetXaxis()->GetFirst();
    Int_t maxBin = fBotAdc[s-1]->GetXaxis()->GetLast();
    Axis_t xmin  = fBotAdc[s-1]->GetXaxis()->GetXmin()/fCalibration->GetBotAdcGain(s);  
    Axis_t xmax  = fBotAdc[s-1]->GetXaxis()->GetXmax()/fCalibration->GetBotAdcGain(s);  
    Float_t factor = (xmax - xmin)/Float_t(fBotAdc[s-1]->GetNbinsX());
    
    TH1F* h = new TH1F(Form("calBot%03d", s),
		       Form("Calibrated Adc, bot slat %d", s),
		       maxBin - minBin, xmin, xmax);

    for (Int_t i = minBin; i <= maxBin; i++) {
      Axis_t x = i*factor;
      h->Fill(x, fBotAdc[s-1]->GetBinContent(i));
    }
  }
  // top tubes
  fHistDir->cd("Cal_Top");
  
  for (Int_t s = 1; s <= fParamsTof->GetNoSlats(); s++) {
    if (!IsValid(fCalibration->GetTopAdcGain(s)))
      continue;
    
    Int_t minBin = fTopAdc[s-1]->GetXaxis()->GetFirst();
    Int_t maxBin = fTopAdc[s-1]->GetXaxis()->GetLast();
    Axis_t xmin  = fTopAdc[s-1]->GetXaxis()->GetXmin()/fCalibration->GetTopAdcGain(s);  
    Axis_t xmax  = fTopAdc[s-1]->GetXaxis()->GetXmax()/fCalibration->GetTopAdcGain(s);
    Float_t factor = (xmax - xmin)/Float_t(fTopAdc[s-1]->GetNbinsX());  
    
    TH1F* h = new TH1F(Form("calTop%03d", s),
		       Form("Calibrated Adc, top slat %d", s),
		       maxBin - minBin, xmin, xmax);
    
    for (Int_t i = minBin; i <= maxBin; i++) {
      Axis_t x = i*factor;
      h->Fill(x, fTopAdc[s-1]->GetBinContent(i));
    }
  }
}

//____________________________________________________________________
 void BrTd1AdcGainCalModule::SaveAscii() 
{

  // save pedestal to ascii file
  
  BrRunInfoManager* runMan = BrRunInfoManager::Instance();
  Int_t* runs = runMan->GetRunNumbers();
  Int_t  nrun = runMan->GetNumberOfRuns();
  
  BrTofCalModule::SaveAscii();
  
  ofstream file(fCalibFile.Data(), ios::out);
  
  file << "****************************************** " << endl;
  file << "*  Calibration for Tof detector " << GetName() << endl;
  file << "*  Adc Gain calibration         " << 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 << "* slat  |     top    |    bot     " << endl;
  file << "* --------------------------------" << endl << endl;
  
  for (Int_t i = 0; i < fParamsTof->GetNoSlats(); i++) {
    Int_t slat = i + 1;
    file << setw(4) << slat 
	 << setw(15) << fCalibration->GetTopAdcGain(slat) 
	 << setw(15) << fCalibration->GetBotAdcGain(slat) << endl;
  }
  
  file << "* ------------------------------------" << endl << endl;
  
}


//____________________________________________________________________
 void BrTd1AdcGainCalModule::ReadAscii() 
{
  // read calibration from file created by this module

  BrTofCalModule::ReadAscii();
  
  ifstream file(fCalibFile.Data(), ios::in);
  
  if (!file) {
    Stop("ReadAscii", "File %s was not found", fCalibFile.Data());
    return;
  }
  
  Float_t top, bot;
  Int_t slat;
  Char_t comment[256];
  
  file.getline(comment, 256);
  while(comment[0] == '*') {
    file.getline(comment, 256);
    if (DebugLevel() > 5)
      cout << comment << endl;
  } 

  for (Int_t i = 1; i <= fParamsTof->GetNoSlats(); i++) {
    file >> slat >> top >> bot;
    if (DebugLevel() > 5) 
      cout << setw(4)  << slat 
	   << setw(12) << top 
	   << setw(12) << bot << endl;
        
    fCalibration->SetTopAdcGain(slat, top);
    fCalibration->SetBotAdcGain(slat, bot);
  }

  fCalibration->SetComment("topAdcGain",
			   "Generated by BrTd1AdcGainCalModule: "
			   "fit sharp peak with a simple landau");
  fCalibration->SetComment("botAdcGain",
			   "Generated by BrTd1AdcGainCalModule: "
			   "fit sharp peak with a simple landau");

}

//____________________________________________________________________
 void BrTd1AdcGainCalModule::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: Flemmin Videbaek" << endl
         << "  Last Modifications: " << endl 
         << "    $Author: videbaek $" << endl  
         << "    $Date: 2002/04/23 15:38:48 $"   << endl 
         << "    $Revision: 1.4 $ " << endl  
         << endl 
         << "-------------------------------------------------" << endl;
}

//____________________________________________________________________
//
// $Log: BrTd1AdcGainCalModule.cxx,v $
// Revision 1.4  2002/04/23 15:38:48  videbaek
// Added useage of Gap in adc as well as changing histogram range.
//
// Revision 1.3  2002/03/20 19:32:17  videbaek
// Introduced gap parameters taken from the calibration DB rather than coded in
// line.
//
// Revision 1.2  2002/02/24 20:33:34  videbaek
// Add root version guards around the setting of the range.
// The code will compile for a lower version, but most likely not
// work very well.
//
// Revision 1.1  2002/02/15 21:21:32  videbaek
// Added new directory for td1 calibration.
// Even though they are of TofDig type, this could not easily be incorporated into
// the tof code. Thus a new set of similar code.
//

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