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

#include "TS800IonChamber.h"
#include "TS800.h"

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

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

ClassImp(TS800IonChamber);

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TS800IonChamber                                                      //
//                                                                      //
//                                                                      //
//////////////////////////////////////////////////////////////////////////



//______________________________________________________________________________
TS800IonChamber::TS800IonChamber(const TS800IonChamber &ic) : TObject(ic)
{
  // -- Copy Constructor.
  ((TS800IonChamber&)ic).Copy(*this);
}


//______________________________________________________________________________
void TS800IonChamber::Copy(TObject &ic) const
{
  // -- Copy this method.
  
  TObject::Copy((TObject&)ic);
  ((TS800IonChamber&)ic).fRandom.SetSeed(0);
  for(Int_t i=0; i<TS800_FP_IC_CHANNELS; i++) ((TS800IonChamber&)ic).slope[i]  = slope[i];
  for(Int_t i=0; i<TS800_FP_IC_CHANNELS; i++) ((TS800IonChamber&)ic).offset[i] = offset[i];
  ((TS800IonChamber&)ic).de_slope  = de_slope;
  ((TS800IonChamber&)ic).de_offset = de_offset;
  for(Int_t i=0; i<4; i++) ((TS800IonChamber&)ic).IcX1Correction[i] = IcX1Correction[i];
  for(Int_t i=0; i<4; i++) ((TS800IonChamber&)ic).IcY1Correction[i] = IcY1Correction[i];
  for(Int_t i=0; i<4; i++) ((TS800IonChamber&)ic).IcX2Correction[i] = IcX2Correction[i];
  for(Int_t i=0; i<4; i++) ((TS800IonChamber&)ic).IcY2Correction[i] = IcY2Correction[i];
  ((TS800IonChamber&)ic).fPIDShift = fPIDShift;
  ((TS800IonChamber&)ic).name = name;

  ((TS800IonChamber&)ic).Clear();
}


//______________________________________________________________________________
void TS800IonChamber::InitClass(TString iname, TS800 *iparent) 
{
  // -- Initialize the default class data.
  //

  name = iname;
  parent = iparent;
  for (Int_t i=0; i<TS800_FP_IC_CHANNELS; i++) {
    slope[i]  = 1.0;
    offset[i] = 0.0;
  }
  de_slope  = 1.0;
  de_offset = 0.0;
  memset(IcX1Correction,'\0',sizeof(IcX1Correction));
  memset(IcY1Correction,'\0',sizeof(IcY1Correction));
  memset(IcX2Correction,'\0',sizeof(IcX2Correction));
  memset(IcY2Correction,'\0',sizeof(IcY2Correction));
  
  fPIDShift = 0.0;
}


//______________________________________________________________________________
void TS800IonChamber::InitTree(TTree *tree) 
{
  // -- Initialize the class to tree.
  //
  
  fChain   = tree;
  fCurrent = -1;
  //  TNamed *aState = (TNamed*)fChain->GetUserInfo()->FindObject("Analysis State");
  #ifdef TESTING
  fChain->SetMakeClass(1);
  #endif

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

}


//______________________________________________________________________________
void TS800IonChamber::Clear(Option_t*) 
{
  // -- Clear the data members.
  //

  memset(raw,'\0',sizeof(raw));
  for(Int_t i=0; i<TS800_FP_IC_CHANNELS; i++) cal[i] = sqrt(-1.0);
  sum = sqrt(-1.0);
  de  = sqrt(-1.0);
}


//______________________________________________________________________________
void TS800IonChamber::GetEntry(Int_t i) 
{
  // --

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




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

  Clear();
  fCon->b_raw->GetEntry(entry);

  for(Int_t j=0; j<TS800_FP_IC_CHANNELS; j++) {
    if(parent->fAddRndm) cal[j] = (Double_t)((fCon->raw[j]+fRandom.Rndm()) * slope[j] + offset[j]);
    else                 cal[j] = (Double_t)(fCon->raw[j] * slope[j] + offset[j]);
  }

  //cout << name << " is now calibrated.\n";
  return 1;
}


//______________________________________________________________________________
//void TS800IonChamber::Calibrate() 
// {
//   // -- 
//   //

//   b_cal->Reset();
//   for (Long64_t i=0; i<b_raw->GetEntries(); i++) {
//     Clear();
//     b_raw->GetEntry(i);
//     for(Int_t j=0; j<TS800_FP_IC_CHANNELS; j++) {
//       cal[j] = (Double_t)raw[j] * slope[j] + offset[j];
//     }
//     b_cal->Fill();
//   }
//   cout << name << " is now calibrated.\n";
// }


//______________________________________________________________________________
Int_t TS800IonChamber::Calculate(Long64_t entry) 
{
  // -- Calculate an entry.
  //

  //  Clear();

  Int_t ch;
  ch  = 0;
  sum = 0;
  for(Int_t j=1; j<TS800_FP_IC_CHANNELS; j++) {        // Let get rid of segment 0.
    if(!isnan(cal[j])) {
      sum += cal[j];
      ch++;
    }
  }
  if (ch>1) sum /= (ch-1);
  else sum = sqrt(-1.0);
  //    de = sum * de_slope + de_offset + IcX1Correction * parent->crdc1.x + IcY1Correction * parent->crdc1.y;
  de = sum * de_slope + de_offset 
    + IcX1Correction[1] * parent->crdc1.x 
    + IcY1Correction[1] * parent->crdc1.y
    + IcX1Correction[2] * parent->crdc1.x * parent->crdc1.x 
    + IcY1Correction[2] * parent->crdc1.y * parent->crdc1.y
    + IcX1Correction[3] * parent->crdc1.x * parent->crdc1.x * parent->crdc1.x 
    + IcY1Correction[3] * parent->crdc1.y * parent->crdc1.y * parent->crdc1.y
    + IcX2Correction[1] * parent->crdc2.x 
    + IcY2Correction[1] * parent->crdc2.y
    + IcX2Correction[2] * parent->crdc2.x * parent->crdc2.x
    + IcY2Correction[2] * parent->crdc2.y * parent->crdc2.y
    + IcX2Correction[3] * parent->crdc2.x * parent->crdc2.x * parent->crdc2.x
    + IcY2Correction[3] * parent->crdc2.y * parent->crdc2.y * parent->crdc2.y;
  
  de += fPIDShift;    // PID stablization correction.

  //     de = sum * de_slope + de_offset 
  //       + IcX1Correction * parent->crdc1.x
  //       + IcP1 * parent->crdc2.x * parent->crdc2.x
  //       + IcP2 * parent->crdc2.x * parent->crdc2.x * parent->crdc2.x
  //       + IcY1Correction * parent->crdc1.y;
  
  //printf("Ion Chamber is now calculated.\n");
  return 1;
}

//______________________________________________________________________________
//void TS800IonChamber::Calculate() 
// {
//   // --

//   Int_t ch;
//   b_sum->Reset();
//   b_de->Reset();
   
//   for(Long64_t i=0; i<b_cal->GetEntries(); i++) {
//     parent->crdc1.Clear();
//     parent->crdc2.Clear();
//     Clear();
//     b_cal->GetEntry(i);
//     parent->crdc1.b_x->GetEntry(i);
//     parent->crdc1.b_y->GetEntry(i);
//     parent->crdc2.b_x->GetEntry(i);
//     parent->crdc2.b_y->GetEntry(i);

//     ch  = 0;
//     sum = 0;
//     for(Int_t j=1; j<TS800_FP_IC_CHANNELS; j++) {        // Let get rid of segment 0.
//       if(!isnan(cal[j])) {
// 	sum += cal[j];
// 	ch++;
//       }
//     }
//     if (ch>1) sum /= (ch-1);
//     else sum = sqrt(-1.0);
//     //    de = sum * de_slope + de_offset + IcX1Correction * parent->crdc1.x + IcY1Correction * parent->crdc1.y;
//     de = sum * de_slope + de_offset 
//       + IcX1Correction[1] * parent->crdc1.x 
//       + IcY1Correction[1] * parent->crdc1.y
//       + IcX1Correction[2] * parent->crdc1.x * parent->crdc1.x 
//       + IcY1Correction[2] * parent->crdc1.y * parent->crdc1.y
//       + IcX1Correction[3] * parent->crdc1.x * parent->crdc1.x * parent->crdc1.x 
//       + IcY1Correction[3] * parent->crdc1.y * parent->crdc1.y * parent->crdc1.y
//       + IcX2Correction[1] * parent->crdc2.x 
//       + IcY2Correction[1] * parent->crdc2.y
//       + IcX2Correction[2] * parent->crdc2.x * parent->crdc2.x
//       + IcY2Correction[2] * parent->crdc2.y * parent->crdc2.y
//       + IcX2Correction[3] * parent->crdc2.x * parent->crdc2.x * parent->crdc2.x
//       + IcY2Correction[3] * parent->crdc2.y * parent->crdc2.y * parent->crdc2.y;


// //     de = sum * de_slope + de_offset 
// //       + IcX1Correction * parent->crdc1.x
// //       + IcP1 * parent->crdc2.x * parent->crdc2.x
// //       + IcP2 * parent->crdc2.x * parent->crdc2.x * parent->crdc2.x
// //       + IcY1Correction * parent->crdc1.y;

//     b_sum->Fill();
//     b_de->Fill();
//   }

//   printf("Ion Chamber is now calculated.\n");
// }


//______________________________________________________________________________
UShort_t* TS800IonChamber::Unpack(UShort_t *p) 
{
  // -- Unpack a packet.
  //

  if(*(p+1) == S800_FP_IC_ENERGY_PACKET){
    // IC packet with times
    UShort_t length, ch;
    length = *p++;
    p++;
    length -= 2;
    
    while(length>0){
      ch      = (UShort_t)((*p)&0xf000)>>12;
      raw[ch] = (UShort_t)((*p++)&0xfff);
      length--;
    }
  }else{
    // Old style IC packet
    UShort_t words = (*(p-2))-2, ch;
    while(words > 0){
      ch      = (UShort_t)((*p)&0xf000)>>12;
      raw[ch] = (UShort_t)((*p++)&0xfff);
      words--;
    }
  }
  return p;
}

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