Esempio n. 1
0
def runAndPlotOptFilter(event, pulse, pta=None, **kwargs):  

  cham = kwargs['cham']
  ionpt = kwargs.get('ionpulsestarttime', 0)

  if pulse.GetChannelName() not in kwargs['chanlist']: return

  print pulse.GetChannelName()

  optkamper = cham.GetOptimalKamper(pulse)
  thewindow = optkamper.GetWindow()
  #print thewindow.GetWindowSize()
  #print thewindow.GetOutputPulseSize()
  preproc = optkamper.GetPreProcessor()
  #print preproc.GetInputPulseSize(), preproc.GetOutputPulseSize()
  #print pulse.GetPulseLength()

  if kwargs.get('runCalculation', True):      
    if pulse.GetChannelName().startswith('chal'):
      optkamper.SetIonPulseStartTime(ionpt);  
    optkamper.MakeKamp(pulse)

  #print thewindow.GetWindowSize()
  #print thewindow.GetOutputPulseSize()
  #print preproc.GetInputPulseSize(), preproc.GetOutputPulseSize()
  #print pulse.GetPulseLength()

  resultsMap = optkamper.GetResults()
  for key, val in resultsMap:
    print key, val.fValue


  rawpulse = np.array(pulse.GetTrace())
  thewindow = optkamper.GetWindow()
  windowpulse = kutil.get_as_nparray(thewindow.GetWindow(), thewindow.GetWindowSize() )
  windowoutput = kutil.get_out( thewindow )

  optfilter = optkamper.GetOptimalFilter()

  noisepower = kutil.get_as_nparray(optfilter.GetNoiseSpectrum(), optfilter.GetNoiseSpectrumSize())
  templatepower = kutil.get_as_nparray(optfilter.GetTemplatePower(), optfilter.GetTemplatePowerSize())

  hc2p = ROOT.KHalfComplexPower()
  hc2p.SetInputPulse( optfilter.GetOptimalFilter(), optfilter.GetOptimalFilterSize())
  hc2p.RunProcess()
  optfilpower = kutil.get_out(hc2p)

  hc2p.SetInputPulse(optfilter.GetInputPulse(), optfilter.GetInputPulseSize())
  hc2p.RunProcess()
  thispulsepower = kutil.get_out( hc2p)

  hc2p.SetInputPulse( optfilter.GetOptFilterAndSignal(), optfilter.GetOptFilterAndSignalSize())
  hc2p.RunProcess()
  optfilter_andpulsepower = kutil.get_out(hc2p)

  ampestimator = kutil.get_out( optfilter)


  res = scipy.optimize.minimize(optkamper.Chi2Functor, [np.abs(windowpulse[ampestimator.argmax()]), ampestimator.argmax()], method='Nelder-Mead')
  chi2_min_amp = res.x[0]
  chi2_min_time = res.x[1]
  print res

  print 'chi2 start time after minimization', chi2_min_time
  print 'chi2 amp after minimization', chi2_min_amp
  print 'chi2 at mins', optfilter.GetChiSquared(chi2_min_amp, chi2_min_time)
  print 'amplitude theoretical variance', 1./optfilter.GetAmpEstimatorDenominator()

  print 'kdata: calcullate chi2 versus time at amplitude', chi2_min_amp
  stepsize = 0.01
  timearray = np.array([atime*stepsize for atime in range( int(pulse.GetPulseLength()/stepsize)) ])
  chi2_time = np.array([optfilter.GetChiSquared(chi2_min_amp, time) for time in timearray])
  
  print 'kdata: calcullate chi2 versus amplitude at time', chi2_min_time
  stepsize = 0.01
  amps = np.array([a*stepsize for a in range( 0, int(int(2*ampestimator.max())/stepsize)) ])
  chi2_amp = np.array([ optfilter.GetChiSquared(a_i, chi2_min_time) for a_i in amps ])

  print 'chi2 amp from scan', chi2_amp.argmin()*stepsize
  print 'chi2 start time from scan', chi2_time.argmin()

  


  theFig = plt.figure( kwargs.get('figure', 1))
  theWidth = 1

  plt.axis('off')

  axes  = plt.subplot(6,1,1)
  plt.cla()
  
  plt.plot(rawpulse, linewidth=theWidth)
  plt.plot(windowoutput, linewidth=theWidth)
  if kwargs.has_key('template'):
    scalefactor = -1*ampestimator.min()
    if ampestimator.max() > scalefactor:
      scalefactor = ampestimator.max()
    plt.plot( np.array(kwargs['template'])* scalefactor)

  axes = plt.subplot(6,1,2)

  plt.cla()
  plt.loglog(noisepower, linewidth=theWidth)
  plt.loglog(templatepower, linewidth=theWidth)
  plt.loglog(optfilpower, linewidth=theWidth)


  axes = plt.subplot(6,1,3)

  plt.cla()
  plt.loglog(thispulsepower, linewidth=theWidth)
  plt.loglog(optfilter_andpulsepower, linewidth=theWidth)


  axes = plt.subplot(6,1,4)

  plt.cla()
  plt.plot(ampestimator, linewidth=theWidth)


  axes = plt.subplot(6,1,5)

  plt.cla()
  plt.plot( amps, chi2_amp, linewidth=theWidth)


  axes = plt.subplot(6,1,6)

  plt.cla()
  plt.plot(timearray, chi2_time, linewidth=theWidth)

  plt.show()
  if kwargs.get('wait', True):
    raw_input()
Esempio n. 2
0
  #now loop through the data again, applying the optimal filter that was just built.
  #for i in range(f.GetEntries()):
    
   # f.GetEntry(i) 
  
  for ii in range(e.GetNumBoloPulses()):
      
    p = e.GetBoloPulse(ii)  #returns a KRawBoloPulseRecord
    
    if p.GetPulseLength() == 0: continue  #skip empty pulses
    
    if p.GetChannelName() == myChannel:    
      bas.SetInputPulse( p.GetTrace() )
      y=bas.RunProcess()
     #plt.subplot(3,3,ii+1)
      x =  kutil.get_out(bas)
      
      coeffs = []
      for i in range(lvl):
	
        coeffs.append(pywt.wavedec(x, 'db2', level = i+1))
      
      
        if max(abs(coeffs[i][0])) > 2*max(abs(coeffs[i][1])):
	  print 'for level', i+1, ':true'
        else:
	  print 'for level', i+1, ':false'
	
        plt.figure(2)
        plt.subplot(lvl+1,2,1)
        plt.plot(x)
Esempio n. 3
0
tree.define_collection(name='pulsesy',
                       prefix='c1y_',
                       size='c1y_n',
                       mix=Pulse)

tree.define_collection(name='pulsesx',
                       prefix='c1x_',
                       size='c1x_n',
                       mix=Pulse)

# loop over "events" in tree
for event in tree:
  print "%d" % event.date
  
  bas.SetInputPulse( event.c1y_val )
  bas.RunProcess()
  pulsey = util.get_out(bas)
  #plt.plot( np.array(event.c1x_val), util.get_out(bas) )
  amp.Fill( np.argmin(pulsey) ) 
     
  #raw_input()
  #plt.cla()

f.close()
amp.Draw()
results.cd()
amp.Write()
raw_input()


Esempio n. 4
0
def kamp(p, pta, **kwargs):
  
  f  = kwargs["kdatafile"]
  channels = kwargs["channellist"]
  cham = kwargs["chamonix"]
  hc2p = kwargs["halfcomp2power"]
  r2hc = kwargs["real2halfcomp"]
  pulsePol = kwargs['pulsePol']
  pulseLength, binSize, minSearch, maxSearch, _pulsetype = getPulseInfo(p.GetChannelName())

  db = kwargs['templateDB']

  if p.GetChannelName() not in channels: 
    return None
          
  print 'Entry', f.GetCurrentEntryNumber(), p.GetChannelName()
        
        
  pulse = p.GetTrace()
        
  tempDft = cham.GetTemplateSpectrum(p.GetChannelName())
  noisePower = cham.GetNoisePower(p.GetChannelName())
  #print noisePower.size()
  #print noisePower[noisePower.size()-1]
  print 'number of noise events found', cham.GetNumNoiseEventsFound(p.GetChannelName())
  optKamper = cham.GetOptimalKamper()
  optFilter = optKamper.GetOptimalFilter()
  optFilter.SetTemplateDFT(tempDft)
  optFilter.SetNoiseSpectrum(noisePower)
  optFilter.SetToRecalculate()
  #optFilter.BuildFilter()
  
  if p.GetIsHeatPulse():
    optKamper.SetWindow(cham.GetHeatWindow())
    optKamper.SetPreProcessor(cham.GetHeatPreProcessor())
    win = cham.GetHeatWindow()
    preproc= cham.GetHeatPreProcessor()

  elif p.GetBoloBoxVersion() > 1.9:
    optKamper.SetWindow(cham.GetIonWindow())
    optKamper.SetPreProcessor(cham.GetBBv2IonPreProcessor())
    win = cham.GetIonWindow()
    preproc= cham.GetBBv2IonPreProcessor()
  else:
    optKamper.SetWindow(cham.GetIonWindow())
    optKamper.SetPreProcessor(cham.GetBBv1IonPreProcessor())
    win = cham.GetIonWindow()
    preproc= cham.GetBBv1IonPreProcessor()

  optKamper.SetPulseTemplateShiftFromPreTrigger( cham.GetTemplateShift(p.GetChannelName()) );
  optKamper.SetAmplitudeEstimatorSearchRangeMax(maxSearch)
  optKamper.SetAmplitudeEstimatorSearchRangeMin(minSearch)

  plt.figure(1)
  plt.subplot(7,1,1)
  plt.cla()
  plt.plot(np.array(pulse))
  plt.title('raw')
  
          
  
  plt.subplot(7,1,2)
  plt.cla()
  plt.plot( kutil.get_as_nparray(win.GetWindow(), win.GetWindowSize()) )
  plt.title('window')
  
  #winpulse = std.vector("double")()
  #winpulse.reserve(win.GetOutputPulseSize())
  #for i in range(winpulse.size()):
  #  winpulse[i] = win.GetOutputPulse()[i]
  
  preproc.SetInputPulse(pulse)
  preproc.RunProcess()
  win.SetInputPulse(preproc)
  win.RunProcess()
  winpulse = kutil.get_out(win)
  plt.subplot(7,1,3)
  plt.cla()
  plt.plot(np.array(winpulse))
  plt.title('baseline removed and windowed')
  # plt.subplot(4,1,2)
  #         plt.cla()
  #         plt.plot(np.array(tempDft))
  #         plt.title('tempDft')
  
  hc2p.SetInputPulse(tempDft)
  print 'template power', hc2p.RunProcess()
  tempPower = kutil.get_out(hc2p)


  plt.subplot(7,1,4)
  plt.cla()
  npp = np.array(noisePower)

  plt.loglog(npp)
  plt.loglog(tempPower)

  #raw_input('hit enter to run filter....')

  #wait, doesn't the optKamper do this?
  #r2hc.SetInputPulse(win)
  #r2hc.RunProcess()
  #optFilter.SetInputPulse(r2hc)
  #optFilter.BuildFilter()

  optFilterResults = optKamper.MakeKamp(p)
  
  kernel = kutil.get_as_nparray(optFilter.GetOptimalFilter(), optFilter.GetOptimalFilterSize())

  hc2p.SetInputPulse(kernel, len(kernel))
  hc2p.RunProcess()
  kernelPower = kutil.get_out(hc2p)
  
  plt.loglog(kernelPower)
  plt.title('filter power')
  
  hc2p.SetInputPulse(optFilter.GetInputPulse(), optFilter.GetInputPulseSize())
  hc2p.RunProcess()
  pulsePower = kutil.get_out(hc2p)
      
  plt.subplot(7,1,5)
  plt.cla()
  plt.loglog(pulsePower)
  plt.title('pulse power')
  
  optOut = kutil.get_out(optFilter)
  
  plt.subplot(7,1,6)
  plt.cla()
  plt.plot(optOut)
  plt.title('amplitude estimator')
  
  #calculate chi^2 as a function of t and see what it looks like.
  chi2 = std.vector("double")()
  for time in range(pulse.size()):  #loop over time bins
    chi2.push_back(optFilter.GetChiSquared(time))
  
  plt.subplot(7,1,7)
  plt.cla()
  plt.plot(np.array(chi2))
  plt.title('chi squared')
  plt.show()
  #raw_input()
  
  plt.figure(2)
  #plt.subplot(1,1,1)
  plt.cla()
    
  plt.plot(winpulse)

  vr = db.view('analytical/bychandate',reduce=False, descending=True, startkey=[p.GetChannelName(), "2012-01-22 00:00:00.0"], limit=1, include_docs=True)
  doc = vr.first()['doc']
  vp = std.vector("double")()
  vp.reserve(pulseLength)
  exec(doc['formula']['python']) #defines 'template' function
  #doc['formula']['par'][2] = doc['formula']['par'][2]/2.016
  #doc['formula']['par'][3] = doc['formula']['par'][3]/2.016
  #doc['formula']['par'][5] = doc['formula']['par'][5]/2.016
  #doc['formula']['par'][0]=300  #put it close to zero, but away from the windowing function
  for i in range( pulseLength ):
    vp.push_back( template(i*binSize, doc['formula']['par']))
    
  #plot the pulse templates for documentation
  if p.GetChannelName().startswith('chal'): thePolarity = 1 #template heat pulses are already negative polarity
  else: thePolarity = pulsePol.GetExpectedPolarity(p)
  scaleFactor = thePolarity*max(abs(np.array(winpulse)))/max(abs(np.array(vp)))
  optScaleFactor = thePolarity*optFilterResults['amp'].fValue/max(abs(np.array(vp)))
  print 'simple scaling by', scaleFactor
  print 'opt filter scaling by at amp', optScaleFactor
  print vp.size(), vp[i], vp[vp.size()-1], vp[i]*scaleFactor
  optPulse = std.vector("double")()
  optPulse.resize(vp.size())
  diffPulse = std.vector("double")()
  diffPulse.resize(vp.size())

  for i in range(vp.size()):
    optPulse[i] = optScaleFactor * vp[i]
    vp[i] = scaleFactor * vp[i]
    diffPulse[i] = optPulse[i] - vp[i]

  plt.plot(np.array(vp))
  plt.plot(np.array(optPulse))
  #plt.plot(np.array(diffPulse))
  
  print 'optimal filter results'
  for key, val in optFilterResults:
    print key, val.fValue, val.fUnit
  raw_input()