"""
How to run it:
python scriptROOTForum.py -p PTLegPol EtaLegPol nTracksLegPol -n 3 
"""

import sys
import argparse
from scipy.special import eval_legendre

import ROOT
from ROOT import TCanvas, gStyle, TFormula
from ROOT import RooFit, RooRealVar, RooFormulaVar, RooArgList, RooArgSet, RooLegendre, RooAbsPdf, RooAbsReal, RooAddPdf, RooProduct, RooGenericPdf, RooMsgService, RooConstVar, RooClassFactory, RooWorkspace
from itertools import repeat
import itertools as it
import csv
import re
import sqlite3
import os.path
import numpy as np
from array import array 
import subprocess
import re
from sympy import *

if __name__ == "__main__" :

   minRange = -1.0
   maxRange =  1.0
   # Four observables to be considered
   x = RooRealVar('x','x',minRange,maxRange) 
   y = RooRealVar('y','y',minRange,maxRange) 

   # Full "PDF" 
   _simplifiedExpression = '1.0*x*y'

   # Add the individual analytical integrals calculated with respect to the corresponding variable
   # Not that these expressions are already evaluated at the maximum (1.0) and minim (-1.0)
   _simplifiedIntDict = {}
   _simplifiedIntDict['x'] = '0.5*pow(x.max(rangeName),2)*y - 0.5*pow(x.min(rangeName),2)*y'
   _simplifiedIntDict['y'] = '0.5*x*pow(y.max(rangeName),2) - 0.5*x*pow(y.min(rangeName),2)' 

   # Combining all the partial integrals into the desired form
   analyticalIntegral = "x:"+_simplifiedIntDict['x']+",y:"+_simplifiedIntDict['y']

   # PDF 
   fitLegPol = ROOT.RooClassFactory.makePdfInstance("CombLeg", str(_simplifiedExpression), RooArgList(x,y), analyticalIntegral) 

