Ignore:
Timestamp:
10/01/04 11:55:29 (20 years ago)
Author:
gaug
Message:
*** empty log message ***
File:
1 edited

Legend:

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

    r5152 r5155  
    6060#include "TString.h"
    6161
     62#include <fstream>
     63
    6264ClassImp(MExtractTimeAndChargeDigitalFilter);
    6365
    6466using namespace std;
    6567
    66 const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainFirst  =  0;
    67 const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainLast   = 14;
    68 const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainFirst  =  3;
    69 const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainLast   = 14;
    70 const Int_t  MExtractTimeAndChargeDigitalFilter::fgHiGainWindowSize  = 6;
    71 const Int_t  MExtractTimeAndChargeDigitalFilter::fgLoGainWindowSize  = 6;
    72 const Int_t  MExtractTimeAndChargeDigitalFilter::fgBinningResolution = 10;
     68const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainFirst       =  0;
     69const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainLast        = 14;
     70const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainFirst       =  3;
     71const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainLast        = 14;
     72const Int_t  MExtractTimeAndChargeDigitalFilter::fgWindowSizeHiGain  = 6;
     73const Int_t  MExtractTimeAndChargeDigitalFilter::fgWindowSizeLoGain  = 6;
     74const Int_t  MExtractTimeAndChargeDigitalFilter::fgBinningResolutionHiGain = 10;
     75const Int_t  MExtractTimeAndChargeDigitalFilter::fgBinningResolutionLoGain = 10;
    7376// --------------------------------------------------------------------------
    7477//
     
    9295  SetBinningResolution();
    9396
    94   SetWeightsFile("");
     97  ReadWeightsFile("");
    9598}
    9699
     
    108111{
    109112
     113  if (windowh != fgWindowSizeHiGain)
     114    *fLog << warn << GetDescriptor()
     115          << ": ATTENTION!!! If you are not Hendrik Bartko, do NOT use a different window size than the default." << endl;
     116  if (windowl != fgWindowSizeLoGain)
     117    *fLog << warn << GetDescriptor()
     118          << ": ATTENTION!!! If you are not Hendrik Bartko, do NOT use a different window size than the default" << endl;
     119
    110120  fWindowSizeHiGain = windowh;
    111121  fWindowSizeLoGain = windowl;
     
    162172// If filenname is empty, then all weights will be set to 1.
    163173//
    164 void MExtractTimeAndChargeDigitalFilter::SetWeightsFile(TString filename)
    165 {
    166 
    167   fAmpWeightsHiGain .Set(fBinningResolution*fWindowSizeHiGain);
    168   fAmpWeightsLoGain .Set(fBinningResolution*fWindowSizeLoGain);
    169   fTimeWeightsHiGain.Set(fBinningResolution*fWindowSizeHiGain);
    170   fTimeWeightsLoGain.Set(fBinningResolution*fWindowSizeLoGain);
     174Bool_t MExtractTimeAndChargeDigitalFilter::ReadWeightsFile(TString filename)
     175{
     176
     177  fAmpWeightsHiGain .Set(fBinningResolutionHiGain*fWindowSizeHiGain);
     178  fAmpWeightsLoGain .Set(fBinningResolutionLoGain*fWindowSizeLoGain);
     179  fTimeWeightsHiGain.Set(fBinningResolutionHiGain*fWindowSizeHiGain);
     180  fTimeWeightsLoGain.Set(fBinningResolutionLoGain*fWindowSizeLoGain);
    171181 
    172182  if (filename.IsNull())
     
    182192          fTimeWeightsLoGain[i] = 1.;
    183193        }
    184       return;
    185     }
    186 
    187   TFile *f = new TFile(filename);
    188   TH1F  *hw_amp  = (TH1F*)f->Get("hw_amp");
    189   TH1F  *hw_time = (TH1F*)f->Get("hw_time");
    190 
    191   if (!hw_amp)
    192     {
    193       *fLog << warn << GetDescriptor()
    194             << ": Could not find hw_amp in " << filename << endl;
    195       return;
    196     }
    197  
    198   if (!hw_time)
    199     {
    200       *fLog << warn << GetDescriptor()
    201             << ": Could not find hw_time in " << filename << endl;
    202       return;
    203     }
    204  
    205   for (UInt_t i=0; i<fAmpWeightsHiGain.GetSize(); i++)
    206     {
    207       fAmpWeightsHiGain [i] = hw_amp->GetBinContent(i+1);
    208       fTimeWeightsHiGain[i] = hw_time->GetBinContent(i+1);
    209     }
    210   for (UInt_t i=0; i<fAmpWeightsLoGain.GetSize(); i++)
    211     {
    212       fAmpWeightsLoGain [i] = hw_amp->GetBinContent(i+1);
    213       fTimeWeightsLoGain[i] = hw_time->GetBinContent(i+1);
    214     }
    215   //    cout << "w_amp[" << i << "] = " << fw_amp[i] << endl;
    216   delete f;
     194      return kTRUE;
     195    }
     196
     197  ifstream fin(filename.Data());
     198 
     199  if (!fin)
     200    {
     201      *fLog << err << GetDescriptor()
     202            << ": No weights file found: " << filename << endl;
     203      return kFALSE;
     204    }
     205
     206  Int_t len = 0;
     207  Int_t cnt = 0;
     208  Bool_t hi = kFALSE;
     209  Bool_t lo = kFALSE;
     210 
     211  TString str;
     212
     213  while (1)
     214    {
     215
     216      str.ReadLine(fin);
     217      if (!fin)
     218        break;
     219     
     220
     221      if (str.Contains("# High Gain Weights:"))
     222        {
     223          str.ReplaceAll("# High Gain Weights:","");
     224          sscanf(str.Data(),"%2i%2i",&fWindowSizeHiGain,&fBinningResolutionHiGain);
     225          *fLog << inf << "Found number of High Gain slices: " << fWindowSizeHiGain
     226                       << " and High Gain resolution: " << fBinningResolutionHiGain << endl;
     227          len = fBinningResolutionHiGain*fWindowSizeHiGain;
     228          fAmpWeightsHiGain .Set(len);
     229          fTimeWeightsHiGain.Set(len);
     230          hi = kTRUE;
     231          continue;
     232        }
     233     
     234      if (str.Contains("# Low Gain Weights:"))
     235        {
     236          str.ReplaceAll("# Low Gain Weights:","");
     237          sscanf(str.Data(),"%2i%2i",&fWindowSizeLoGain,&fBinningResolutionLoGain);
     238          *fLog << inf << "Found number of Low Gain slices: " << fWindowSizeLoGain
     239                       << " and Low Gain resolution: " << fBinningResolutionLoGain << endl;
     240          len = fBinningResolutionLoGain*fWindowSizeHiGain;
     241          fAmpWeightsLoGain .Set(len);
     242          fTimeWeightsLoGain.Set(len);
     243          lo = kTRUE;
     244          continue;
     245        }
     246     
     247      if (str.Contains("#"))
     248        continue;
     249
     250      if (len == 0)
     251        continue;
     252
     253      sscanf(str.Data(),"\t%f\t%f",lo ? &fAmpWeightsLoGain [cnt] : &fAmpWeightsHiGain [cnt],
     254                                         lo ? &fTimeWeightsLoGain[cnt] : &fTimeWeightsHiGain[cnt]);
     255
     256      if (++cnt == len)
     257        {
     258          len = 0;
     259          cnt = 0;
     260        }
     261    }
     262
     263  if (cnt != len)
     264    {
     265      *fLog << err << GetDescriptor()
     266            << ": Size mismatch in weights file " << filename << endl;
     267      return kFALSE;
     268    }
     269
     270  if (!hi)
     271    {
     272      *fLog << err << GetDescriptor()
     273            << ": No correct header found in weights file " << filename << endl;
     274      return kFALSE;
     275    }
     276
     277  return kTRUE;
     278}
     279
     280//----------------------------------------------------------------------------
     281//
     282// Read a pre-defined weights file into the class.
     283// This is mandatory for the extraction
     284//
     285Bool_t MExtractTimeAndChargeDigitalFilter::WriteWeightsFile(TString filename)
     286{
     287
     288
     289  Float_t amp[60];
     290  Float_t tim[60]; 
     291 
     292  ofstream fn(filename.Data());
     293
     294  fn << "# High Gain Weights: 6 10" << endl;
     295  fn << "# (Amplitude)  (Time) " << endl;
     296
     297  for (UInt_t i=0; i<60; i++)
     298    {
     299      fn << "\t" << amp[i] << "\t" << tim[i] << endl;
     300    }
     301
     302  fn << "# Low Gain Weights: 6 10" << endl;
     303  fn << "# (Amplitude)  (Time) " << endl;
     304
     305  for (UInt_t i=0; i<60; i++)
     306    {
     307      fn << "\t" << amp[i] << "\t" << tim[i] << endl;
     308    }
     309  return kTRUE;
    217310}
    218311
     
    238331
    239332  fLoGainSignal.Set(range);
     333
     334  fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain;
     335  fTimeShiftLoGain = (Float_t)fLoGainFirst + 0.5 + 1./fBinningResolutionLoGain;
    240336
    241337  return kTRUE;
     
    300396    return;
    301397
    302 #if 0
    303   if (maxpos < 2)
    304     {
    305       time = (Float_t)fHiGainFirst;
    306       return;
    307     }
    308 #endif
    309  
    310398  Float_t time_sum  = 0.;
    311399  Float_t fmax      = 0.;
     
    334422      for (Int_t sample=0; sample < fWindowSizeHiGain; sample++)
    335423      {
    336         const Int_t   idx = fBinningResolution*sample+fBinningResolutionHalf;
     424        const Int_t   idx = fBinningResolutionHiGain*sample+fBinningResolutionHalfHiGain;
    337425        const Float_t pex = fHiGainSignal[sample+i]-PedMean[(sample+i+abflag) & 0x1];
    338426        sum              += fAmpWeightsHiGain [idx]*pex;
     
    351439    {
    352440      ftime_max        /= fmax;
    353       Int_t t_iter      = Int_t(ftime_max*fBinningResolution);
     441      Int_t t_iter      = Int_t(ftime_max*fBinningResolutionHiGain);
    354442      Int_t sample_iter = 0;
    355443
    356       while ( t_iter > fBinningResolutionHalf-1 || t_iter < -fBinningResolutionHalf )
    357         {
    358           if (t_iter > fBinningResolutionHalf-1)
     444      while ( t_iter > fBinningResolutionHalfHiGain-1 || t_iter < -fBinningResolutionHalfHiGain )
     445        {
     446          if (t_iter > fBinningResolutionHalfHiGain-1)
    359447            {
    360               t_iter -= fBinningResolution;
     448              t_iter -= fBinningResolutionHiGain;
    361449              max_p--;
    362450              sample_iter--;
    363451            }
    364           if (t_iter < -fBinningResolutionHalf)
     452          if (t_iter < -fBinningResolutionHalfHiGain)
    365453            {
    366               t_iter += fBinningResolution;
     454              t_iter += fBinningResolutionHiGain;
    367455              max_p++;
    368456              sample_iter++;
     
    377465      for (Int_t sample=0; sample < fWindowSizeHiGain; sample++)
    378466      {
    379         const Int_t   idx = fBinningResolution*sample + fBinningResolutionHalf + t_iter;
     467        const Int_t   idx = fBinningResolutionHiGain*sample + fBinningResolutionHalfHiGain + t_iter;
    380468        const Int_t   ids = max_p + sample;
    381469        const Float_t pex = ids < 0 ? 0. :
     
    383471        sum              += fAmpWeightsHiGain [idx]*pex;
    384472        time_sum         += fTimeWeightsHiGain[idx]*pex;
    385         //        cout << idx << " " << fw_amp[idx] << "  " << pex << "  " << t_iter << "  " << sum << "  " << ((Int_t(p2-ptr)+abflag) & 0x1) << "  " << PedMean[(Int_t(p2-ptr)+abflag) & 0x1] << endl;
    386473      }
    387474
    388475      if (sum != 0.)
    389         time = max_p + 1. + sample_iter
    390              - ((Float_t)t_iter)/fBinningResolution - 0.4 - time_sum/sum
    391              + (Float_t)fHiGainFirst;
     476        time = max_p + fTimeShiftHiGain /* this shifts the time to the start of the rising edge */
     477             - ((Float_t)t_iter)/fBinningResolutionHiGain - time_sum/sum;
    392478      else
    393479        time = 0.;
     
    404490{
    405491
    406   const Byte_t *end = ptr + fLoGainLast - fLoGainFirst + 1;
    407  
    408   Float_t time_sum  = 0;
    409   Float_t fmax      = 0;
    410   Float_t ftime_max = 0;
    411  
    412   Float_t pedes = ped.GetPedestal();
    413 
     492  Int_t range = fLoGainLast - fLoGainFirst + 1;
     493  const Byte_t *end = ptr + range;
     494  Byte_t *p     = ptr;
     495  Byte_t maxpos = 0;
     496  Byte_t max    = 0;
     497  Int_t count   = 0;
     498
     499  //
     500  // Check for saturation in all other slices
     501  //
     502  while (p<end)
     503    {
     504
     505      fLoGainSignal[count++] = (Float_t)*p;
     506
     507      if (*p > max)
     508        {
     509          max    = *p;
     510          maxpos =  p-ptr;
     511        }
     512
     513      if (*p++ >= fSaturationLimit)
     514        sat++;
     515    }
     516 
     517  Float_t time_sum  = 0.;
     518  Float_t fmax      = 0.;
     519  Float_t ftime_max = 0.;
     520  Int_t   max_p     = 0;
     521 
     522  const Float_t pedes  = ped.GetPedestal();
    414523  const Float_t ABoffs = ped.GetPedestalABoffset();
    415524       
     
    421530  // Calculate the sum of the first fWindowSize slices
    422531  //
    423   sat           = 0;
    424   Byte_t *p     = ptr;
    425   Byte_t *max_p = ptr;
    426 
    427   while (p < end-fWindowSizeLoGain)
    428     {
    429       sum      = 0;
    430       time_sum = 0;
    431 
     532  for (Int_t i=0;i<range-fWindowSizeLoGain;i++)
     533    {
     534      sum      = 0.;
     535      time_sum = 0.;
     536     
    432537      //
    433538      // Slide with a window of size fWindowSizeLoGain over the sample
    434539      // and multiply the entries with the corresponding weights
    435540      //
    436       Byte_t *p1 = p++;
    437541      for (Int_t sample=0; sample < fWindowSizeLoGain; sample++)
    438542      {
    439         const Int_t idx = fBinningResolution*sample+fBinningResolutionHalf;
    440         const Int_t adx = (Int_t(p1-ptr)+abflag) & 0x1;
    441         sum      += fAmpWeightsLoGain [idx]*(*p1-PedMean[adx]);
    442         time_sum += fTimeWeightsLoGain[idx]*(*p1-PedMean[adx]);
    443         p1++;
     543        const Int_t   idx = fBinningResolutionLoGain*sample+fBinningResolutionHalfLoGain;
     544        const Float_t pex = fLoGainSignal[sample+i]-PedMean[(sample+i+abflag) & 0x1];
     545        sum              += fAmpWeightsLoGain [idx]*pex;
     546        time_sum         += fTimeWeightsLoGain[idx]*pex;
    444547      }
    445548
     
    448551        fmax      = sum;
    449552        ftime_max = time_sum;
    450         max_p     = p;
     553        max_p     = i;
    451554      }
    452     } /* while (p<end-fWindowSizeLoGain) */
    453  
    454     if (fmax!=0)
     555    } /*   for (Int_t i=0;i<range-fWindowSizeLoGain;i++) */
     556 
     557  if (fmax!=0)
    455558    {
    456559      ftime_max        /= fmax;
    457       Int_t t_iter      = Int_t(ftime_max*fBinningResolution);
     560      Int_t t_iter      = Int_t(ftime_max*fBinningResolutionLoGain);
    458561      Int_t sample_iter = 0;
    459       p                 = max_p;
    460 
    461       while((t_iter)>fBinningResolutionHalf-1 || t_iter<-fBinningResolutionHalf)
    462         {
    463           if (t_iter>fBinningResolutionHalf-1)
     562
     563      while ( t_iter > fBinningResolutionHalfLoGain-1 || t_iter < -fBinningResolutionHalfLoGain )
     564        {
     565          if (t_iter > fBinningResolutionHalfLoGain-1)
    464566            {
    465               t_iter -= fBinningResolution;
    466               p--;
     567              t_iter -= fBinningResolutionLoGain;
     568              max_p--;
    467569              sample_iter--;
    468570            }
    469           if (t_iter < -fBinningResolutionHalf)
     571          if (t_iter < -fBinningResolutionHalfLoGain)
    470572            {
    471               t_iter += fBinningResolution;
    472               p++;
     573              t_iter += fBinningResolutionLoGain;
     574              max_p++;
    473575              sample_iter++;
    474576            }
    475577        }
    476578     
    477       sum       = 0;
    478       Byte_t *p2 = p;
    479      
     579      sum = 0.;
    480580      //
    481581      // Slide with a window of size fWindowSizeLoGain over the sample
     
    484584      for (Int_t sample=0; sample < fWindowSizeLoGain; sample++)
    485585      {
    486         const Int_t idx = fBinningResolution*sample+fBinningResolutionHalf+t_iter;
    487         const Int_t adx = (Int_t(p2-ptr)+abflag) & 0x1;
    488         sum      += fAmpWeightsLoGain [idx]*(*p2-PedMean[adx]);
    489         time_sum += fTimeWeightsLoGain[idx]*(*p2-PedMean[adx]);
    490         p2++;
     586        const Int_t   idx = fBinningResolutionLoGain*sample + fBinningResolutionHalfLoGain + t_iter;
     587        const Int_t   ids = max_p + sample;
     588        const Float_t pex = ids < 0 ? 0. :
     589          ( ids > range ? 0. : fLoGainSignal[ids]-PedMean[(ids+abflag) & 0x1]);
     590        sum              += fAmpWeightsLoGain [idx]*pex;
     591        time_sum         += fTimeWeightsLoGain[idx]*pex;
    491592      }
    492593
    493       if (sum != 0)
    494         time = (Float_t)(max_p - ptr) + 1. + sample_iter
    495              - (Float_t)t_iter/fBinningResolution - 0.4 - time_sum/sum
    496              + (Float_t)fLoGainFirst;
     594      if (sum != 0.)
     595        time = max_p + fTimeShiftLoGain /* this shifts the time to the start of the rising edge */
     596             - ((Float_t)t_iter)/fBinningResolutionLoGain - time_sum/sum;
    497597      else
    498598        time = 0.;
     
    527627  if (IsEnvDefined(env, prefix, "BinningResolution", print))
    528628    {
    529       SetBinningResolution(GetEnvValue(env, prefix, "BinningResolution", fBinningResolution));
     629      SetBinningResolution(GetEnvValue(env, prefix, "BinningResolutionHiGain", fBinningResolutionHiGain),
     630                           GetEnvValue(env, prefix, "BinningResolutionLoGain", fBinningResolutionLoGain));
    530631      rc = kTRUE;
    531632    }
     
    536637}
    537638
     639Int_t  MExtractTimeAndChargeDigitalFilter::PostProcess()
     640{
     641 
     642  *fLog << endl;
     643  *fLog << inf << "Used High Gain weights in the extractor: " << endl;
     644 
     645  for (Int_t i=0;i<fBinningResolutionHiGain*fWindowSizeHiGain; i++)
     646    *fLog << inf << fAmpWeightsHiGain[i] << "   " << fTimeWeightsHiGain[i] << endl;
     647
     648  *fLog << endl;
     649  *fLog << inf << "Used Low Gain weights in the extractor: " << endl;
     650  for (Int_t i=0;i<fBinningResolutionLoGain*fWindowSizeLoGain; i++)
     651    *fLog << inf << fAmpWeightsLoGain[i] << "   " << fTimeWeightsLoGain[i] << endl;
     652 
     653  return kTRUE;
     654}
Note: See TracChangeset for help on using the changeset viewer.