TTemplate Class Reference

#include <TTemplate.h>

Inheritance diagram for TTemplate:
TObject

List of all members.

Public Member Functions

 TTemplate ()
 TTemplate (const std::string &det, int refine, int trigger_polarity, bool debug)
virtual ~TTemplate ()
bool HasConverged () const
bool CheckConverged ()
bool Empty () const
int PulsesMerged () const
void Initialize (int pulseID, TH1D *pulse, TDirectory *dir)
void AddPulse (double x_offset, double y_scale, double y_offset, const TH1D *)
void AddToDirectory (TDirectory *dir, TDirectory *hist_dir)
void NormaliseToAmplitude ()
void NormaliseToIntegral ()
void NormaliseToSumSquares ()
TH1 * RebinToOriginalSampling ()
double GetPedestal () const
double GetTime () const
double GetAmplitude () const
int GetPolarity () const
double GetRefineFactor () const
const TH1D * GetHisto () const
const char * GetName () const

Static Public Member Functions

static std::string MakeName (const IDs::channel &ch)

Private Member Functions

void SubtractPedestal ()
void ScaleHist (double factor)
 ClassDef (TTemplate, 1)

Private Attributes

bool fDebug
bool fConverged
 have we converged or not?
int fTotalPulses
 How many pulses have been averaged to make the template.
int fRefineFactor
 How many samples in the template correspond to 1 sample in an actual wavform.
int fTriggerPolarity
IDs::channel fChannel
std::string fName
TH1D * fErrors
TH1D * fTemplatePulse

Detailed Description

Definition at line 11 of file TTemplate.h.


Constructor & Destructor Documentation

TTemplate::TTemplate (  ) 

Definition at line 9 of file TTemplate.cpp.

00009                     :
00010   fDebug(false),
00011   fConverged(false),
00012   fTotalPulses(0),
00013   fRefineFactor(0),
00014   fTriggerPolarity(0),
00015   fChannel(),
00016   fErrors(NULL),
00017   fTemplatePulse(NULL){
00018 }

TTemplate::TTemplate ( const std::string &  det,
int  refine,
int  trigger_polarity,
bool  debug 
)

Definition at line 20 of file TTemplate.cpp.

References fChannel, fErrors, and IDs::channel::str().

00020                                                                                     :
00021   fDebug(debug),
00022   fConverged(false),
00023   fTotalPulses(0),
00024   fRefineFactor(refine),
00025   fTriggerPolarity(trigger_polarity),
00026   fChannel(det),
00027   fName(MakeName(fChannel)),
00028   fTemplatePulse(NULL){
00029 
00030        // Setup the error hist
00031        std::string error_histname = "hErrorVsPulseAdded_" + fChannel.str();
00032        std::string error_histtitle = "Plot of the Error as each new Pulse is added to the template for the " + fChannel.str() + " channel";
00033        int n_bins = 10000;
00034        fErrors = new TH1D(error_histname.c_str(), error_histtitle.c_str(), n_bins,0,n_bins);
00035 }

TTemplate::~TTemplate (  )  [virtual]

Definition at line 37 of file TTemplate.cpp.

00037                      {
00038 }


Member Function Documentation

void TTemplate::AddPulse ( double  x_offset,
double  y_scale,
double  y_offset,
const TH1D *  hPulse 
)

...and new bin error

Definition at line 47 of file TTemplate.cpp.

References fDebug, fErrors, fTemplatePulse, and fTotalPulses.

00047                                                                                             {
00048 
00049   if (fDebug) {
00050     std::cout << "TTemplate::AddPulse(): # pulses = " << fTotalPulses << std::endl;
00051   }
00052 
00053   double template_pedestal = fTemplatePulse->GetBinContent(1);
00054   double total_error=0;
00055 
00056   // Loop through the pulse histogram
00057   for (int iPulseBin = 1; iPulseBin < hPulse->GetNbinsX(); ++iPulseBin) {
00058 
00059     // First, get the corrected sample value based on the fitted parameters
00060     double uncorrected_value = hPulse->GetBinContent(iPulseBin);
00061     double corrected_value = uncorrected_value - y_offset;
00062     corrected_value /= y_scale;
00063     corrected_value += template_pedestal;
00064 
00065     // Get the corrected bin number based on the fitted time offset We add 0.5
00066     // so that we round to the nearest integer and then subtract time offset
00067     // because this value might not want to go directly into the template
00068     int bin_number = iPulseBin + 0.5 - x_offset; 
00069 
00070     // Only change the bin contents of bins within the range of the template histogram
00071     if (bin_number < 1 || bin_number > fTemplatePulse->GetNbinsX()) {
00072       continue;
00073     }
00074 
00075     // Store the old bin content and error for later
00076     double old_bin_content = fTemplatePulse->GetBinContent(bin_number);
00077     double old_bin_error = fTemplatePulse->GetBinError(bin_number);
00078 
00079     // Calculate the new bin content...
00080     double new_bin_content = fTotalPulses * old_bin_content + corrected_value;
00081     new_bin_content /= (fTotalPulses + 1);
00082 
00084     double new_bin_error = ((fTotalPulses - 1)*old_bin_error*old_bin_error)
00085                          + (corrected_value - old_bin_content)*(corrected_value - new_bin_content);
00086     new_bin_error = std::sqrt(new_bin_error / fTotalPulses);
00087 
00088     // increment total error
00089     total_error+=new_bin_error;
00090 
00091     if (fDebug) {
00092       cout << "TemplateCreator::AddPulseToTemplate(): Bin #" << bin_number 
00093            << ": Corrected Sample Value = " << corrected_value << endl
00094            << "\t\t\tOld Value (Error) = " << old_bin_content << "(" << old_bin_error << ")" << endl
00095            << "\t\t\tNew Value (Error) = " << new_bin_content << "(" << new_bin_error << ")" << endl;
00096     }
00097 
00098     // Set the new bin content and error of the template
00099     fTemplatePulse->SetBinContent(bin_number, new_bin_content);
00100     fTemplatePulse->SetBinError(bin_number, new_bin_error);
00101   }
00102 
00103   // increment the total number of pulses
00104   ++fTotalPulses;
00105 
00106   // fill the error histoo
00107   fErrors->Fill(fTotalPulses, total_error);
00108 }

void TTemplate::AddToDirectory ( TDirectory *  dir,
TDirectory *  hist_dir 
) [inline]

Definition at line 24 of file TTemplate.h.

References fErrors, and fTemplatePulse.

00024                                                                {
00025        TDirectory* curr=TDirectory::CurrentDirectory();
00026         dir->cd();
00027         TObject::Write();
00028         if(hist_dir){
00029           fTemplatePulse->SetDirectory(hist_dir);
00030           fErrors->SetDirectory(hist_dir);
00031         }
00032         curr->cd();
00033       }

bool TTemplate::CheckConverged (  ) 

Definition at line 110 of file TTemplate.cpp.

References fConverged, fErrors, and fTotalPulses.

00110                               {
00111   if(fConverged) return true;
00112 
00113   // Check the difference between this iteration and previous ones and, if it's small, the template has converged
00114   int n_bins_to_check = 10;
00115   if (fTotalPulses<n_bins_to_check) return false;
00116   double convergence_limit = 0.1;
00117   int newest_bin=fErrors->FindBin(fTotalPulses);
00118   double error=fErrors->GetBinContent(newest_bin)/fTotalPulses;
00119   for (int iPrevBin = 0; iPrevBin < n_bins_to_check; ++iPrevBin) {
00120     double previous_error = fErrors->GetBinContent(newest_bin-iPrevBin);
00121     previous_error /= fTotalPulses- iPrevBin;
00122 
00123     if ( std::fabs(previous_error - error) > convergence_limit) {
00124        fConverged=false;
00125        break;
00126     } else{
00127        fConverged=true;
00128     }
00129   }
00130 
00131   return fConverged;
00132 }

TTemplate::ClassDef ( TTemplate  ,
 
) [private]
bool TTemplate::Empty (  )  const [inline]

Definition at line 20 of file TTemplate.h.

References fTotalPulses.

00020 {return fTotalPulses==0;}

double TTemplate::GetAmplitude (  )  const
const TH1D* TTemplate::GetHisto (  )  const [inline]
const char* TTemplate::GetName (  )  const [inline]

Definition at line 49 of file TTemplate.h.

References fName.

00049 {return fName.c_str();}

double TTemplate::GetPedestal (  )  const

Definition at line 200 of file TTemplate.cpp.

References fTemplatePulse.

Referenced by TemplateFitter::FitPulseToTemplate(), GetAmplitude(), and TemplateFitAPGenerator::TemplateFitAPGenerator().

00200                                   {
00201   return fTemplatePulse->GetBinContent(1);
00202 }

int TTemplate::GetPolarity (  )  const [inline]

Definition at line 43 of file TTemplate.h.

References fTriggerPolarity.

00043 {return fTriggerPolarity;};

double TTemplate::GetRefineFactor (  )  const [inline]
double TTemplate::GetTime (  )  const
bool TTemplate::HasConverged (  )  const [inline]

Definition at line 18 of file TTemplate.h.

References fConverged.

00018 {return fConverged;}

void TTemplate::Initialize ( int  pulseID,
TH1D *  pulse,
TDirectory *  dir 
)

Definition at line 40 of file TTemplate.cpp.

References fTemplatePulse, and fTotalPulses.

00040                                                                    {
00041   
00042   fTemplatePulse=pulse;
00043   fTemplatePulse->SetBit(TH1::kIsAverage);
00044   ++fTotalPulses;
00045 }

static std::string TTemplate::MakeName ( const IDs::channel ch  )  [inline, static]

Definition at line 50 of file TTemplate.h.

References IDs::channel::str().

Referenced by TemplateArchive::GetTemplate().

00050 {return ch.str()+"_template";}

void TTemplate::NormaliseToAmplitude (  ) 

Definition at line 134 of file TTemplate.cpp.

References fTemplatePulse, ScaleHist(), and SubtractPedestal().

00134                                     {
00135     if(!fTemplatePulse) return;
00136     fTemplatePulse->SetBit(TH1::kIsAverage);
00137     SubtractPedestal();
00138 
00139     double norm = std::fabs(fTemplatePulse->GetMaximum()); 
00140     ScaleHist(1./norm);
00141 }

void TTemplate::NormaliseToIntegral (  ) 

Definition at line 143 of file TTemplate.cpp.

References fTemplatePulse, ScaleHist(), and SubtractPedestal().

00143                                    {
00144     if(!fTemplatePulse) return;
00145     fTemplatePulse->SetBit(TH1::kIsAverage);
00146     SubtractPedestal();
00147 
00148     double norm = fTemplatePulse->Integral(); 
00149     ScaleHist(1./norm);
00150 }

void TTemplate::NormaliseToSumSquares (  ) 

Definition at line 152 of file TTemplate.cpp.

References fTemplatePulse, ScaleHist(), and SubtractPedestal().

Referenced by TemplateConvolveAPGenerator::TemplateConvolveAPGenerator().

00152                                      {
00153     if(!fTemplatePulse) return;
00154     fTemplatePulse->SetBit(TH1::kIsAverage);
00155     SubtractPedestal();
00156 
00157     TH1D* tmp = (TH1D*) fTemplatePulse->Clone("tmp"); 
00158     tmp->Multiply(tmp);
00159     double norm = sqrt(tmp->Integral());
00160     delete tmp;
00161     ScaleHist(1./norm);
00162 }

int TTemplate::PulsesMerged (  )  const [inline]

Definition at line 21 of file TTemplate.h.

References fTotalPulses.

00021 {return fTotalPulses;}

TH1 * TTemplate::RebinToOriginalSampling (  ) 

Definition at line 164 of file TTemplate.cpp.

References fRefineFactor, and fTemplatePulse.

Referenced by TemplateConvolveAPGenerator::TemplateConvolveAPGenerator().

00164                                        {
00165    return fTemplatePulse->Rebin(fRefineFactor);
00166 }

void TTemplate::ScaleHist ( double  factor  )  [private]

Definition at line 188 of file TTemplate.cpp.

References fTemplatePulse.

Referenced by NormaliseToAmplitude(), NormaliseToIntegral(), and NormaliseToSumSquares().

00188                                       {
00189 
00190     // Subtract off the pedesal
00191     for (int iBin = 0; iBin <= fTemplatePulse->GetNbinsX(); ++iBin) {
00192       double old_value = fTemplatePulse->GetBinContent(iBin);
00193       double new_value = old_value * factor;
00194 
00195       fTemplatePulse->SetBinContent(iBin, new_value);
00196     }
00197 }

void TTemplate::SubtractPedestal (  )  [private]

Definition at line 168 of file TTemplate.cpp.

References fTemplatePulse.

Referenced by NormaliseToAmplitude(), NormaliseToIntegral(), and NormaliseToSumSquares().

00168                                 {
00169 
00170     // Normalise the template so that it has pedestal=0 and amplitude=1
00171     // Work out the pedestal of the template from the first 5 bins
00172     int n_bins_for_template_pedestal = 5;
00173     double total = 0;
00174     for (int iBin = 1; iBin <= n_bins_for_template_pedestal; ++iBin) {
00175       total += fTemplatePulse->GetBinContent(iBin);
00176     }
00177     double template_pedestal = total / n_bins_for_template_pedestal;
00178    
00179     // Subtract off the pedesal
00180     for (int iBin = 1; iBin <= fTemplatePulse->GetNbinsX(); ++iBin) {
00181       double old_value = fTemplatePulse->GetBinContent(iBin);
00182       double new_value = old_value - template_pedestal;
00183 
00184       fTemplatePulse->SetBinContent(iBin, new_value);
00185     }
00186 }


Member Data Documentation

Definition at line 61 of file TTemplate.h.

Referenced by TTemplate().

bool TTemplate::fConverged [private]

have we converged or not?

Definition at line 57 of file TTemplate.h.

Referenced by CheckConverged(), and HasConverged().

bool TTemplate::fDebug [private]

Definition at line 56 of file TTemplate.h.

Referenced by AddPulse().

TH1D* TTemplate::fErrors [private]

Definition at line 63 of file TTemplate.h.

Referenced by AddPulse(), AddToDirectory(), CheckConverged(), and TTemplate().

std::string TTemplate::fName [private]

Definition at line 62 of file TTemplate.h.

Referenced by GetName().

int TTemplate::fRefineFactor [private]

How many samples in the template correspond to 1 sample in an actual wavform.

Definition at line 59 of file TTemplate.h.

Referenced by GetRefineFactor(), and RebinToOriginalSampling().

TH1D* TTemplate::fTemplatePulse [private]
int TTemplate::fTotalPulses [private]

How many pulses have been averaged to make the template.

Definition at line 58 of file TTemplate.h.

Referenced by AddPulse(), CheckConverged(), Empty(), Initialize(), and PulsesMerged().

Definition at line 60 of file TTemplate.h.

Referenced by GetAmplitude(), GetPolarity(), and GetTime().


The documentation for this class was generated from the following files:

Generated on 15 Jun 2016 for AlcapDAQ by  doxygen 1.6.1