Newer
Older
STPerformancePaper / TupToHist.py
@Andrea Mauri Andrea Mauri on 25 Oct 2017 10 KB Start: put necessary file to run TupToHist
import os
import inspect
from pprint import pprint
from itertools import product
import math
import numpy as n
import sys
import pickle
from datetime import datetime
from suppl.Structure import *
from drawing.CreateTTHist import CreateTTHist
from drawing.CreateITHist import CreateITHist
from drawing.Create_Maps import TT_Map as TT_Map_func
from drawing.Create_Maps import IT_Map as IT_Map_func
from config import binning
from config import bin_name
from config import perform_window_eff_study
from config import Number_Of_Events
from config import histogram_address
from config import plot_address
from config import pkl_address
from config import extra_name
from config import final_eff_window
import ROOT as R
from ROOT import gStyle
gStyle.SetOptStat(False)
#from ROOT import RooFit as RF

binning
bin_name

def TupToHist(data, oparation_mode, Number_Of_Events=Number_Of_Events, pkl_address=pkl_address, histogram_address=histogram_address, plot_address=plot_address):
    global perform_window_eff_study

    IT_Map=IT_Map_func()
    TT_Map=TT_Map_func()
    f_input = R.TFile(data)
    start = datetime.now()
    
    if (oparation_mode=='1'):      ##############    TT      ##############
        tree_TTHitEfficiency = f_input.TTHitEfficiency
        t_TTHitEfficiency = tree_TTHitEfficiency.Get("Tracks") #TrackMonTuple
        coll_TTHitEfficiency = create_coll(det = "TT", mode = "Efficiency")
        print "TT Efficiency"
        for i, s in enumerate(t_TTHitEfficiency):
            if i%100==0:
                cli_progress_test(i, t_TTHitEfficiency.GetEntries(), start)    
            if Number_Of_Events>0:
                if i>Number_Of_Events: continue       

            
            for run_bin in coll_TTHitEfficiency:
                #if ((s.RunNumber>=coll_TTHitEfficiency[run_bin]["run_start"]) and (s.RunNumber<=coll_TTHitEfficiency[run_bin]["run_stop"])):
                if (
                        ( ("BinByStrip" in bin_name) and ((abs(s.clusterStrip)>=coll_TTHitEfficiency[run_bin]["Strip_start"]) and (abs(s.clusterStrip)<=coll_TTHitEfficiency[run_bin]["Strip_stop"])) )
                        or
                        ( ("BinByY" in bin_name) and ((abs(s.expected_y)>=coll_TTHitEfficiency[run_bin]["Y_start"]) and (abs(s.expected_y)<=coll_TTHitEfficiency[run_bin]["Y_stop"])) )
                        or
                        ( ("BinByPt" in bin_name) and ((s.pt>=coll_TTHitEfficiency[run_bin]["pT_start"]) and (s.pt<=coll_TTHitEfficiency[run_bin]["pT_stop"])) )
                        or
                        ( ("AllRuns" in bin_name or "BinByMonth" in bin_name or "BinByRunNumber" in bin_name) and ((s.RunNumber>=coll_TTHitEfficiency[run_bin]["run_start"]) and (s.RunNumber<=coll_TTHitEfficiency[run_bin]["run_stop"])) )
                ) :
                    if s.isExpected:  # expected hit
                        coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["nbExpected"]+=s.isExpected
                        if s.isFound:
                            coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["residual"].Fill(s.hit_residual)
                            if abs(s.hit_residual) < final_eff_window:
                                coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["nbFound"]+=s.isFound
                                coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["clusterSize"].Fill(s.clusterSize)
                                coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["SNratio"].Fill(s.clusterCharge/s.clusterNoise)
                            
                    else :  # hit is noise
                        coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["residual_noise"].Fill(s.hit_residual)
                        if abs(s.hit_residual) < final_eff_window:
                            coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["nbFoundNoise"]+=s.isFound
                
                                        
        coll_TTHitEfficiency = find_efficiency(coll_TTHitEfficiency)
        coll_TTHitEfficiency = find_noise_fraction(coll_TTHitEfficiency)
        if perform_window_eff_study:
            coll_TTHitEfficiency = window_eff_study(coll_TTHitEfficiency)
            write_window_eff_study(coll_TTHitEfficiency, "Efficiency", histogram_address+"TTHitEfficiency"+"_"+bin_name+"_efficiency_study_")
        write_histogram(coll_TTHitEfficiency, "Efficiency",histogram_address+"TTHitEfficiency"+"__"+bin_name+"_")        
        coll_TTHitEfficiency = make_coll_lite(coll = coll_TTHitEfficiency, det = "TT", mode = "Efficiency")
        for run_bin in coll_TTHitEfficiency:
            try:
                suffix = coll_TTHitEfficiency[run_bin]["comment"]
            except:
                suffix = str(coll_TTHitEfficiency[run_bin]["run_start"])+"__"+str(coll_TTHitEfficiency[run_bin]["run_stop"])
            # These are the plots with the map of the TT/IT
            if "AllRuns" in bin_name:
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable  ="occupancy", mode="Value",suffix=suffix,address=plot_address)
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable  ="efficiency", mode="Value",suffix=suffix,address=plot_address)
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable = "mean", mode="Value",suffix= suffix,address=plot_address)
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable = "width", mode="Value",suffix=suffix,address=plot_address)   
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable = "clusterSize_mean", mode="Value",suffix=suffix,address=plot_address)      
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable = "SNratio_max", mode="Value",suffix=suffix,address=plot_address)    
                CreateTTHist(coll_TTHitEfficiency[run_bin]["data"],variable = "noise_fraction", mode="Value",suffix=suffix,address=plot_address)   
            
        with open(pkl_address+'TTHitEfficiency_'+bin_name+extra_name+'.pkl', 'wb') as basket:
            pickle.dump(coll_TTHitEfficiency, basket)        
        create_efficiency_trends(coll_TTHitEfficiency, "TT",histogram_address+"Trends_TTHitEfficiency"+"__"+bin_name+"_")
    
    elif (oparation_mode=='2'):     ##############    IT      ##############
        tree_ITHitEfficiency = f_input.ITHitEfficiency
        t_ITHitEfficiency = tree_ITHitEfficiency.Get("Tracks") #TrackMonTuple
        coll_ITHitEfficiency = create_coll(det = "IT", mode = "Efficiency")    
        print "IT Efficiency"
        for i, s in enumerate(t_ITHitEfficiency):
            if i%100==0:
                cli_progress_test(i, t_ITHitEfficiency.GetEntries(), start)    
            if Number_Of_Events>0:
                if i>Number_Of_Events: continue
                
            for run_bin in coll_ITHitEfficiency:
                #if ((s.RunNumber>=coll_ITHitEfficiency[run_bin]["run_start"]) and (s.RunNumber<=coll_ITHitEfficiency[run_bin]["run_stop"])):
                if (
                        (("BinByStrip" in bin_name) and ((abs(s.clusterStrip)>=coll_ITHitEfficiency[run_bin]["Strip_start"]) and (abs(s.clusterStrip)<=coll_ITHitEfficiency[run_bin]["Strip_stop"])) )
                        or
                        (("BinByY" in bin_name) and ((abs(s.expected_y)>=coll_ITHitEfficiency[run_bin]["Y_start"]) and (abs(s.expected_y)<=coll_ITHitEfficiency[run_bin]["Y_stop"])) )
                        or
                        (("BinByPt" in bin_name) and ((s.pt>=coll_ITHitEfficiency[run_bin]["pT_start"]) and (s.pt<=coll_ITHitEfficiency[run_bin]["pT_stop"])))
                        or
                        (("AllRuns" in bin_name or "BinByMonth" in bin_name or "BinByRunNumber" in bin_name) and ((s.RunNumber>=coll_ITHitEfficiency[run_bin]["run_start"]) and (s.RunNumber<=coll_ITHitEfficiency[run_bin]["run_stop"])))
                ) :
                    if s.isFound:
                        coll_ITHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["residual"].Fill(s.hit_residual)
                    #coll_ITHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["nbFound"]+=s.isFound                    
                    if abs(s.hit_residual) < final_eff_window:
                        coll_TTHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["nbFound"]+=s.isFound
                    coll_ITHitEfficiency[run_bin]["data"][s.clusterSTchanMapID]["nbExpected"]+=s.isExpected
                    
        coll_ITHitEfficiency = find_efficiency(coll_ITHitEfficiency)
        if perform_window_eff_study:
            coll_ITHitEfficiency = window_eff_study(coll_ITHitEfficiency)
            write_window_eff_study(coll_ITHitEfficiency, "Efficiency", histogram_address+"ITHitEfficiency"+"_"+bin_name+"_efficiency_study_")
        write_histogram(coll_ITHitEfficiency, "Efficiency",histogram_address+"ITHitEfficiency"+"__"+bin_name+"_")        
        coll_ITHitEfficiency = make_coll_lite(coll = coll_ITHitEfficiency, det = "IT", mode = "Efficiency")    
        for run_bin in coll_ITHitEfficiency:
            try:
                suffix = coll_ITHitEfficiency[run_bin]["comment"]
            except:
                suffix = str(coll_ITHitEfficiency[run_bin]["run_start"])+"__"+str(coll_ITHitEfficiency[run_bin]["run_stop"])
            CreateITHist(coll_ITHitEfficiency[run_bin]["data"],variable="efficiency", mode="Value",suffix=suffix, address=plot_address)
            CreateITHist(coll_ITHitEfficiency[run_bin]["data"],variable = "mean", mode="Value",suffix= suffix,address=plot_address)
            CreateITHist(coll_ITHitEfficiency[run_bin]["data"],variable = "width", mode="Value",suffix=suffix,address=plot_address)   
        with open(pkl_address+'ITHitEfficiency_'+bin_name+extra_name+'.pkl', 'wb') as basket:
            pickle.dump(coll_ITHitEfficiency, basket)                
        create_efficiency_trends(coll_ITHitEfficiency, "IT",histogram_address+"Trends_ITHitEfficiency"+"__"+bin_name+"_")
    
    else:
        syntax_explanation("TupToHist.py")

    return True

if __name__ == "__main__":
    if len(sys.argv)==3:
        data = sys.argv[1]
        TupToHist(data,sys.argv[2])
    else:
        syntax_explanation("TupToHist.py")