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


#include "TS800CrdcPads.h"
#include "TS800Crdc.h"
#include "nrutil.h"

#include <iostream>
#include <math.h>

#include <TNamed.h>
#include <TList.h>

ClassImp(TS800CrdcPads);

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TS800Pads                                                            //
//                                                                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

extern short gNumrecError, gNumrecMessage;


//______________________________________________________________________________
TS800CrdcPads::TS800CrdcPads(const TS800CrdcPads &pad) : TObject(pad)
{
  // -- Copy Constructor.
  ((TS800CrdcPads&)pad).Copy(*this);

  parent = NULL;
  b_cal  = NULL;
  b_sum  = NULL;
  fChain = NULL;
  fCurrent = 0;

  Clear();
}


//______________________________________________________________________________
Int_t TS800CrdcPads::Calibrate(Long64_t entry) 
{
  // -- Calibrate an entry.
  //

  // Check that a TTree had been initialized.
  if(!fChain){printf("TTree or TChain has not been initialized!!!\n"); return -1;}

  Int_t index;
  Int_t samples[256];
  Bool_t fAddRndm = kTRUE;   // FIX ME!
  
  Clear();
  fCon->parent->b_filled->GetEntry(entry);
  fCon->parent->b_channel->GetEntry(entry);
  fCon->parent->b_data->GetEntry(entry);
  bzero(samples, sizeof(samples));
  memset(sum,'\0', sizeof(sum));
  for (Int_t f=0; f<fCon->parent->filled; f++) {
    index       = fCon->parent->channel[f];
    if(fAddRndm) sum[index] += (Double_t)((fCon->parent->data[f]+fRandom.Rndm()) - ped[index]);
    else         sum[index] += (Double_t)( fCon->parent->data[f] - ped[index]);
    //    sum[index] += (Double_t)((fCon->parent->data[f]) - ped[index]);
    samples[index]++;
  }
  for (Int_t ch=0; ch<fCon->parent->channels; ch++) {
    if(sum[ch]<0) sum[ch] = sqrt(-1.0);
    if (samples[ch] > 0) {
      cal[ch] = sum[ch]*slope[ch]/(Double_t)(samples[ch]);
      //	cal[ch] = sum[ch]*slope[ch]+thresh[ch]*samples[ch];
      if(cal[ch]<0) cal[ch] = sqrt(-1.0);
      if(thresh[ch]>ped[ch] && cal[ch]<(thresh[ch]-ped[ch])) cal[ch] = sqrt(-1.0);
    }
  }
  return 1;
}


//______________________________________________________________________________
// Bool_t TS800CrdcPads::Calibrate() 
// {
//   // --
//   //

//   // Check that a TTree had been initialized.
//   if(!fChain) {
//     printf("TTree or TChain has not been initialized!!!\n");
//     return kFALSE;
//   }

//   Int_t index;
//   Int_t samples[256];
  
//   b_cal->Reset();
//   b_sum->Reset();
//   Long64_t nEntries = parent->b_filled->GetEntries();
//   cout << "Entries: " << nEntries << endl;
//   for (Long64_t i=0; i<nEntries; i++) {
//     Clear();
//     parent->b_filled->GetEntry(i);
//     parent->b_channel->GetEntry(i);
//     //    cout << i << " " ;
//     //    cout << parent->filled << " ";
//     //    for(Int_t zz=0; zz<parent->filled; zz++) cout << parent->channel[zz] << " " ;
//     //    cout << endl;
//     parent->b_data->GetEntry(i);
//     bzero(samples, sizeof(samples));
//     memset(sum,'\0', sizeof(sum));
//     for (Int_t f=0; f<parent->filled; f++) {
//       index      = parent->channel[f];
//       sum[index] += parent->data[f] - ped[index];
//       samples[index]++;
//     }
//     for (Int_t ch=0; ch<parent->channels; ch++) {
//       if(sum[ch]<0) sum[ch] = sqrt(-1.0);
//       if (samples[ch] > 0) {
// 	cal[ch] = sum[ch]*slope[ch]/(Double_t)(samples[ch]);
// 	//	cal[ch] = sum[ch]*slope[ch]+thresh[ch]*samples[ch];
// 	if(cal[ch]<0) cal[ch] = sqrt(-1.0);
// 	if(thresh[ch]>ped[ch] && cal[ch]<(thresh[ch]-ped[ch])) cal[ch] = sqrt(-1.0);
//       }
//     }
//     b_cal->Fill();
//     b_sum->Fill();
//   }
//   cout << name << " is now calibrated.\n";

//   return kTRUE;
// }


//______________________________________________________________________________
void TS800CrdcPads::InitClass(TString iname) 
{
  // --
  //

  name = iname;
  sampleWidth = 8;
  for (Int_t i=0; i<parent->channels; i++) {
    ped[i]    = 0;
    slope[i]  = 1;
    thresh[i] = 0;
  }
}


//______________________________________________________________________________
void TS800CrdcPads::InitTree(TTree *tree) 
{
  // --
  //

  fChain   = tree;
  fCurrent = -1;
  //  TNamed *aState = (TNamed*)fChain->GetUserInfo()->FindObject("Analysis State");
  #ifdef TESTING
  fChain->SetMakeClass(1);
  #endif

  //   fChain->SetBranchAddress(name+".cal[224]",cal,&b_cal);
  //   fChain->SetBranchAddress(name+".sum[256]",sum,&b_sum);
  //  if(      strcmp(aState->GetName(),"0")){
    b_cal = fChain->GetBranch(name+".cal[224]");
    b_sum = fChain->GetBranch(name+".sum[256]");
//   }else if(strcmp(aState->GetName(),"1")){
//     b_cal = fChain->GetBranch(name+".cal[224]");
//     b_sum = fChain->GetBranch(name+".sum[256]");
//   }else if(strcmp(aState->GetName(),"2")){
//     fChain->SetBranchAddress("E02023Cal."+name+".cal[224]",cal,&b_cal);
//     fChain->SetBranchAddress("E02023Cal."+name+".sum[256]",sum,&b_sum);
//  }
}


//______________________________________________________________________________
void TS800CrdcPads::Clear(Option_t*) 
{
  // --
  //

  //  for (Int_t i=0; i<parent->channels; i++) cal[i] = sqrt(-1.0);
  //  for (Int_t i=0; i<parent->channels; i++) sum[i] = sqrt(-1.0);
  for (Int_t i=0; i<TS800_FP_CRDC_CHANNELS; i++) cal[i] = sqrt(-1.0);
  for (Int_t i=0; i<256; i++) sum[i] = sqrt(-1.0);
}


//______________________________________________________________________________
void TS800CrdcPads::Copy(TObject &pad) const
{
  // -- Copy this method.

  TObject::Copy((TObject&)pad);
  for(Int_t i=0; i<TS800_FP_CRDC_CHANNELS; i++) ((TS800CrdcPads&)pad).ped[i]    = ped[i];
  for(Int_t i=0; i<TS800_FP_CRDC_CHANNELS; i++) ((TS800CrdcPads&)pad).slope[i]  = slope[i];
  for(Int_t i=0; i<TS800_FP_CRDC_CHANNELS; i++) ((TS800CrdcPads&)pad).thresh[i] = thresh[i];
  ((TS800CrdcPads&)pad).sampleWidth = sampleWidth;
  ((TS800CrdcPads&)pad).name        = name;

  // Clear();
}


//______________________________________________________________________________
void TS800CrdcPads::GetEntry(int i) 
{
  // --
  //

  b_cal->GetEntry(i);
  b_sum->GetEntry(i);
}


//______________________________________________________________________________
void TS800CrdcPads::LoadCalibration(Char_t *filename) 
{
  // --
  //

  FILE *file;
  char str[200], var[100], *sub, **ap, *argv[10];
  Double_t value;
  
  // This method will read Tcl format	
  file = fopen(filename, "r");
  if (file == NULL) {
    cout <<  "Could not find calibration file " << filename << endl;
    return;
  }
  while (!feof(file)) {
    fgets(str, 200, file);
    sscanf(str, "set %s %lf", var, &value);
    // Parse the name of the variable separated by dots
    sub = var;
    for (ap = argv; (*ap = strsep(&sub, ".")) != NULL;)
      if (**ap != '\0')
	if (++ap >= &argv[10]) break;
    if (strcmp(argv[4], "ped")    == 0) ped[strtol(argv[5], NULL, 10)]    = value;		
    if (strcmp(argv[4], "slope")  == 0) slope[strtol(argv[5], NULL, 10)]  = value;		
    if (strcmp(argv[4], "thresh") == 0) thresh[strtol(argv[5], NULL, 10)] = value;		
  }
  fclose(file);
  cout << "Calibration of " << name << " loaded.\n";
}



Last change: Sun Dec 21 12:38:55 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.