// Author: Andrew M. Rogers, NSCL 07/01/2006
//* Copyright (C) 2006-2008 Andrew M. Rogers

#include <fstream>

#include <TCanvas.h>
#include <TTree.h>
#include <TMath.h>
#include <TObject.h>

#include "TScaler.h"

ClassImp(TScaler);
//ClassImp(TScalerCh);

////////////////////////////////////////////////////////////////////////////////
/* BEGIN_HTML
<p> TScaler
</p>
END_HTML */
////////////////////////////////////////////////////////////////////////////////


TClonesArray *TScaler::fgScalers = 0;

//______________________________________________________________________________
void TScaler::Clear(Option_t *)
{
  // -- Clears all relavent TScaler data members.
  //

  startTime = 0;
  endTime   = 0;

  memset(scaler,'\0',sizeof(scaler));
  //  for(Int_t i=0; i<TSCALER_NSCALERS; i++){
  //    fScalers[i]->Clear();
  //  }
  //  fScalers->Clear();
  TIter next(fScalers);
  TScalerCh *psc;
  while((psc = (TScalerCh*)next.Next())) psc->Clear();
}


//______________________________________________________________________________
void TScaler::DrawTotals()
{
  // -- Draw a histogram showing all scaler totals.
  //

//   TCanvas *c_tot = new TCanvas("c_tot","Scaler Totals Canvas",1000,500);
//   b_h_totals->GetEntry(fChain->GetEntries()-1);
//   h_totals.SetFillColor(kRed);
//   h_totals.Draw();

}

//______________________________________________________________________________
void TScaler::DumpMap()
{
  // -- Dump the scaler mapping to stdout.
  //

  printf("****************************SCALER DUMP MAP***********************************\n");
  Char_t objName[500];
  for(Int_t i=0; i<TSCALER_NSCALERS; i++){
    TScalerCh *pScaler = (TScalerCh*)fScalers->At(i);
    sprintf(objName,"scaler%02i",i);
    //    printf("* %s \t%-20.20s \t%s\n",objName,fScalers[i]->GetName(),fScalers[i]->GetTitle());
    printf("* %s \t%-20.20s \t%s\n",objName,pScaler->GetName(),pScaler->GetTitle());
  }

  printf("*............................................................................*\n");
}


//______________________________________________________________________________
Bool_t TScaler::DumpScalers(Option_t *fileName,Option_t *option)
{
  // -- Dump the scaler values and related quantities to stdout and, optionally, a file.
  //

  if(!b_fScalers){ printf("ERROR:  Branch not set.\n"); return kFALSE;}
  Char_t *outFileName = (Char_t*)fileName;
  Char_t *modeOption  = (Char_t*)option;

  Char_t objName[500];
  Char_t outPut[2000];
  ofstream outFile;
  if(strcmp(outFileName,"NONE")!=0){
    if(strcmp(modeOption,"RECREATE")==0)    outFile.open(outFileName,ios::out);
    else if(strcmp(modeOption,"UPDATE")==0) outFile.open(outFileName,ios::app);
    else return kFALSE;

    cout << "Creating File:  " << outFileName << endl;
    if(!outFile.is_open()) cout << "File Creation FAILED!!!" << endl;
    sprintf(outPut,"* ScalerObj \tScaler Name         \t       Total \tAvg Rate (Actual)  sigma (Actual)  Avg Rate\n");
    outFile << outPut;
    sprintf(outPut,"*.................................................................................................*\n");
    outFile << outPut;
  }

  printf("******************************SCALER DUMP*************************************\n");

  // Calculate rate.
  Double_t avgRate[TSCALER_NSCALERS];
  Double_t sigma[TSCALER_NSCALERS];
  Double_t rate[TSCALER_NSCALERS];
  Double_t vari[TSCALER_NSCALERS];
  memset(avgRate,'\0',sizeof(avgRate));
  memset(sigma,'\0',sizeof(sigma));
  memset(rate,'\0',sizeof(rate));
  memset(vari,'\0',sizeof(vari));
  Long64_t nEntries = b_fScalers->GetEntries()-1;
  //Calculate average rate.
  for(Int_t i=0; i<nEntries; i++){
    b_fScalers->GetEntry(i);
    for(Int_t j=0; j<TSCALER_NSCALERS; j++){
      TScalerCh *pScaler = (TScalerCh*)fScalers->At(j);
      rate[j] += pScaler->rate/(Double_t)nEntries;
      //      if(j==0)   cout << rate[j] << endl;
    }
  }
  for(Int_t m=0; m<TSCALER_NSCALERS; m++) {avgRate[m] = rate[m]; 
    //    cout << avgRate[m] << " " << rate[m] << " " << nEntries <<endl;
  }
  // Calculate sigma.
  Double_t sum2[TSCALER_NSCALERS];
  Double_t sumc[TSCALER_NSCALERS];
  memset(sum2,'\0',sizeof(sum2));
  memset(sumc,'\0',sizeof(sumc));
  for(Int_t i=0; i<nEntries; i++){
    b_fScalers->GetEntry(i);
    for(Int_t j=0; j<TSCALER_NSCALERS; j++){
      TScalerCh *pScaler = (TScalerCh*)fScalers->At(j);
      sum2[j] = sum2[j] + (pScaler->rate - avgRate[j])*(pScaler->rate - avgRate[j]);
      sumc[j] = sumc[j] + (pScaler->rate - avgRate[j]);
    }
  }
  for(Int_t m=0; m<TSCALER_NSCALERS; m++) {
    vari[m]  = (sum2[m] - (sumc[m]*sumc[m])/(Double_t)nEntries)/((Double_t)nEntries - 1);
    sigma[m] = TMath::Sqrt(vari[m]);
  }


  b_h_totals->GetEntry(b_fScalers->GetEntries()-1);
  TNamed *nTmp = (TNamed*)fEChain->GetUserInfo()->FindObject("Elapsed Run Time");
  Double_t eTime = atof(nTmp->GetTitle());
  printf("ScalerObj \tScaler Name         \t       Total \tAvg Rate (Actual)  sigma (Actual)    Avg Rate\n");
  printf("*.................................................................................................*\n");
  for(Int_t i=0; i<TSCALER_NSCALERS; i++){
    TScalerCh *pScaler = (TScalerCh*)fScalers->At(i);
    sprintf(objName,"scaler%02i",i);
    Int_t tot = (Int_t)h_totals.GetBinContent(i+1);
    printf("* %s \t%-20.20s \t%12i \t%9.2lf \t%9.2lf \t%9.2lf\n",objName,pScaler->GetName(),
    	   tot,avgRate[i],sigma[i],(Double_t)tot/eTime);

    if(strcmp(outFileName,"NONE")!=0){
      sprintf(outPut,"%s \t%-20.20s \t%12i \t%9.2lf \t%9.2lf \t%9.2lf\n",objName,pScaler->GetName(),
    	   tot,avgRate[i],sigma[i],(Double_t)tot/eTime);
      outFile << outPut;
    }
  }

  if(strcmp(outFileName,"NONE")!=0) outFile.close();

  printf("*................................................................................................*\n");

  return kTRUE;
}


//______________________________________________________________________________
void TScaler::InitBranch(TTree *tree)
{
  // -- Initialize the TScaler branches on the tree.
  // Initializes or creates all the branches and member branches for the TScaler
  // class on the tree.  This is typically called after instanciating TTree and
  // TScaler.  The branch structure will then be generated on the TTree.

}


//______________________________________________________________________________
Bool_t TScaler::InitClass()
{
  // -- Initialze the defaults for the TScaler class.
  //

  SetNameTitle("scalers","Scalers");
  h_totals.SetNameTitle("h_totals","Scaler Totals");
  h_totals.SetBins(TSCALER_NSCALERS,0,TSCALER_NSCALERS);
  h_totals.Reset();
  h_totals.SetFillColor(kRed);

  fgScalers = new TClonesArray("TScalerCh",64);
  fScalers  = fgScalers;
  TClonesArray &fSc = *fgScalers;
  
  Char_t chain[500];
  for(Int_t i=0; i<TSCALER_NSCALERS; i++){
    sprintf(chain,"scaler%02i",i);

    h_totals.GetXaxis()->SetBinLabel(i+1,chain);
    new(fSc[i]) TScalerCh(chain,chain);
    scaler[i]   = 0;
  }    

  Clear();

  return kTRUE;
}


//______________________________________________________________________________
Int_t TScaler::InitTree(TTree *tree)
{
  // -- Initialize the scaler TTree and set the branch addresses.
  //

  if(!tree){
    printf("ERROR:  Tree could not be initialized.\n");
    return -1;
  }
  fEChain = tree;
  fChain   = (TTree*)fEChain->GetDirectory()->FindObjectAny("scalerT");
  if(!fChain){
    printf("ERROR:  Could not find scaler tree.\n");
    return -2;
  }

  fCurrent = -1;
  
  TScaler *sc = this;
  fChain->SetBranchAddress("scalers",&sc);

  b_startTime = fChain->GetBranch("startTime");
  b_endTime   = fChain->GetBranch("endTime");
  b_h_totals  = fChain->GetBranch("h_totals");
  b_fScalers  = fChain->GetBranch("fScalers");

  return 1;
}


//______________________________________________________________________________
Bool_t TScaler::LoadScalerMap(Char_t *fileName)
{
  // -- Load the mapping file.
  //

  Int_t scalerCh;
  Char_t scalerName[500],
    scalerTitle[500];

  ifstream file(fileName,ios::in);
  if(!file.is_open()) {printf("File could not be opened!!!\n"); return kFALSE;}

  Char_t line[1000];

  file.getline(line,1000);
  while(!isdigit(line[0])){
    file.getline(line,1000);
  }

  while(!file.eof()){
    sscanf(line,"%i %s %s",&scalerCh,scalerName,scalerTitle);
    #ifdef TSCALER_DEBUG
    printf("%02i \t%-20.20s \t%s\n",scalerCh,scalerName,scalerTitle);
    #endif
    if(scalerCh>TSCALER_NSCALERS){ 
      printf("scalerCh %i is greater that TSCALER_NSCALERS.\n",scalerCh);
      return kFALSE;
    }
    TScalerCh *pScaler = (TScalerCh*)fScalers->At(scalerCh);
    pScaler->SetNameTitle(scalerName,scalerTitle);
    h_totals.GetXaxis()->SetBinLabel(scalerCh+1,scalerName);
    if(!file.eof()) file.getline(line,1000);
  }

  printf("* Scaler map file loaded. \t\t\t\t\t\t\t [OK]\n");
  return kTRUE;
}


//______________________________________________________________________________
UShort_t* TScaler::Unpack(UShort_t *pEvent)
{
  // -- Unpack the raw scaler event buffer.
  //
  
  UShort_t  nScalers;
  ULong64_t upper,lower,value;

  // Get the number of Scalers read, its nEntities, and advance the pointer
  // to the end time offset.
  nScalers = *pEvent;
  if(nScalers >  TSCALER_NSCALERS) {
    printf("Number of scalers %i is greater than TSCALER_NSCALERS %i\n",nScalers,TSCALER_NSCALERS);
    return pEvent;
  }
  for(Int_t j=0; j<10; j++) *pEvent++;
  
  // Get the end time.
  lower   = (ULong64_t)*pEvent++;
  upper   = (ULong64_t)*pEvent++;
  endTime = (ULong64_t)(upper<<16|lower);
  *pEvent++;
  *pEvent++;
  *pEvent++;

  // Get the start time.
  lower     = (ULong64_t)*pEvent++;
  upper     = (ULong64_t)*pEvent++;
  startTime = (ULong64_t)(upper<<16|lower);
  *pEvent++;
  *pEvent++;
  *pEvent++;

  // Get the scaler values.
  for(Int_t i=0; i<nScalers; i++){
    TScalerCh *pScalers  = (TScalerCh*)fScalers->At(i);
    lower = (ULong64_t)*pEvent++;
    upper = (ULong64_t)*pEvent++;
    value = (ULong64_t)(upper<<16|lower);
    scaler[i] = value;
    pScalers->counts = scaler[i];
    pScalers->rate   = (Double_t)scaler[i]/(Double_t)(endTime-startTime);
    h_totals.Fill(i,value);
    //  h_rate.Fill(i,value);
  }

  #ifdef TSCALER_DEBUG
  cout << "nScalers: " << nScalers 
       << " endTime: " << endTime
       << " startTime: " << startTime
       << endl;
  cout << "Scalers: {";
  for(Int_t k=0; k<nScalers; k++) cout << value << ",";
  cout << endl;
  #endif

  return pEvent;
}

Last change: Sun Dec 21 12:38:59 2008
Last generated: 2008-12-21 12:38

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.