Module_path = basepath + '/Modules/' + MODULE_name + '/'
sys.path.append(Module_path)
#print('We are working from here' + os.getcwd())
'''
---------------------------------------------------- Import packages and Framework functions ----------------------------------------------------------------------------------------------------
'''
import numpy as np
import root_numpy as rn
import Framework.read_and_save.read_and_save as RAS
from Plotter.Plotter import Plotter
'''
---------------------------------------------- (Option 1) Open and read .hdf5 original file like a hdf5 object ----------------------------------------------------------------------------------
'''
hdf5_folder_path = basepath + 'Data_hdf5/'  #The folder with files after the calibrations comming from RootA transformed in hdf5
hdf5_file_name = 'Analysis_TEST_file'  #Without .hdf5 extension
Testing_data_hdf5 = RAS.Read_hdf5_file(
    hdf5_folder_path, hdf5_file_name)  #Array-matrix with our data
'''
---------------------------------------------- (Option 2) Open and read .root original file like ndarray object ---------------------------------------------------------------------------------
'''
################################## One can work directly with the root files converting it to ndarray object. This is an example:
#Path_to_tree = '/Users/dani/Dani/FISICA/INVESTIGACION/DOCTORADO/TESIS_NUCLEAR/e753/Analysis_Dani/RootA/'
#Tree_name = ['r0092_000a'] #More than one tree is allowed
#Branches = 'All' #Or do a selection over concrete braches
#Testing_data_ndarray = RAS.Read_Root_file(Path_to_tree, Tree_name, Branches, Tree_selection = None)
'''
----------------------------------------------------------- Do some analysis here ---------------------------------------------------------------------------------------------------------------
'''
################################################# hdf5 object example:
#variable_array = Testing_data_hdf5['Brho'][()]
#Testing_data_hdf5['Brho']['Brho'>75]
'''
import numpy as np
import Framework.read_and_save.read_and_save as RAS
from Plotter.Plotter import Plotter
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.inset_locator import (inset_axes, InsetPosition,
                                                  mark_inset)
'''
---------------------------------------------- Open and read .hdf5 original file like a hdf5 object ----------------------------------------------------------------------------------
'''
hdf5_folder_path = basepath + 'Data_hdf5/' + MODULE_name + '_run/'  #The folder with files after the calibrations comming from RootA transformed in hdf5
file_14_degrees = 'Analysis_14_file_ChargeStates_variables'  #Without .hdf5 extension
file_21_degrees = 'Analysis_21_file_ChargeStates_variables'  #Without .hdf5 extension
file_14_21_degrees = 'Analysis_14+21_file_ChargeStates_variables'  #Without .hdf5 extension

data_14_degrees = RAS.Read_hdf5_file(
    hdf5_folder_path, file_14_degrees)  #Array-matrix with our data
data_21_degrees = RAS.Read_hdf5_file(
    hdf5_folder_path, file_21_degrees)  #Array-matrix with our data
data_14_21_degrees = RAS.Read_hdf5_file(
    hdf5_folder_path, file_14_21_degrees)  #Array-matrix with our data

#Variables aka branches are: ['Z', 'Zi', 'M', 'M_Q', 'Mri', 'Mr', 'Q', 'Qi'] ---> data_14_degrees.keys()

M_14 = data_14_degrees['M']
M_Q_14 = data_14_degrees['M_Q']
Z_14 = data_14_degrees['Z']
Zi_Q_14 = data_14_degrees['Zi']
M_21 = data_21_degrees['M']
M_Q_21 = data_21_degrees['M_Q']
Z_14 = data_14_degrees['Z']
Zi_Q_14 = data_14_degrees['Zi']
data_21 = data_runs_21[condition_21]

############################################# Save ALL variables once cut it
Group_name = 'All'  #write "All" for all the variables.
RAS.Save_hdf5_or_ndarray_object_as_hdf5(Path_to_tree, ['data_runs_14'],
                                        Group_name,
                                        data_14)  #put directly the data array
RAS.Save_hdf5_or_ndarray_object_as_hdf5(Path_to_tree, ['data_runs_21'],
                                        Group_name,
                                        data_21)  #put directly the data array
'''
------------------------------------------------------------------------- Analysis ---------------------------------------------------------------------------------------------------------------
'''

################################################ 14 degrees ################################################################
data_hdf5_14 = RAS.Read_hdf5_file(Path_to_tree, 'data_runs_14')

M_vs_M_Q_14 = Plotter([data_hdf5_14['M_Q'], data_hdf5_14['M']
                       ])  #Create the base with the variables in a object
M_vs_M_Q_14.SetFigSize(12, 7)
M_vs_M_Q_14.SetBinX(50)
M_vs_M_Q_14.SetBinY(50)
M_vs_M_Q_14.SetFigTitle(r'run97 M vs M_Q    14$\degree$', 20)
M_vs_M_Q_14.SetLabelX('M_Q', 20)
M_vs_M_Q_14.SetLabelY('M', 20)
M_vs_M_Q_14.SetSizeTicksX(10)
M_vs_M_Q_14.SetBoxText(
    'Selection:\n Z>0 \n Zi>0 \n 70<M<180 \n 2.25<M_Q<4.0 \n -100<Pf<100 \n -110<Yf<50 \n  Xf>-1500 \n MW_Nr>=0'
)
M_vs_M_Q_14.Histo_2D()  #Draw it
######### Save and show the created figure