Ignore:
Timestamp:
05/17/05 12:08:31 (19 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/msignal/MExtractPINDiode.cc

    r4896 r7043  
    2525//////////////////////////////////////////////////////////////////////////////
    2626//
    27 //   MExtractPINDiode
     27//  MExtractPINDiode
    2828//
    2929//  Extracts the signal from a fixed window in a given range.
    3030//
    3131//  Call: SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast)
    32 //  to modify the ranges. Ranges have to be an even number. In case of odd
    33 //  ranges, the last slice will be reduced by one.
     32//  to modify the ranges.
     33//
    3434//  Defaults are:
    3535//
    36 //   fHiGainFirst =  fgHiGainFirst =  3
    37 //   fHiGainLast  =  fgHiGainLast  =  14
    38 //   fLoGainFirst =  fgLoGainFirst =  3
    39 //   fLoGainLast  =  fgLoGainLast  =  14
     36//   fHiGainFirst =  fgHiGainFirst =  0
     37//   fHiGainLast  =  fgHiGainLast  =  29
     38//   fLoGainFirst =  fgLoGainFirst =  0
     39//   fLoGainLast  =  fgLoGainLast  =  0
     40//
     41//  The FADC slices are fit by a Gaussian around the pulse maximum.
     42//  The following figures show two typical (high-intensity and low-intensity)
     43//  pulses together with the applied fit:
     44//
     45//Begin_Html
     46/*
     47<img src="images/PINDiode_pulse_high.gif">
     48<img src="images/PINDiode_pulse_low.gif">
     49*/
     50//End_Html
     51//
     52// The fit ranges can be modified with the functions:
     53// - SetLowerFitLimit()
     54// - SetUpperFitLimit()
     55//
     56// Defaults are:
     57//   - fLowerFitLimit: 2
     58//   - fUpperFitLimit: 5
    4059//
    4160//////////////////////////////////////////////////////////////////////////////
     
    4463#include <fstream>
    4564
     65#include <TF1.h>
     66#include <TH1.h>
     67#include <TPad.h>
     68
    4669#include "MLog.h"
    4770#include "MLogManip.h"
     
    6184using namespace std;
    6285
    63 const UInt_t MExtractPINDiode::fgPINDiodeIdx     = 100;
    64 const Byte_t MExtractPINDiode::fgHiGainFirst =  0;
    65 const Byte_t MExtractPINDiode::fgHiGainLast  =  14;
    66 const Byte_t MExtractPINDiode::fgLoGainFirst =  0;
    67 const Byte_t MExtractPINDiode::fgLoGainLast  =  14;
     86const UInt_t MExtractPINDiode::fgPINDiodeIdx   =  3;
     87const Byte_t MExtractPINDiode::fgHiGainFirst   =  0;
     88const Byte_t MExtractPINDiode::fgHiGainLast    = 29;
     89const Byte_t MExtractPINDiode::fgLoGainFirst   =  0;
     90const Byte_t MExtractPINDiode::fgLoGainLast    =  0;
     91const Byte_t MExtractPINDiode::fgLowerFitLimit =  2;
     92const Byte_t MExtractPINDiode::fgUpperFitLimit =  5;
     93
    6894// --------------------------------------------------------------------------
    6995//
     
    7298// Calls:
    7399// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
    74 // - SetPINDiodeIdx()
     100//
     101// - Set fPINDiodeIdx   to fgPINDiodeIdx
     102// - Set fLowerFitLimit to fgLowerFitLimit
     103// - Set fUpperFitLimit to fgUpperFitLimit
    75104//
    76105MExtractPINDiode::MExtractPINDiode(const char *name, const char *title)
    77 {
    78  
     106    : fSlices(NULL)
     107{
     108
    79109  fName  = name  ? name  : "MExtractPINDiode";
    80110  fTitle = title ? title : "Task to extract the signal from the FADC slices";
     
    82112  SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
    83113  SetPINDiodeIdx();
     114
     115  SetLowerFitLimit();
     116  SetUpperFitLimit(); 
     117
     118  fPedMean.Set(2);
     119}
     120
     121// --------------------------------------------------------------------------
     122//
     123// - delete the histogram fSlices, if it exists
     124//
     125MExtractPINDiode::~MExtractPINDiode()
     126{
     127  if (fSlices)
     128    delete fSlices;
    84129}
    85130
     
    89134//
    90135// Checks:
    91 // - if the window defined by (fHiGainLast-fHiGainFirst-1) are odd, subtract one
    92 // - if the window defined by (fLoGainLast-fLoGainFirst-1) are odd, subtract one
    93 // - if the Hi Gain window is smaller than 2, set fHiGainLast to fHiGainFirst+1
    94 // - if the Lo Gain window is smaller than 2, set fLoGainLast to fLoGainFirst+1
     136// - if the Hi Gain window is smaller than 4, set fHiGainLast to fHiGainFirst+3
    95137//
    96138// Calls:
     
    99141// Sets:
    100142// - fNumHiGainSamples to: (Float_t)(fHiGainLast-fHiGainFirst+1)
    101 // - fNumLoGainSamples to: (Float_t)(fLoGainLast-fLoGainFirst+1)
     143// - fNumLoGainSamples to: 0.
    102144// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
    103 // - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples) 
     145// - fSqrtLoGainSamples to: 0.
    104146// 
    105147void MExtractPINDiode::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
    106148{
    107149
    108   const Byte_t window = hilast-hifirst+1+lolast-lofirst+1;
    109   const Byte_t weven  = window & ~1;
    110 
    111   if (weven != window)
     150  lofirst = 0;
     151  lolast  = 0;
     152
     153  const Byte_t window = hilast-hifirst+1;
     154
     155  if (window<4)
    112156    {
    113157      *fLog << warn << GetDescriptor()
    114             << Form("%s%2i%s%2i",": Total window size has to be even, set last slice from "
    115                     ,(int)lolast," to ",(int)(lolast-1)) << endl;
    116       lolast -= 1;
    117     }
    118  
    119   if (window<2)
    120     {
    121       *fLog << warn << GetDescriptor()
    122             << Form("%s%2i%s%2i",": Total window is smaller than 2 FADC sampes, set last slice from"
    123                     ,(int)lolast," to ",(int)(lofirst+1)) << endl;
    124       hilast = hifirst+1;
    125     }
    126  
     158            << Form("%s%2i%s%2i",": Total window is smaller than 4 FADC sampes, set last slice from"
     159                    ,(int)lolast," to ",(int)(lofirst+3)) << endl;
     160      hilast = hifirst+3;
     161    }
    127162
    128163  MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
    129164
    130165  fNumHiGainSamples = (Float_t)(fHiGainLast-fHiGainFirst+1);
    131   fNumLoGainSamples = (fLoGainLast == 0) ? 0. : (Float_t)(fLoGainLast-fLoGainFirst+1); 
     166  fNumLoGainSamples = 0.;
    132167
    133168  fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
    134   fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples); 
    135  
    136   fNumSamples = (fLoGainLast == 0)
    137     ? fHiGainLast-fHiGainFirst+1
    138     : fHiGainLast-fHiGainFirst+1+fLoGainLast-fLoGainFirst+1;
    139   fSqrtSamples = TMath::Sqrt((Float_t)fNumSamples);
     169  fSqrtLoGainSamples = 0.;
    140170 
    141171}
     
    149179// they were not found:
    150180//
     181//  - MRawEvtData2
    151182//  - MExtractedPINDiode
     183//
     184// Initializes fPedMean to:
     185// - fPedMean[0]: pedestal + AB-offset
     186// - fPedMean[1]: pedestal - AB-offset
     187//
     188// Initializes TH1F fSlices to [fHiGainFirst-0.5,fHiGainLast+0.5]
    152189//
    153190Int_t MExtractPINDiode::PreProcess(MParList *pList)
     
    157194    return kFALSE;
    158195 
     196  fRawEvt = NULL;
     197  fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData2"));
     198  if (!fRawEvt)
     199    {
     200      *fLog << err << AddSerialNumber("MRawEvtData2") << " not found... aborting." << endl;
     201      return kFALSE;
     202    }
     203
    159204  fPINDiode = (MExtractedSignalPINDiode*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalPINDiode"));
    160205  if (!fPINDiode)
    161206    return kFALSE;
    162207
     208  fPedMean.Reset();
     209
    163210  const MPedestalPix &ped   = (*fPedestals)[fPINDiodeIdx];
    164211
    165     if (&ped)
    166       {
    167         fPedestal = ped.GetPedestal();
    168         fPedRms   = ped.GetPedestalRms();
    169       }
    170     else
    171       {
    172         *fLog << err << " Cannot find MPedestalPix of the PIN Diode (idx="
    173               << fPINDiodeIdx << ")" << endl;
     212  if (&ped)
     213    {
     214      fPedMean[0] = ped.GetPedestal() + ped.GetPedestalABoffset();
     215      fPedMean[1] = ped.GetPedestal() - ped.GetPedestalABoffset();     
     216    }
     217  else
     218    {
     219      *fLog << err << " Cannot find MPedestalPix of the PIN Diode (idx="
     220            << fPINDiodeIdx << ")" << endl;
    174221        return kFALSE;
    175       }
    176 
    177     return kTRUE;
     222    }
     223 
     224  if (fSlices)
     225    delete fSlices;
     226
     227  fSlices = new TH1F("PINDiode","PIN Diode fitted slices",(Int_t)(fHiGainLast-fHiGainFirst+1),
     228                     fHiGainFirst-0.5,fHiGainLast+0.5);
     229  fSlices->SetDirectory(NULL);
     230
     231  return kTRUE;
    178232}
    179233
     
    190244 
    191245  fPINDiode->SetUsedFADCSlices(fHiGainFirst, fLoGainLast);
    192  
     246
     247  *fLog << endl;
     248  *fLog << inf << "Taking " << fNumHiGainSamples
     249        << " HiGain samples from slice " << (Int_t)fHiGainFirst
     250        << " to " << (Int_t)(fHiGainLast+fHiLoLast) << " incl" << endl;
     251
    193252  return kTRUE;
    194  
    195 }
    196 
    197 
    198 
    199 void MExtractPINDiode::FindSignalandVarianceHiGain(Byte_t *ptr, Int_t &sum, Int_t &sum2, Byte_t &sat) const
    200 {
    201 
    202   Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
    203 
     253}
     254
     255
     256
     257
     258// ----------------------------------------------------------------------------------
     259//
     260// Extracts the (pedestal-subtracted) FADC slices between fHiGainFirst and fHiGainLast
     261// and fills them into the histogram fSlices. Memorizes the position of maximum at
     262// maxpos.
     263//
     264// Checks for saturation
     265//
     266// Fits fSlices to a Gaussian in the ranges: maxpos-fLowerFitLimit, maxpos+fUpperFitLimit
     267//
     268// Writes fit results into MExtractedSignalPINDiode
     269//
     270Int_t MExtractPINDiode::Process()
     271{
     272
     273
     274  MRawEvtPixelIter pixel(fRawEvt);
     275 
     276  fPINDiode->Clear();
     277  fSlices->Reset();
     278 
     279  pixel.Jump(fPINDiodeIdx);
     280 
     281  Byte_t sat  = 0;
     282
     283  Int_t higainsamples = pixel.GetNumHiGainSamples();
     284  Int_t logainsamples = pixel.GetNumLoGainSamples();
     285 
     286  const Bool_t higainabflag = pixel.HasABFlag();
     287  Byte_t *ptr = pixel.GetHiGainSamples()+fHiGainFirst;
     288  Byte_t *end = ptr+higainsamples;
     289 
     290  Int_t cnt=0;
     291
     292  Float_t max = 0.;
     293  Int_t maxpos = 0;
     294 
    204295  while (ptr<end)
    205296    {
    206       sum  += *ptr;
    207       sum2 += *ptr * *ptr;
    208 
    209       if (*ptr++ >= fSaturationLimit)
    210         sat++;
    211     }
    212 }
    213 
    214 void MExtractPINDiode::FindSignalandVarianceLoGain(Byte_t *ptr, Int_t &sum, Int_t &sum2, Byte_t &sat) const
    215 {
    216 
    217   Byte_t *end = ptr +  fLoGainLast - fLoGainFirst  + 1;
    218 
    219   while (ptr<end)
    220     {
    221       sum  += *ptr;
    222       sum2 += *ptr * *ptr;
    223297     
    224       if (*ptr++ >= fSaturationLimit)
    225         sat++;
    226     }
    227 }
    228 
    229 
    230 
    231 // --------------------------------------------------------------------------
    232 //
    233 // Calculate the integral of the FADC time slices and store them as a new
    234 // pixel in the MExtractedPINDiode container.
    235 //
    236 Int_t MExtractPINDiode::Process()
    237 {
    238 
    239 
    240   MRawEvtPixelIter pixel(fRawEvt);
    241  
    242   fPINDiode->Clear();
    243  
    244   pixel.Jump(fPINDiodeIdx);
    245  
    246   Int_t sum   = 0;
    247   Int_t sum2  = 0;
    248   Byte_t sat  = 0;
    249   Int_t max   = 0;
    250  
    251   //
    252   // Calculate first the time:
    253   //
    254   const Int_t maxhi = pixel.GetIdxMaxHiGainSample();
    255   const Int_t maxlo = pixel.GetIdxMaxLoGainSample();
    256  
    257   if (maxhi > maxlo)
    258     max = maxhi;
    259   else
    260     max = maxlo + pixel.GetNumHiGainSamples();
    261  
    262   FindSignalandVarianceHiGain(pixel.GetHiGainSamples()+fHiGainFirst,sum,sum2,sat);
    263   if (pixel.HasLoGain())
    264     FindSignalandVarianceLoGain(pixel.GetLoGainSamples()+fLoGainFirst,sum,sum2,sat);
    265 
    266   const Float_t var = ((Float_t)sum2 - (Float_t)sum*sum/fNumSamples)/(fNumSamples-1);
    267   const Float_t rms = TMath::Sqrt(var);
    268  
    269   //
    270   // FIXME: The following formulae have to be revised!!
    271   //
    272   fPINDiode->SetExtractedSignal(sum - fPedestal*fNumSamples, fPedRms*fSqrtSamples);
    273   fPINDiode->SetExtractedRms   (rms, rms/2./fSqrtSamples);
    274   fPINDiode->SetExtractedTime  (max, rms/fSqrtSamples);
    275   fPINDiode->SetSaturation(sat);
     298      if (*ptr >= fSaturationLimit)
     299        {
     300          sat++;
     301          break;
     302        }
     303
     304      const Float_t cont = (Float_t)*ptr - fPedMean[(cnt + higainabflag) & 0x1];
     305      fSlices->Fill(cnt,cont);
     306
     307      if (cont > max)
     308      {
     309        max = cont;
     310        maxpos = cnt;
     311      }
     312
     313      ptr++;
     314      cnt++;
     315    }
     316 
     317  cnt = 0;
     318 
     319  if (pixel.HasLoGain() && !sat)
     320    {
     321     
     322      ptr = pixel.GetLoGainSamples();
     323      end = ptr+logainsamples;
     324     
     325      const Bool_t logainabflag = (higainabflag + pixel.GetNumHiGainSamples()) & 0x1;
     326     
     327      while (ptr<end)
     328        {
     329         
     330          if (*ptr >= fSaturationLimit)
     331            {
     332              sat++;
     333              break;
     334            }
     335
     336          const Float_t cont = (Float_t)*ptr - fPedMean[(cnt + logainabflag) & 0x1];
     337         
     338          fSlices->Fill(cnt+ higainsamples,cont);
     339         
     340          if (cont > max)
     341            {
     342              max    = cont;
     343              maxpos = cnt+higainsamples;
     344            }
     345          ptr++;
     346          cnt++;
     347        }
     348    }
     349         
     350  if (sat)
     351    {
     352      fPINDiode->SetSaturation(1);
     353      return kTRUE;
     354    }
     355
     356  fSlices->Fit("gaus", "RQ", "", maxpos-fLowerFitLimit,maxpos+fUpperFitLimit);
     357 
     358  TF1 *gausfunc = fSlices->GetFunction("gaus");
     359
     360  fPINDiode->SetExtractedSignal(gausfunc->GetParameter(0), gausfunc->GetParError(0));
     361  fPINDiode->SetExtractedTime  (gausfunc->GetParameter(1), gausfunc->GetParError(1));
     362  fPINDiode->SetExtractedSigma (gausfunc->GetParameter(2), gausfunc->GetParError(2));
     363  fPINDiode->SetExtractedChi2  (gausfunc->GetChisquare());
    276364  fPINDiode->SetReadyToSave();
    277  
     365
    278366  return kTRUE;
    279367}
    280368
     369// ----------------------------------------------------------------------------------
     370//
     371// deletes fSlices and sets pointer to NULL
     372//
     373Int_t MExtractPINDiode::PostProcess()
     374{
     375 
     376  delete fSlices;
     377  fSlices = NULL;
     378 
     379  return kTRUE;
     380}
Note: See TracChangeset for help on using the changeset viewer.