import petaloAnalysis
from ROOT import *
import numpy as np
import pandas as pd
from array import array

from pymongo import MongoClient

#map(processData, [(vel,100,0,1500,0,0) for vel in xrange(150,250,5)])
#connect to db
client = MongoClient('127.0.0.1', 27017)
db = client['petalo']
collection = db['cherenkov']

#old version -> compute here
#petalo = petaloAnalysis.PetaloAnalysis('sensors.csv','events.csv')
#petalo.processWaveform(0,1500,1,0,0)

c1 = TCanvas("c1","multipads",900,700);

velocities = []
sigmas = []
errors = []

for vel in xrange(150,250,5):
#    result = petalo.computeDTOF(vel)
    result = collection.find_one({'vel': vel, 'pde': 100, 'minwl': 0, 'maxwl': 1500, 'asic': 0, 'sptr': 0})
        
    gStyle.SetOptStat(1110)
    gStyle.SetOptFit(1)
    h1 = TH1F("h1", "PETALO", 80,-100,100)
    for n in result['dtof']:
        h1.Fill(n)
    
    gauF1 = TF1("gauF1","gaus",-200,200)
    h1.Fit("gauF1","","e",-15,15);
    h1.Draw()
    c1.Print('plots/velocity/' + str(vel) + '.png')

    sigma = h1.GetFunction("gauF1").GetParameter(2)
    error = h1.GetFunction("gauF1").GetParError(2)
    sigmas.append(sigma)
    errors.append(error)
    velocities.append(vel)


velocities = array('f', velocities)
sigmas = array('f', sigmas)
errors = array('f', errors)
zeros = array('f', np.zeros(len(velocities)))

    
c2 = TCanvas("c2","multipads",900,700);
velocity = TGraphErrors(len(velocities), velocities, sigmas,zeros, errors)
velocity.Draw()
c2.Print('plots/velocity/velocities.png')
