def __init__(self):
        """Initialize whatever is needed"""
        self.my_logger = Logger()
        #self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
        #}
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #initialize RooFit
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        self.output_filename = 'worskapce_with_embedded_toys.root'
예제 #2
0
  def __init__(self,name = "plotters_base_functionality" ):
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.name = name
        #ROOT.gSystem.AddIncludePath("-I$ROOFITSYS/include/");
        #ROOT.gROOT.ProcessLine(".L tdrstyle.cc")
        #from ROOT import setTDRStyle
        #ROOT.setTDRStyle(True)

        #ROOT.gStyle.SetPalette(1)
        #ROOT.gStyle.SetOptStat(0)
        self.copy_to_web_dir = False
        self.webdir = ""
        self.save_extensions = ['png','pdf','eps']
    def __init__(self, datacard_name, datacard_input):
        self.my_logger = Logger()
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #

        self.datacard_name = datacard_name
        self.d_input = datacard_input
        self.log.debug('Datacard: {0} Datacard input: {1}'.format(self.datacard_name, self.d_input))

        #self.not_a_process = ['observation','functions_and_definitions', 'setup']
        self.not_a_process = self.d_input['setup']['reserved_sections']
        self.lumi_scaling = 1.0

        #process lists
        self.signal_process_list = self._get_processes('signal')
        self.bkg_process_list = self._get_processes('background')
        self.process_list = self.signal_process_list+self.bkg_process_list

        self.log.debug('Processes: {0}'.format(self.process_list))

        #self.n_systematics, self.systematics_lines = self._get_systematics_lines()
        self.card_header='' #set of information lines os a header of the card.
예제 #4
0
 def __init__(self, run_data_name, run_dict):
     """Set the values from configuration"""
     
     self.log = Logger().getLogger(self.__class__.__name__, 10)
     self.run_dict = run_dict
     self.create_cards_dir="/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/"
     self.version= "v1"
     self.sqrts_dc="8TeV"
     self.sqrts_plot="14"
     self.fs="2e2mu"
     self.lumi="19.79"
     self.lumi_zfill="19.79"
     self.discriminant="D_{0-}(90^{o})"
     self.MY_CUR_WWW_SUBDIR=""
     self.append_name_base = run_data_name
     self.MY_CUR_WWW_SUBDIR = self.append_name_base
     self.POIs = list(self.run_dict['POI'])
     self.poi = self.run_dict['POI_setup']
     self.termNames =  self.run_dict['termNames']
     self.templatedir = self.run_dict['templatedir']
     self.discriminant = self.run_dict['discriminant_name']
     self.additional_options = self.run_dict['additional_options']
     self.fs  = self.run_dict['final_state']
     self.www_dir = "/afs/cern.ch/user/r/roko/www/html/Geoloc/{0}".format(self.MY_CUR_WWW_SUBDIR)
     self.do_copy_to_webdir = True
     print "Setting web directory to: {0}".format(self.www_dir)
     self.do_cmd = ""
     #self.poi= {"k2k1_ratio":{"switch":0, "range":"-100,100", "name":"k2k1", "nice_name":"k_{2}/k_{1}", "value":"0"}, 
           #"k3k1_ratio":{"switch":0, "range":"-100,100", "name":"k3k1", "nice_name":"k_{3}/k_{1}", "value":"0"}}
     self.setup_POI_related_stuff()
    def __init__(self):
        """Initialize whatever is needed"""
        self.my_logger = Logger()
        #self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
                        #}
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #initialize RooFit
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        self.output_filename = 'worskapce_with_embedded_toys.root'
예제 #6
0
 def __init__(self,name = "templates" ):
     self.log = Logger().getLogger(self.__class__.__name__, 10)
     self.name = name
     #ROOT.gSystem.AddIncludePath("-I$ROOFITSYS/include/");
     ROOT.gROOT.ProcessLine(".L tdrstyle.cc")
     from ROOT import setTDRStyle
     ROOT.setTDRStyle(True)
     
     ROOT.gStyle.SetPalette(1)
     ROOT.gStyle.SetOptStat(0)
     self.copy_to_web_dir = False
     self.webdir = ""
     self.pp = pprint.PrettyPrinter(indent=4)
     self.k2k1_ratio=0
     self.k3k1_ratio=0
     self.total_pdf=0
class DatacardBuilder(object):
    """
    Class for building datacards, both textual and workspace part

    EXAMPLE_____________________________________________________________
    #*** HEADER ***
    imax 1 number of bins
    jmax 5 number of processes minus 1
    kmax 14 number of nuisance parameters
    ----------------------------------------------------------------------------------------------------------------------------------
    shapes *    ch1  hzz4l_2e2muS_8TeV_xs_SM_125_mass4l_v3.Databin0.root w:$PROCESS
    ----------------------------------------------------------------------------------------------------------------------------------
    bin          ch1
    observation  8.0
    #***PER-PROCESS INFORMATION ***
    ----------------------------------------------------------------------------------------------------------------------------------
    bin                                   ch1               ch1               ch1               ch1               ch1               ch1
    process                               trueH2e2muBin0_8  bkg_zjets_8       bkg_ggzz_8        bkg_qqzz_8        out_trueH_8       fakeH_8
    process                               0                 1                 2                 3                 4                 5
    rate                                  1.0000            1.0526            0.3174            5.7443            1.0000            0.5684
    ----------------------------------------------------------------------------------------------------------------------------------
    CMS_eff_e               lnN           1.046             -                 1.046             1.046             1.046             1.046

    EXAMPLE_____________________________________________________________

    """
    def __init__(self, datacard_name, datacard_input):
        self.my_logger = Logger()
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #

        self.datacard_name = datacard_name
        self.d_input = datacard_input
        self.log.debug('Datacard: {0} Datacard input: {1}'.format(self.datacard_name, self.d_input))

        #self.not_a_process = ['observation','functions_and_definitions', 'setup']
        self.not_a_process = self.d_input['setup']['reserved_sections']
        self.lumi_scaling = 1.0

        #process lists
        self.signal_process_list = self._get_processes('signal')
        self.bkg_process_list = self._get_processes('background')
        self.process_list = self.signal_process_list+self.bkg_process_list

        self.log.debug('Processes: {0}'.format(self.process_list))

        #self.n_systematics, self.systematics_lines = self._get_systematics_lines()
        self.card_header='' #set of information lines os a header of the card.

    def make_txt_card(self):
        """Make text part of the datacard and dump to a file.
            - loop on processes and fill in txt card lines
        """
        self.process_lines = self._get_process_lines()
        self.n_systematics, self.systematics_lines = self._get_systematics_lines()

        txt_card = """
                    Datacard for event category: {cat}
                    {card_header}

                    ---------------------------------------
                    imax 1 number of bins
                    jmax {jmax} number of processes minus 1
                    kmax {kmax} number of nuisance parameters
                    ---------------------------------------
                    {shapes_line}
                    ---------------------------------------
                    bin          cat_{cat}
                    observation  {n_observed}
                    ---------------------------------------
                    bin          {process_cat}
                    process      {process_name}
                    process      {process_number}
                    rate         {process_rate}
                    ---------------------------------------
                   """.format(cat = self.datacard_name,
                              jmax = (len(self.process_list)-1),
                              kmax = self.n_systematics,
                              shapes_line = self._get_shapes_line(),
                              n_observed = self._get_observation(),
                              process_cat = self.process_lines['bin'],
                              process_name = self.process_lines['name'],
                              process_number = self.process_lines['number'],
                              process_rate = self.process_lines['rate'],
                              #process_systematics = self.systematics_lines,
                              card_header = self.card_header
                              )

        txt_card = textwrap.dedent(txt_card)
        txt_card+= textwrap.dedent(self.systematics_lines)
        print txt_card
        file_datacard_name = self.datacard_name+'.txt'
        if self.lumi_scaling != 1.0:
            file_datacard_name = file_datacard_name.replace('.txt', '.lumi_scale_{0:3.2f}.txt'.format(self.lumi_scaling))

        with open(file_datacard_name, 'w') as file_datacard:
            file_datacard.write(textwrap.dedent(txt_card))
            file_datacard.write(textwrap.dedent(self.systematics_lines))
            self.log.info('Datacard saved: {0}'.format(file_datacard_name))



    def _get_shapes_line(self):
        """
        Gets the line with shape
        shapes *    {cat}  {cat}.root w:$PROCESS
        """
        self.shapes_exist = False
        for p in self.process_list:
            self.log.debug('Checking for shape in {0}/{1}'.format(self.datacard_name, p))
            try:
                self.d_input[p]['shape']
            except KeyError:
                pass
            else:
                if self.d_input[p]['shape']:
                    self.shapes_exist = True
                    self.shapes_output_file = "{0}.input.root".format(self.datacard_name)
                    if self.lumi_scaling != 1.0:
                        self.shapes_output_file = self.shapes_output_file.replace('input','lumi_scale_{0:3.2f}.input'.format(self.lumi_scaling))
                    break

        if self.shapes_exist:
            return "shapes *    cat_{cat}  {shapes_output_file} w:$PROCESS".format(cat = self.datacard_name, shapes_output_file = self.shapes_output_file)
        else:
            return "#shapes are not used - counting experiment card"



    def _get_processes(self, process_type='signal,background'):
        """Read the input dictionary and count processes.
        """
        sig_process_list = []
        bkg_process_list = []
        process_list=[]
        for p in self.d_input.keys():
            if p not in self.not_a_process:
                if self.d_input[p]['is_signal']:
                    sig_process_list.append(p)
                else:
                    bkg_process_list.append(p)

        if 'signal' in process_type.lower():
            process_list+=sorted(sig_process_list)
        if 'background' in process_type.lower():
            process_list+=sorted(bkg_process_list)

        return process_list


    def _get_process_lines(self):
        """
        Gets and formats lines coresponding to processes from the self.process_list
        """
        process_lines = {'bin': '', 'name':'', 'number':'', 'rate':'','sys':''}
        #get enumerates from signal and background processes
        #signal_process_list = []
        #bkg_process_list = []
        #for p in self.process_list:
            #if self.d_input[p]['is_signal']:
                #signal_process_list.append(p)
            #else:
                #bkg_process_list.append(p)

        #self.signal_process_list = sorted(signal_process_list)
        #self.bkg_process_list = sorted(bkg_process_list)

        signal_process_dict = dict(enumerate(self.signal_process_list, start=-(len(self.signal_process_list)-1)))
        bkg_process_dict    = dict(enumerate(self.bkg_process_list, start=1))


        #constructing the lines
        is_first = True
        for p_number in sorted(signal_process_dict.keys()):
            #delimiter = '\t\t'
            delimiter = ' '
            if is_first:
                delimiter = ''
                is_first = False
            p_name = signal_process_dict[p_number]
            process_lines['bin']    += ( delimiter + 'cat_' + str(self.datacard_name) )
            process_lines['name']   += ( delimiter + str(p_name) )
            process_lines['number'] += ( delimiter + str(p_number) )
            process_lines['rate']   += ( delimiter + str(float(self.d_input[p_name]['rate'])  * self.lumi_scaling) )
            process_lines['sys']     = "#systematics line: not implemented yet!!!"

        for p_number in sorted(bkg_process_dict.keys()):
            #delimiter = '\t\t'
            delimiter = ' '
            if is_first:
                delimiter = ''
                is_first = False
            p_name = bkg_process_dict[p_number]
            process_lines['bin']    += ( delimiter + 'cat_' + str(self.datacard_name) )
            process_lines['name']   += ( delimiter + str(p_name) )
            process_lines['number'] += ( delimiter + str(p_number) )
            process_lines['rate']   += ( delimiter + str(float(self.d_input[p_name]['rate']) * self.lumi_scaling) )
            process_lines['sys']     = "#systematics line: not implemented yet!!!"


        return process_lines


    def _get_observation(self):
        """
        Read the data from trees and applies a cut.
        So far, we only get rate directly as a number.
        """
        return self.d_input['observation']['rate']

    def _get_systematics_lines(self):
        """
        Find systematics and construct a table/dict
        """
        systematics_lines_list = []
        sys_dict = self.d_input['systematics']
        #loop on keys, i.e. sys names and append value if process found, otherwise, append '-'
        for sys_id in sys_dict.keys():
            values = []
            for sig_id in self.signal_process_list:
                try:
                    value = sys_dict[sys_id][sig_id]
                except KeyError:
                    value = '-'
                values.append(str(value))

            for bkg_id in self.bkg_process_list:
                try:
                    value = sys_dict[sys_id][bkg_id]
                except KeyError:
                    value = '-'
                values.append(str(value))

            if sys_dict[sys_id]['type'].startswith('param'): values=[]
            systematics_lines_list.append('{0} {1} {2}'.format(sys_id, sys_dict[sys_id]['type'],string.join(values,' ') ))

            self.log.debug('Systematic line: {0} '.format(systematics_lines_list[-1]))        #show the last one


        systematics_lines = ''
        n_systematics = 0
        for line in systematics_lines_list:
            systematics_lines += line
            systematics_lines += '\n'
            n_systematics += 1
        return (n_systematics, systematics_lines)


    def make_workspace(self):
        """Make RooWorkspace and dump to a file"""

        gSystem.AddIncludePath("-I$CMSSW_BASE/src/ ");
        gSystem.Load("$CMSSW_BASE/lib/slc5_amd64_gcc472/libHiggsAnalysisCombinedLimit.so");
        gSystem.AddIncludePath("-I$ROOFITSYS/include");



        self.w = RooWorkspace('w')
        #run all functions_and_definitions:
        for factory_statement in self.d_input['functions_and_definitions']:
            self.w.factory(factory_statement)

        for p in self.process_list:
            self.log.debug('Checking for shape in {0}/{1}'.format(self.datacard_name, p))
            try:
                self.d_input[p]['shape']
            except KeyError:
                pass
            else:
                if self.d_input[p]['shape']:
                    self.shapes_exist = True
                    self.w.factory(self.d_input[p]['shape'])
        self.log.debug('Printing workspace...')

        self.data_obs = self.w.pdf('ggH').generate(RooArgSet(self.w.var('mass4l')), self._get_observation())

        self.data_obs.SetNameTitle('data_obs','data_obs')
        getattr(self.w,'import')(self.data_obs)
        if self.DEBUG:
            print 20*"----"
            self.w.Print()
            print 20*"----"
        self.w.writeToFile(self.shapes_output_file)
        self.log.debug('Datacard workspace written to {0}'.format(self.shapes_output_file))


    def scale_lumi_by(self, lumi_scaling):
        """
        Scales luminosity in datacards by a fixed factor. This can be
        used to get exclusion limits projections with higher luminosities.
        """
        self.lumi_scaling = lumi_scaling
        if self.lumi_scaling != 1.0:
            self.card_header+='Rates in datacard are scaled by a factor of {0}'.format(self.lumi_scaling)

        self.log.debug('Rates in datacards will be scaled by a factor of {0}'.format(self.lumi_scaling))
예제 #8
0
class TemplatesPlotter(PlotPolisher):  
  def __init__(self,name = "templates" ):
      self.log = Logger().getLogger(self.__class__.__name__, 10)
      self.name = name
      #ROOT.gSystem.AddIncludePath("-I$ROOFITSYS/include/");
      ROOT.gROOT.ProcessLine(".L tdrstyle.cc")
      from ROOT import setTDRStyle
      ROOT.setTDRStyle(True)
      
      ROOT.gStyle.SetPalette(1)
      ROOT.gStyle.SetOptStat(0)
      self.copy_to_web_dir = False
      self.webdir = ""
      self.pp = pprint.PrettyPrinter(indent=4)
      self.k2k1_ratio=0
      self.k3k1_ratio=0
      self.total_pdf=0
      
  def set_k2k1_and_k3k1(self, k2k1, k3k1):
      self.k2k1_ratio = k2k1
      self.k3k1_ratio = k3k1
      
  def setName(self, newname): self.name = newname
  def get_scale_factors(self):
            
        factors = {'lambda12_cosP' : 0.0,
            'lambda13_cosP' : 0.0,
            'lambda23_cosN' : 0.0,
            'lambda23_cosP' : 0.0,
            'gamma33' : 0.034,
            'gamma13' : 0.0,
            'gamma12' : -0.269303399267,
            'gamma11' : 1.0,
            'lambda13_cosN' : 0.0,
            'lambda12_sinN' : 0.0,
            'lambda12_cosN' : 0.538606798534,
            'gamma22' : 0.09,
            'gamma23' : 0.0,
            }
            
        nominator={
            'ggH_shape'      :             '{0}'.format(factors['gamma11']),
            'gg0Ph_shape'    :     '{0}*{1}*{1}'.format(factors['gamma22'],self.k2k1_ratio),     # @0 = k2k1_ratio
            'gg0M_shape'     :     '{0}*{1}*{1}'.format(factors['gamma33'],self.k3k1_ratio),     # @1 = k3k1_ratio
            'ggInt_12P_shape':         '{0}*{1}'.format(factors['lambda12_cosP'],self.k2k1_ratio),  
            'ggInt_12N_shape':    '{0}*{1}*(-1)'.format(factors['lambda12_cosN'],self.k2k1_ratio),
            'ggInt_13P_shape':         '{0}*{1}'.format(factors['lambda13_cosP'],self.k3k1_ratio),  
            'ggInt_13N_shape':    '{0}*{1}*(-1)'.format(factors['lambda13_cosN'],self.k3k1_ratio),  
            'ggInt_23P_shape':     '{0}*{1}*{2}'.format(factors['lambda23_cosP'],self.k2k1_ratio,self.k3k1_ratio),  
            'ggInt_23N_shape':'{0}*{1}*{2}*(-1)'.format(factors['lambda23_cosN'],self.k2k1_ratio,self.k3k1_ratio)  
            }
        return nominator


  def set_total_pdf(self, plot, root_file):
        f = TFile(root_file,"READ")
        nominator= self.get_scale_factors()    
        for i_pdf in range(len(plot['histos'])):
            if i_pdf==0:
                total_pdf = f.Get(plot['histos'][i_pdf])
                total_pdf.Scale(eval(nominator[plot['histos'][i_pdf]]))
                self.log.debug('TOTAL_PDF -> Picking up the first term {0} and the scale {1}. The histo is TH2: {2}'.format(plot['histos'][i_pdf], eval(nominator[plot['histos'][i_pdf]]), isinstance(total_pdf,TH2)))

            if plot['histos'][i_pdf]=="TOTAL_PDF":
                break
            another_template = f.Get(plot['histos'][i_pdf])
            another_template.Scale(float(eval(nominator[plot['histos'][1]]))) 
            self.log.debug('TOTAL_PDF -> Adding up term {0} and the scale {1} to histo TH2:{2}'.format(plot['histos'][i_pdf], eval(nominator[plot['histos'][i_pdf]]),isinstance(total_pdf,TH2)))
            total_pdf.Add(another_template)
        self.total_pdf = total_pdf    
        self.log.debug('TOTAL_PDF -> Added all terms and now the histo is TH2:{0} ... returning the value.'.format(isinstance(self.total_pdf,TH2)))
        
  def makePlot(self, data):

      self.c1 =ROOT.TCanvas("cc1","Templates",1000,800)
      self.c1.cd()
      ROOT.gPad.SetRightMargin(0.2)
      
      try:
	  data['content']
      except KeyError:
	  raise KeyError, "Canvas \'content\' dictionary is not provided in config file."
	  
      try:
	  data['setup']
      except KeyError:
	  print "@@@@ Canvas \'setup\' dictionary is not provided. "
	  self.setup_exist=False
      else:
	  self.arrangeCanvas(self.c1, data['setup'])
	  self.setup_exist=True
	  
      
      try:
	  self.leg
      except AttributeError:
	  self.leg_exist=False
      else:
	  self.leg_exist=True
      print data['setup']['add_text']
      try:
	  data['setup']['add_text']
      except KeyError:
	  self.add_text_exist=False
      else:
	  self.add_text_exist=True
	  
      
      
      i=0
      #rat = RootAttributeTranslator()
      for plot in data['content']:
        try:
            plot['POI']
        except KeyError:
            self.plot_total_pdf=False
        else:
            self.plot_total_pdf=True  
            plot['histos'].append("TOTAL_PDF")
            
            
        
  
        for fs in data['setup']['final_states']:
            print 100*"-"
            theTemplFile = plot['file']+"_"+fs+".root"
            interference_tag = "13"
            
            if ('ggInt_13P_shape' in plot['histos']) and ('ggInt_13N_shape' in plot['histos']): 
                plot['histos'].append("ggInt_13_shape")
                interference_tag = "13"
            elif ('ggInt_12P_shape' in plot['histos']) and ('ggInt_12N_shape' in plot['histos']): 
                plot['histos'].append("ggInt_12_shape")
                interference_tag = "12"
            if self.plot_total_pdf:
                self.set_total_pdf(plot, theTemplFile)
                
                self.log.debug('TOTAL_PDF -> Returned histo is TH2:{0}'.format(isinstance(self.total_pdf,TH2)))
            for theTemplate in plot['histos']:
               
                doSum = False
                if theTemplate == "ggInt_{0}_shape".format(interference_tag) : 
                    doSum = True
                    
                self.log.debug("Running on file:{0}  template:{1}".format(theTemplFile, theTemplate))
                if DEBUG:
                    pp.pprint(plot)
                th2 = SimplePlotter()
                
                #plot['th2'] = th2.getTH2(theTemplFile, theTemplate)
                if theTemplate=="TOTAL_PDF":
                    plot['th2'] = self.total_pdf
                    self.log.debug('TOTAL_PDF -> The addition is over. Going to plot now.' )
                elif not doSum:
                    f = TFile(theTemplFile,"READ")
                    plot['th2'] = f.Get(theTemplate)
                else:
                    self.log.debug('Summing up the interference template, which has been divided into positive and negative part.')
                    f = TFile(theTemplFile,"READ")
                    th2_P = f.Get("ggInt_{0}P_shape".format(interference_tag))
                    th2_N = f.Get("ggInt_{0}N_shape".format(interference_tag))
                        
                    #get lamda factors
                    tree=f.Get('factors')
                    factors={}
                    for fn in ['lambda{0}_cosP'.format(interference_tag),'lambda{0}_cosN'.format(interference_tag)]:
                        factors[fn] = array('d',[0])
                        tree.SetBranchAddress(fn,factors[fn])
                    tree.GetEntry(0)
                    for fn in factors.keys(): factors[fn] = factors[fn][0]
                    print "Lambdas:",factors['lambda{0}_cosP'.format(interference_tag)], factors['lambda{0}_cosN'.format(interference_tag)]
                    th2_P.Scale(factors['lambda{0}_cosP'.format(interference_tag)])
                    th2_N.Scale(factors['lambda{0}_cosN'.format(interference_tag)])
                    th2_P.Add(th2_N, -1)
                    plot['th2'] = th2_P
                 
                 
                 
                    
                if self.setup_exist: self.arrangeAxis(plot['th2'],data['setup'])
                if self.leg_exist:
                    self.leg.AddEntry(plot['th2'],plot['legend']['text'],plot['legend']['opt']);
                try:
                    plot['draw_opt']
                except KeyError:
                    draw_opt = "COLZ"
                else:
                    draw_opt = str(plot['draw_opt'])
                if draw_opt.lower()=="surf":
                    self.c1.SetTheta(16.56)
                    self.c1.SetPhi(57.83133)

                plot['th2'].Draw(draw_opt)
                
                if self.leg_exist: self.leg.Draw()
                
                
                template_name={
                    'ggH_shape':"T_{11}",
                    'gg0M_shape':'T_{33}',
                    'ggInt_13P_shape':'T_{13}',
                    'ggInt_13N_shape':'T_{13}',
                    'ggInt_13_shape':'T_{13}',
                    'gg0Ph_shape':'T_{22}',
                    'ggInt_12P_shape':'T_{12}',
                    'ggInt_12N_shape':'T_{12}',
                    'ggInt_12_shape':'T_{12}',
                    'qqZZ_shape':'T_{qqZZ}',
                    'TOTAL_PDF' : 'Total PDF'
                    }
                    
                if self.add_text_exist: 
                        data['setup']['add_text'][0]['text'] = "{0}(X,Y), {1}, GEN level, 8TeV".format(template_name[theTemplate], fs)
                        if theTemplate=="TOTAL_PDF":
                            data['setup']['add_text'][0]['text']+=", Parameters: k2/k1={0}, k3/k1={1}".format(self.k2k1_ratio,self.k3k1_ratio)
                        self.add_text(data['setup']['add_text'])
                
                
                plot_name = "{0}_{1}_{2}".format(theTemplFile.replace("/","_"), theTemplate, draw_opt)
                #plot_name = self.name
                self.save_extensions = ['png','pdf','eps']
                try:
                    data['setup']['save_ext']
                except KeyError:
                    self.log.info("No extensions are provided in setup. Using default: ", self.save_extensions)
                else:
                    self.save_extensions = list(data['setup']['save_ext'])
                self.save(self.c1, plot_name, self.save_extensions)
    
 
            
        
      
  def setCopyToWebDir(self,doCopy=False,webdir=""):
      if doCopy:
	  self.copy_to_web_dir = True
	  if webdir!="":
	      self.webdir = webdir
	  else:
	      raise ValueError, "You have to provide a webdir path if you want to copy the files."
      else:
	self.copy_to_web_dir = False
	self.webdir = ""
      return 0
	

  def doCopyToWebDir(self,plot_name, newname=""):
      if newname=="":
	    newname = plot_name
      if self.webdir!="" :
          misctools.make_sure_path_exists(self.webdir)
          if not os.path.exists("{0}/index.php".format(self.webdir)) : shutil.copy("/afs/cern.ch/user/r/roko/www/html/index.php",self.webdir)
	  shutil.copy(plot_name,self.webdir+"/"+newname)
	  self.log.info("Copied {0} to webdir {1}".format(plot_name,self.webdir+"/"+newname))
      else :
	  raise ValueError, "You have to provide a webdir path if you want to copy the files."      
      return 0

      
  def save(self, canv, plot_name, extensions=[]):     
      #extensions = ['.png','.pdf','.eps','.root']
      if len(extensions)==0: extensions=['']
      for ext in extensions:
	postfix = "."+ext
	if ext=='': postfix=''
	canv.SaveAs(plot_name+postfix)
	self.log.debug("Saving to: {0}.*".format(plot_name))
	if self.copy_to_web_dir : 
	  self.doCopyToWebDir(plot_name+postfix)
def prepare_asimov_toy_datasets_for_sync():
    #parseOptions()
    DEBUG = False
    if opt.verbosity != 10:
        os.environ['PYTHON_LOGGER_VERBOSITY'] = str(opt.verbosity)
    if opt.verbosity >= 4:
        DEBUG = True

    #1) RooDataSet with all processes for one sqrts and final state
    #- have RooCategory for sqrts and final state
    #chan  = [1_7, 2_7, 3_7, 1_8,2_8,3_8]
    #cat.defineType(chan[j],j); j=0..6
    #cat.setLabel(chan[j]);
    #2) finally append all toys to same RooDataSet

    log = Logger().getLogger("prepare_toy_datasets_for_sync", 10)

    toy_manager = ToyDataSetManager()
    #toy_manager.set_workspace_path(opt.ws_path)
    #path_to_tree, tree_variables, weight = "1", dataset_name = "my_dataset"):
    #shapes *        ch1_ch1  hzz4l_4muS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch2  hzz4l_4eS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch3  hzz4l_2e2muS_7TeV.input.root w:$PROCESS
    #shapes *        ch2_ch1  hzz4l_4muS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch2  hzz4l_4eS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch3  hzz4l_2e2muS_8TeV.input.root w:$PROCESS

    model = "trees_SM"
    #model = "trees_Mix_fa2"
    #model = "trees_Mix_fa3"
    toys_dir = "/afs/cern.ch/user/r/roko/wd_datacards/CreateFullSimToys/{0}".format(
        model)

    #Christoys:
    #toys_dir = "/afs/cern.ch/work/c/chmartin/public/ForEmbedded/Final_v2/"

    #chan_path_dict = {
    #'ch1_ch1' :  "{0}/Toys_CJLSTntuple_7TeV_4mu_*.root/SelectedTree".format(toys_dir),
    #'ch1_ch2' :  "{0}/Toys_CJLSTntuple_7TeV_4e_*.root/SelectedTree".format(toys_dir),
    #'ch1_ch3' :  "{0}/Toys_CJLSTntuple_7TeV_2e2mu_*.root/SelectedTree".format(toys_dir),
    #'ch2_ch1' :  "{0}/Toys_CJLSTntuple_8TeV_4mu_*.root/SelectedTree".format(toys_dir),
    #'ch2_ch2' :  "{0}/Toys_CJLSTntuple_8TeV_4e_*.root/SelectedTree".format(toys_dir),
    #'ch2_ch3' :  "{0}/Toys_CJLSTntuple_8TeV_2e2mu_*.root/SelectedTree".format(toys_dir)
    #}

    chan_path_dict = {
        'ch1_ch1': "{0}/DTree_7TeV_4mu_*.root/selectedEvents".format(toys_dir),
        'ch1_ch2': "{0}/DTree_7TeV_4e_*.root/selectedEvents".format(toys_dir),
        'ch1_ch3':
        "{0}/DTree_7TeV_2e2mu_*.root/selectedEvents".format(toys_dir),
        'ch2_ch1': "{0}/DTree_8TeV_4mu_*.root/selectedEvents".format(toys_dir),
        'ch2_ch2': "{0}/DTree_8TeV_4e_*.root/selectedEvents".format(toys_dir),
        'ch2_ch3':
        "{0}/DTree_8TeV_2e2mu_*.root/selectedEvents".format(toys_dir)
    }

    pp = pprint.PrettyPrinter(indent=4)
    #if DEBUG:
    #log.debug('Initial dictionary:')
    #pp.pprint(chan_path_dict)
    channel_name = sorted(chan_path_dict.keys())
    cat = RooCategory("CMS_channel", "CMS_channel")
    for cat_idx, cat_name in enumerate(channel_name):
        cat.defineType(cat_name, cat_idx)
        cat.setLabel(cat_name)
    if DEBUG:
        log.debug('Category : {0}'.format(cat_idx))
        cat.Print('v')

    my_vars = []
    if ("fa3" in model) and ("fa2" in model):
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'mass4l'
        ]
    elif "fa3" in model:
        my_vars = ['D_bkg', 'D_0m', 'D_cp', 'Weight', 'mass4l']
    elif "fa2" in model:
        my_vars = ['D_bkg', 'D_0hp', 'D_int', 'Weight', 'mass4l']
    else:
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'mass4l'
        ]

    my_rrv = dict()
    my_rrv['D_bkg'] = RooRealVar('D_bkg', 'D_bkg', 0, 1.)

    my_rrv['D_0m'] = RooRealVar('D_0m', 'D_0m', 0, 1.)
    my_rrv['D_cp'] = RooRealVar('D_cp', 'D_cp', -0.5, 0.5)

    my_rrv['D_0hp'] = RooRealVar('D_0hp', 'D_0hp', 0, 1.)
    my_rrv['D_int'] = RooRealVar('D_int', 'D_int', -0.2, 1)

    my_rrv['mass4l'] = RooRealVar('mass4l', 'mass4l', 100, 1000.)
    my_rrv['Weight'] = RooRealVar('Weight', 'Weight', 1.)

    my_rrv['D_bkg'].setBins(5)
    #my_rrv['D_bkg'].setBins(50)
    my_rrv['D_0m'].setBins(50)
    my_rrv['D_cp'].setBins(50)
    my_rrv['D_0hp'].setBins(50)
    my_rrv['D_int'].setBins(50)

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    if os.path.exists(opt.output_filename):
        log.debug("Removing file: {0}".format(opt.output_filename))
        os.remove(opt.output_filename)

    import lib.RootHelpers.RootHelperBase as rhb
    root_helper = rhb.RootHelperBase()

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    #import asimov dataset
    for cat_idx, cat_name in enumerate(channel_name):
        my_path_to_tree = chan_path_dict[cat_name]
        log.debug("my_path_to_tree: {0} ".format(my_path_to_tree))

        the_dataset = toy_manager.get_dataset_from_tree(
            path_to_tree=my_path_to_tree,
            tree_variables=my_vars,
            weight="(mass4l<140.6&&mass4l>105.6)",
            dataset_name="toys/toy_asimov_v0_{0}".format(cat_idx),
            basket=False,
            weight_var_name="Weight")

        the_dataset_with_cat = RooDataSet(
            "toys/toy_asimov_{0}".format(cat_idx),
            "toys/toy_asimov_{0}".format(cat_idx), my_arg_set,
            RooFit.Index(cat), RooFit.Import(cat_name, the_dataset),
            RooFit.WeightVar("Weight"))
        log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(
            the_dataset_with_cat.sumEntries()))
        the_dataset_with_cat.Print()
        if cat_idx == 0:
            combined_dataset = the_dataset_with_cat
        else:
            combined_dataset.append(the_dataset_with_cat)

    log.debug('RooDataSet combined_dataset contains {0} events'.format(
        combined_dataset.sumEntries()))
    combined_dataset.Print()
    combined_dataset.Print("v")
    #combined_dataset.SetNameTitle("toys/embedded_asimov","toys/embedded_asimov")
    #toy_manager.add_to_basket(combined_dataset)
    toy_manager.add_to_basket(combined_dataset,
                              new_name="toys/embedded_asimov",
                              new_title="toys/embedded_asimov")
    toy_manager.dump_datasets_to_file(opt.output_filename,
                                      'UPDATE')  #this one can receive both
def prepare_toy_datasets_for_sync():
    #parseOptions()
    DEBUG = False
    if opt.verbosity != 10:
        os.environ['PYTHON_LOGGER_VERBOSITY'] = str(opt.verbosity)
    if opt.verbosity >= 4:
        DEBUG = True

    #1) RooDataSet with all processes for one sqrts and final state
    #- have RooCategory for sqrts and final state
    #chan  = [1_7, 2_7, 3_7, 1_8,2_8,3_8]
    #cat.defineType(chan[j],j); j=0..6
    #cat.setLabel(chan[j]);
    #2) finally append all toys to same RooDataSet

    log = Logger().getLogger("prepare_toy_datasets_for_sync", 10)

    toy_manager = ToyDataSetManager()
    #toy_manager.set_workspace_path(opt.ws_path)
    #path_to_tree, tree_variables, weight = "1", dataset_name = "my_dataset"):
    #shapes *        ch1_ch1  hzz4l_4muS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch2  hzz4l_4eS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch3  hzz4l_2e2muS_7TeV.input.root w:$PROCESS
    #shapes *        ch2_ch1  hzz4l_4muS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch2  hzz4l_4eS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch3  hzz4l_2e2muS_8TeV.input.root w:$PROCESS

    #model = "trees_v3/bkg_only"
    #model = "trees_v3/bkg_only/zjets"
    #model = "trees_v3/bkg_only/qqZZ"
    #model = "trees_v3/bkg_only/ggZZ"

    model = "trees_v3/SM"
    #model = "trees_v3/Mixed_fa3"
    #model = "trees_v3/Mixed_fa2"

    #model = "trees_v2/SM"
    #model = "trees_v2/Mixed_fa3"
    #model = "trees_v2/Mixed_fa2"

    #model = "trees_v0/SM"
    #model = "trees_v0/Mixed_fa3"
    #model = "trees_v0/Mixed_fa2"

    toys_dir = "/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/CMSdata/SYNC/{0}".format(
        model)

    #Christoys:
    #toys_dir = "/afs/cern.ch/work/c/chmartin/public/ForEmbedded/Final_v2/"

    chan_path_dict = {
        'ch1_ch1':
        "{0}/Toys_CJLSTntuple_7TeV_4mu_*.root/ToyEvents".format(toys_dir),
        'ch1_ch2':
        "{0}/Toys_CJLSTntuple_7TeV_4e_*.root/ToyEvents".format(toys_dir),
        'ch1_ch3':
        "{0}/Toys_CJLSTntuple_7TeV_2e2mu_*.root/ToyEvents".format(toys_dir),
        'ch2_ch1':
        "{0}/Toys_CJLSTntuple_8TeV_4mu_*.root/ToyEvents".format(toys_dir),
        'ch2_ch2':
        "{0}/Toys_CJLSTntuple_8TeV_4e_*.root/ToyEvents".format(toys_dir),
        'ch2_ch3':
        "{0}/Toys_CJLSTntuple_8TeV_2e2mu_*.root/ToyEvents".format(toys_dir)
    }
    pp = pprint.PrettyPrinter(indent=4)
    #if DEBUG:
    #log.debug('Initial dictionary:')
    #pp.pprint(chan_path_dict)
    channel_name = sorted(chan_path_dict.keys())
    cat = RooCategory("CMS_channel", "CMS_channel")
    for cat_idx, cat_name in enumerate(channel_name):
        cat.defineType(cat_name, cat_idx)
        cat.setLabel(cat_name)
    if DEBUG:
        log.debug('Category : {0}'.format(cat_idx))
        cat.Print('v')

    my_vars = []
    if ("fa3" in model) and ("fa2" in model):
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'ZZMass'
        ]
    elif "fa3" in model:
        my_vars = ['D_bkg', 'D_0m', 'D_cp', 'Weight', 'ZZMass']
    elif "fa2" in model:
        my_vars = ['D_bkg', 'D_0hp', 'D_int', 'Weight', 'ZZMass']
    else:
        my_vars = [
            'D_bkg', 'D_0m', 'D_cp', 'D_0hp', 'D_int', 'Weight', 'ZZMass'
        ]

    my_rrv = dict()
    my_rrv['D_bkg'] = RooRealVar('D_bkg', 'D_bkg', 0, 1.)

    my_rrv['D_0m'] = RooRealVar('D_0m', 'D_0m', 0, 1.)
    my_rrv['D_cp'] = RooRealVar('D_cp', 'D_cp', -0.5, 0.5)

    my_rrv['D_0hp'] = RooRealVar('D_0hp', 'D_0hp', 0, 1.)
    my_rrv['D_int'] = RooRealVar('D_int', 'D_int', -0.2, 1)

    my_rrv['ZZMass'] = RooRealVar('ZZMass', 'ZZMass', 100, 1000.)
    my_rrv['Weight'] = RooRealVar('Weight', 'Weight', 1.)

    #my_rrv['D_bkg'].setBins(2)
    my_rrv['D_bkg'].setBins(50)
    my_rrv['D_0m'].setBins(50)
    my_rrv['D_cp'].setBins(50)
    my_rrv['D_0hp'].setBins(50)
    my_rrv['D_int'].setBins(50)

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    if os.path.exists(opt.output_filename):
        log.debug("Removing file: {0}".format(opt.output_filename))
        os.remove(opt.output_filename)

    ###run on all toys
    #for idx in range(0,1000):  #run on toy numbers
    #log.debug('Running on toy number : {0}'.format(idx))

    ##run on each channel (sqrts+finalstate)
    #for cat_idx, cat_name in enumerate( channel_name ):
    #my_path_to_tree = chan_path_dict[cat_name]
    #log.debug("my_path_to_tree:  {0}".format(my_path_to_tree ))

    #the_dataset = toy_manager.get_dataset_from_tree(path_to_tree = my_path_to_tree, tree_variables=my_vars,
    #weight="ToyNumber=={0} && (ZZMass>105.6 && ZZMass<140.6)".format(idx),
    #dataset_name="toys/toy_{0}_{1}".format(idx,cat_idx), basket = False)
    #the_dataset_with_cat = RooDataSet(the_dataset.GetName(),the_dataset.GetName(), my_arg_set, RooFit.Index(cat),RooFit.Import(cat_name,the_dataset))

    #log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(the_dataset_with_cat.sumEntries()))
    #if cat_idx==0:
    #combined_dataset = the_dataset_with_cat
    #else:
    #combined_dataset.append(the_dataset_with_cat)

    #log.debug('RooDataSet combined_dataset contains {0} events'.format(combined_dataset.sumEntries()))
    #combined_dataset.Print("v")
    #toy_manager.add_to_basket(combined_dataset, new_name = "toys/comb_{0}".format(idx), new_title = "toys/comb_{0}".format(idx))
    #toy_manager.dump_datasets_to_file(opt.output_filename,'UPDATE')  #this one can receive both

    import lib.RootHelpers.RootHelperBase as rhb
    root_helper = rhb.RootHelperBase()

    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    #import asimov dataset
    for cat_idx, cat_name in enumerate(channel_name):
        my_path_to_tree = chan_path_dict[cat_name]
        log.debug("my_path_to_tree: {0} ".format(my_path_to_tree))

        the_dataset = toy_manager.get_dataset_from_tree(
            path_to_tree=my_path_to_tree,
            tree_variables=my_vars,
            weight="(ZZMass<140.6&&ZZMass>105.6)",
            dataset_name="toys/toy_asimov_v0_{0}".format(cat_idx),
            basket=False,
            weight_var_name="Weight")
        #mass_column = RooFormulaVar("CMS_zz4l_mass","CMS_zz4l_mass", "ZZMass", RooArgList(my_rrv['ZZMass']))
        #the_dataset.addColumn(mass_column)
        #my_arg_set.add(mass_column)

        #my_tree = root_helper.get_TTree(my_path_to_tree, cut = "(ZZMass<140.6&&ZZMass>105.6)")
        #log.debug('Selected tree contains {0} events'.format(my_tree.GetEntries()))
        #the_dataset  =  RooDataSet("toys/toy_asimov_v0_{0}".format(cat_idx), "toys/toy_asimov_v0_{0}".format(cat_idx), my_tree, my_arg_set, "", "Weight")

        the_dataset_with_cat = RooDataSet(
            "toys/toy_asimov_{0}".format(cat_idx),
            "toys/toy_asimov_{0}".format(cat_idx), my_arg_set,
            RooFit.Index(cat), RooFit.Import(cat_name, the_dataset),
            RooFit.WeightVar("Weight"))

        #the_dataset_with_cat = RooDataSet(the_dataset.GetName(),the_dataset.GetName(), my_arg_set, RooFit.Index(cat),RooFit.Import(cat_name,the_dataset))
        log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(
            the_dataset_with_cat.sumEntries()))
        the_dataset_with_cat.Print()
        if cat_idx == 0:
            combined_dataset = the_dataset_with_cat
        else:
            combined_dataset.append(the_dataset_with_cat)

    log.debug('RooDataSet combined_dataset contains {0} events'.format(
        combined_dataset.sumEntries()))
    combined_dataset.Print()
    combined_dataset.Print("v")
    toy_manager.add_to_basket(combined_dataset,
                              new_name="toys/embedded_asimov",
                              new_title="toys/embedded_asimov")
    toy_manager.dump_datasets_to_file(opt.output_filename,
                                      'UPDATE')  #this one can receive both
def prepare_toy_datasets_for_sync():
    #parseOptions()
    DEBUG = False
    if opt.verbosity!=10:
        os.environ['PYTHON_LOGGER_VERBOSITY'] =  str(opt.verbosity)
    if opt.verbosity >=4:
        DEBUG = True



    #1) RooDataSet with all processes for one sqrts and final state
    #- have RooCategory for sqrts and final state
        #chan  = [1_7, 2_7, 3_7, 1_8,2_8,3_8]
        #cat.defineType(chan[j],j); j=0..6
        #cat.setLabel(chan[j]);
    #2) finally append all toys to same RooDataSet

    log = Logger().getLogger("prepare_toy_datasets_for_sync", 10)

    toy_manager = ToyDataSetManager()
    #toy_manager.set_workspace_path(opt.ws_path)
    #path_to_tree, tree_variables, weight = "1", dataset_name = "my_dataset"):
    #shapes *        ch1_ch1  hzz4l_4muS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch2  hzz4l_4eS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch3  hzz4l_2e2muS_7TeV.input.root w:$PROCESS
    #shapes *        ch2_ch1  hzz4l_4muS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch2  hzz4l_4eS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch3  hzz4l_2e2muS_8TeV.input.root w:$PROCESS

    #model = "trees_v3/bkg_only"
    #model = "trees_v3/bkg_only/zjets"
    #model = "trees_v3/bkg_only/qqZZ"
    #model = "trees_v3/bkg_only/ggZZ"

    model = "trees_v3/SM"
    #model = "trees_v3/Mixed_fa3"
    #model = "trees_v3/Mixed_fa2"

    #model = "trees_v2/SM"
    #model = "trees_v2/Mixed_fa3"
    #model = "trees_v2/Mixed_fa2"

    #model = "trees_v0/SM"
    #model = "trees_v0/Mixed_fa3"
    #model = "trees_v0/Mixed_fa2"

    toys_dir = "/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/CMSdata/SYNC/{0}".format(model)

    #Christoys:
    #toys_dir = "/afs/cern.ch/work/c/chmartin/public/ForEmbedded/Final_v2/"

    chan_path_dict = {
            'ch1_ch1' :  "{0}/Toys_CJLSTntuple_7TeV_4mu_*.root/ToyEvents".format(toys_dir),
            'ch1_ch2' :  "{0}/Toys_CJLSTntuple_7TeV_4e_*.root/ToyEvents".format(toys_dir),
            'ch1_ch3' :  "{0}/Toys_CJLSTntuple_7TeV_2e2mu_*.root/ToyEvents".format(toys_dir),
            'ch2_ch1' :  "{0}/Toys_CJLSTntuple_8TeV_4mu_*.root/ToyEvents".format(toys_dir),
            'ch2_ch2' :  "{0}/Toys_CJLSTntuple_8TeV_4e_*.root/ToyEvents".format(toys_dir),
            'ch2_ch3' :  "{0}/Toys_CJLSTntuple_8TeV_2e2mu_*.root/ToyEvents".format(toys_dir)
        }
    pp = pprint.PrettyPrinter(indent=4)
    #if DEBUG:
        #log.debug('Initial dictionary:')
        #pp.pprint(chan_path_dict)
    channel_name = sorted(chan_path_dict.keys())
    cat = RooCategory("CMS_channel","CMS_channel")
    for cat_idx, cat_name in enumerate( channel_name ):
            cat.defineType(cat_name,cat_idx);
            cat.setLabel(cat_name);
    if DEBUG:
            log.debug('Category : {0}'.format(cat_idx))
            cat.Print('v')

    my_vars = []
    if ("fa3" in model) and ("fa2" in model):
        my_vars = ['D_bkg','D_0m','D_cp', 'D_0hp','D_int','Weight','ZZMass']
    elif "fa3" in model:
        my_vars = ['D_bkg','D_0m','D_cp','Weight','ZZMass']
    elif "fa2" in model:
        my_vars = ['D_bkg', 'D_0hp','D_int','Weight','ZZMass']
    else:
        my_vars = ['D_bkg','D_0m','D_cp', 'D_0hp','D_int','Weight','ZZMass']

    my_rrv = dict()
    my_rrv['D_bkg']     = RooRealVar('D_bkg','D_bkg', 0,1.)


    my_rrv['D_0m']      = RooRealVar('D_0m','D_0m', 0,1.)
    my_rrv['D_cp']      = RooRealVar('D_cp','D_cp', -0.5,0.5)

    my_rrv['D_0hp']      = RooRealVar('D_0hp','D_0hp', 0,1.)
    my_rrv['D_int']      = RooRealVar('D_int','D_int', -0.2,1)

    my_rrv['ZZMass']    = RooRealVar('ZZMass','ZZMass', 100,1000.)
    my_rrv['Weight']    = RooRealVar('Weight','Weight', 1.)


    #my_rrv['D_bkg'].setBins(2)
    my_rrv['D_bkg'].setBins(50)
    my_rrv['D_0m'].setBins(50)
    my_rrv['D_cp'].setBins(50)
    my_rrv['D_0hp'].setBins(50)
    my_rrv['D_int'].setBins(50)



    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')



    if os.path.exists(opt.output_filename):
        log.debug("Removing file: {0}".format(opt.output_filename))
        os.remove(opt.output_filename)

    ###run on all toys
    #for idx in range(0,1000):  #run on toy numbers
        #log.debug('Running on toy number : {0}'.format(idx))


        ##run on each channel (sqrts+finalstate)
        #for cat_idx, cat_name in enumerate( channel_name ):
            #my_path_to_tree = chan_path_dict[cat_name]
            #log.debug("my_path_to_tree:  {0}".format(my_path_to_tree ))

            #the_dataset = toy_manager.get_dataset_from_tree(path_to_tree = my_path_to_tree, tree_variables=my_vars,
                                                            #weight="ToyNumber=={0} && (ZZMass>105.6 && ZZMass<140.6)".format(idx),
                                                            #dataset_name="toys/toy_{0}_{1}".format(idx,cat_idx), basket = False)
            #the_dataset_with_cat = RooDataSet(the_dataset.GetName(),the_dataset.GetName(), my_arg_set, RooFit.Index(cat),RooFit.Import(cat_name,the_dataset))

            #log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(the_dataset_with_cat.sumEntries()))
            #if cat_idx==0:
                #combined_dataset = the_dataset_with_cat
            #else:
                #combined_dataset.append(the_dataset_with_cat)

        #log.debug('RooDataSet combined_dataset contains {0} events'.format(combined_dataset.sumEntries()))
        #combined_dataset.Print("v")
        #toy_manager.add_to_basket(combined_dataset, new_name = "toys/comb_{0}".format(idx), new_title = "toys/comb_{0}".format(idx))
        #toy_manager.dump_datasets_to_file(opt.output_filename,'UPDATE')  #this one can receive both

    import lib.RootHelpers.RootHelperBase as rhb
    root_helper = rhb.RootHelperBase()


    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    #import asimov dataset
    for cat_idx, cat_name in enumerate( channel_name ):
            my_path_to_tree = chan_path_dict[cat_name]
            log.debug("my_path_to_tree: {0} ".format(my_path_to_tree ))

            the_dataset = toy_manager.get_dataset_from_tree(path_to_tree = my_path_to_tree, tree_variables = my_vars,
                                                                  weight="(ZZMass<140.6&&ZZMass>105.6)", dataset_name="toys/toy_asimov_v0_{0}".format(cat_idx), basket = False, weight_var_name = "Weight")
            #mass_column = RooFormulaVar("CMS_zz4l_mass","CMS_zz4l_mass", "ZZMass", RooArgList(my_rrv['ZZMass']))
            #the_dataset.addColumn(mass_column)
            #my_arg_set.add(mass_column)

            #my_tree = root_helper.get_TTree(my_path_to_tree, cut = "(ZZMass<140.6&&ZZMass>105.6)")
            #log.debug('Selected tree contains {0} events'.format(my_tree.GetEntries()))
            #the_dataset  =  RooDataSet("toys/toy_asimov_v0_{0}".format(cat_idx), "toys/toy_asimov_v0_{0}".format(cat_idx), my_tree, my_arg_set, "", "Weight")

            the_dataset_with_cat = RooDataSet("toys/toy_asimov_{0}".format(cat_idx),"toys/toy_asimov_{0}".format(cat_idx), my_arg_set, RooFit.Index(cat), RooFit.Import(cat_name,the_dataset), RooFit.WeightVar("Weight"))


            #the_dataset_with_cat = RooDataSet(the_dataset.GetName(),the_dataset.GetName(), my_arg_set, RooFit.Index(cat),RooFit.Import(cat_name,the_dataset))
            log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(the_dataset_with_cat.sumEntries()))
            the_dataset_with_cat.Print()
            if cat_idx==0:
                combined_dataset = the_dataset_with_cat
            else:
                combined_dataset.append(the_dataset_with_cat)

    log.debug('RooDataSet combined_dataset contains {0} events'.format(combined_dataset.sumEntries()))
    combined_dataset.Print()
    combined_dataset.Print("v")
    toy_manager.add_to_basket(combined_dataset, new_name = "toys/embedded_asimov", new_title = "toys/embedded_asimov")
    toy_manager.dump_datasets_to_file(opt.output_filename,'UPDATE')  #this one can receive both
class RootHelperBase(object):
    """
    Class that helps to pick any object from any root file
    by specifiying the path to the object like:
    path/to/root_file.root/path/to/root_object.
    Class can also check for type of object and return TypeError
    in case the object is not of a desired type.
    """

    def __init__(self):
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.pp = pprint.PrettyPrinter(indent=4)
        self.DEBUG = True

    def check_in_opened_files_table(self, file_name, access):
        """
        Makes a check in the open_files_table dictionary and returns
        pointer to the file if file is opened correct access mode.

        Returns:
        --------
        tuple (is_opened, pointer_to_file).
        In case file doesn't exist (is_opened=False,pointer_to_file=None)

        """
        try:
            self.open_files_table[file_name][access]
        except KeyError:
            self.log.debug("File {0} is not opened in {1} mode.".format(file_name, access))
            return (False, None)
        else:
            the_file = self.open_files_table[file_name][access]
            if isinstance(the_file, TFile):
                if the_file.IsOpen() and not the_file.IsZombie():
                    self.log.debug(
                        "File {0} is already opened in {1} mode. Return pointer to file.".format(file_name, access)
                    )
                    return (True, the_file)
                else:
                    self.log.debug("File {0}   in {1} mode is either closed or zombie.".format(file_name, access))
                    self.open_files_table[file_name][access] = None
                    return (False, None)
            else:
                self.log.debug("File {0} is not opened in {1} mode.".format(file_name, access))
                return (False, None)

    def update_opened_files_table(self, file_object, access):
        """
        Update the status of files opened.
        file_name: acces : file_pointer structure.
        """
        try:
            self.open_files_table
        except AttributeError:
            # self.open_files_table = collections.OrderedDict()
            self.open_files_table = {}
            self.open_files_table[file_object.GetName()] = {access: file_object}
        else:

            try:
                self.open_files_table[file_object.GetName()]
            except KeyError:
                self.open_files_table[file_object.GetName()] = {access: file_object}
            else:
                self.open_files_table[file_object.GetName()].update({access: file_object})
            # self.open_files_table['dummy'].update({access : file_object})
            # self.open_files_table[file_object.GetName()][access] = file_object

        if self.DEBUG:
            self.pp.pprint(self.open_files_table)

        return 0

    def TFile_safe_open(self, file_name, access="READ"):
        """
        Safely open TFile object. Memory is saved by cheking if the file is already
        open by looking up in the list open_files_table.

        """
        # check if file is already openedby looking-up the opend files dict
        is_opened = False
        rootfile = None
        try:
            self.open_files_table
        except AttributeError:
            pass
        else:
            is_opened, rootfile = self.check_in_opened_files_table(file_name, access)
        if is_opened:
            self.log.debug("Returning pointer to ROOT file: {0}".format(file_name))
            return rootfile

        self.log.debug("Opening ROOT file: {0}".format(file_name))

        if access.upper() == "READ" and not os.path.exists(file_name):
            raise IOError, "File path does not exist: {0}".format(file_name)
        else:
            base_dir = os.path.dirname(file_name)
            misc.make_sure_path_exists(base_dir)

        rootfile = TFile.Open(file_name, access)
        self.update_opened_files_table(rootfile, access)

        if not rootfile:
            raise IOError, "The file {0} either doesn't exist or cannot be open".format(file_name)
        return rootfile

    def get_paths(self, path):
        """
        Returns tuple (path_to_root_file, path_to_root_object_in_root_file)
        """
        path_contains_file = ".root" in path
        path_segments = path.split(".root")
        if path.endswith(".root"):  # only root file path exists
            return (path, "")

        # print path_segments
        # assert 1<len(path_segments)<=2, 'Path should be in format <path/to/dir>root_object_file.root/path_to_root_object_in_file'
        assert (
            0 < len(path_segments) <= 2
        ), "Path should be in format <path/to/dir>root_object_file.root/path_to_root_object_in_file"
        path_to_file = ""
        if len(path_segments) == 2:  # path contains file name and object path in the root file
            path_to_file = path_segments[0] + ".root"
            self.log.debug("Src root file: {0}".format(path_to_file))
            # path_to_root_object = string.join(path_segments[-1].split('/')[1:],'/') #to remove the '/' after .root

        if path_segments[-1].startswith("/"):
            path_to_root_object = path_segments[-1][1:]  # to remove the '/' after .root
        else:
            path_to_root_object = path_segments[-1]  # there is no '/' at the beggining
        self.log.debug("Src root_object name: {0}".format(path_to_root_object))

        return (path_to_file, path_to_root_object)

        # path_to_file = path_segments[0]+'.root'
        # self.log.debug('Src root file: {0}'.format(path_to_file ))
        # path_to_root_object = string.join(path_segments[-1].split('/')[1:],'/') #to remove the '/' after .root
        # self.log.debug('Src root_object name: {0}'.format(path_to_root_object))

        # return (path_to_file,path_to_root_object)

    def get_object(self, path, object_type=None, clone=False):
        """
        Get any root object copy from path and check it's type.
        The object is copied from the file if needed.
        """
        path_to_file, path_to_root_object = self.get_paths(path)
        root_object_file = self.TFile_safe_open(path_to_file, "READ")
        the_object = root_object_file.Get(path_to_root_object)
        is_TTree = isinstance(the_object, TTree)
        if clone:
            if not is_TTree:
                the_object = copy.deepcopy(root_object_file.Get(path_to_root_object))
                self.log.debug("Coping root_object {0} of type={1}.".format(path_to_root_object, type(the_object)))
                root_object_file.Close()
            else:
                # FIXME
                self.log.warn("Cloning the full tree {0}. !!! Still not fully tested !!!".format(path_to_root_object))
                the_object = root_object_file.Get(path_to_root_object).CloneTree()
                # will not close file since it will destroy the object. Better to write the tree down first, then close file.

        else:
            self.log.debug(
                "Pointer to root_object {0} of type={1} is returned.".format(path_to_root_object, type(the_object))
            )
        return the_object

    def get_TTree(self, path, cut=None, clone=False):
        """
        Get a tree from the path of format //machine/file_name.root/subdir/tree_name.
        If path is list it will asume TChain. Wildcards can be used but ".root" has
        to exost in the path name, otherwise 'segmentation violation'
        """

        the_tree = TChain()

        if isinstance(path, list):
            tree_name = self.get_paths(path[0])[1]
            the_tree.SetName(tree_name)
            for item in path:
                assert isinstance(item, str), "The tree path should be of string format and not: {0}".format(type(item))
                add_result = the_tree.Add(item)

        elif isinstance(path, str):
            tree_name = self.get_paths(path)[1]
            the_tree.SetName(tree_name)
            add_result = the_tree.Add(path)
        self.log.debug("TChain has been constructed from {0} files with correct tree names.".format(add_result))
        if cut:
            assert isinstance(cut, str), "The TTree cut has to be string value, not {0} !!!".format(type(cut))
            clone = True
            the_selection_tree = the_tree.CopyTree(cut)
            return the_selection_tree
        else:
            return the_tree

    def get_histogram(self, path, hist_type=TH1, clone=False):
        """
        Get TH1 object or any other that inherits from TH1
        """
        return self.get_object(path, hist_type, clone)

    def get_embedded_object(
        self, path_to_container, container_type=None, embedded_object=None, object_type=None, clone=False
    ):
        """
        Get an object embedded into another class, like e.g. a TH1 from TCanvas
        saved in file. In case only path_to_container is given, it will return the container
        like with get_object method.
        """
        pass

    def add_to_basket(self, root_object, new_name=None, new_title=None):
        """
        Add object to the basket with new_name and new_title.
        If new_name contains "/" then a directory will be created inside the file. (TODO)
        """

        if new_name:
            # name_in_basket = new_name
            new_name_no_subfolders = new_name.split("/")[-1]  # remove subfolder name from the new_name
            root_object.SetName(new_name_no_subfolders)
            name_in_basket = new_name
        else:
            name_in_basket = root_object.GetName()

        if new_title:
            root_object.SetTitle(new_title)
        try:
            self.root_fruit_basket
        except AttributeError:
            self.root_fruit_basket = collections.OrderedDict()
            self.log.debug("Creating new root-object basket.")
        else:
            if self.DEBUG and len(self.root_fruit_basket) < 10:
                self.log.debug("Adding root-object to existing basket. Basket state (printed if less then 10 items):")
                self.pp.pprint(self.root_fruit_basket)
        self.root_fruit_basket[name_in_basket] = root_object

    def _get_subfolders_and_name(self, path):
        """
        Gives back the 2 element tuple with subfolder path and a name of root_object
        """
        path_segments = path.split("/")
        assert len(path_segments) > 0, "The name should not be empty string."
        if len(path_segments) > 1:
            # check if first is '/'
            if path_segments[0] == "":
                path_segments.pop(0)
            subfolders = string.join(path_segments[:-1], "/")
            root_object_name = path_segments[-1]
            self.log.debug("Root-subfolder: {0}".format(subfolders))
            self.log.debug("Root-object name: {0}".format(root_object_name))
            return (subfolders, root_object_name)
        else:
            root_object_name = path_segments[-1]
            return (None, root_object_name)

    def _get_directory(self, root_file, path):
        """
        Create and cd to the directory if given like a/b/c
        """
        root_file.cd()
        # subfolders = self._get_subfolders_and_name(path)[0]
        if path:
            self.log.debug("Creating root-subfolder {0}".format(path))
            mkdir_res = root_file.mkdir(path)
            self.log.info("Root-subfolder {0} created with code = {1}".format(path, mkdir_res))
            root_file.cd(path)
        else:  # no subfolder will be created
            root_file.cd()
        self.log.debug("Current directory: {0}".format(gDirectory.GetPath()))

    def flush_basket(self):
        """
        Resets the basket content and delets the basket.
        """

        try:
            del self.root_fruit_basket
        except:
            raise RuntimeError, "Basket cannot be flushed and destroyed! It even doesn't exist ..."
        else:
            self.log.info("Basket flushed!")
            return 0

    def dump_basket_to_file(self, file_name, access="UPDATE"):
        """
        Save what is in basket to a file. Create directories in the path if needed.
        """
        out_file = self.TFile_safe_open(file_name, access)
        out_file.cd()
        if self.DEBUG:
            self.pp.pprint(self.root_fruit_basket)

        for item_name in self.root_fruit_basket.keys():
            subfolders, root_object_name = self._get_subfolders_and_name(item_name)
            self._get_directory(out_file, subfolders)  # it will create and cd to the directory if given like a/b/c
            self.log.debug(
                "Writing root-object: {0} Object name: {1} ; Object title: {2}".format(
                    self.root_fruit_basket[item_name],
                    self.root_fruit_basket[item_name].GetName(),
                    self.root_fruit_basket[item_name].GetTitle(),
                )
            )
            is_TTree = isinstance(self.root_fruit_basket[item_name], TTree)
            if is_TTree:
                self.log.debug("This is a TTree object : {0}".format(self.root_fruit_basket[item_name]))
                copy_tree_name = self.root_fruit_basket[item_name].GetName()
                copy_tree_title = self.root_fruit_basket[item_name].GetTitle()
                tree_for_saving = self.root_fruit_basket[item_name].CloneTree(0)
                copy_res = tree_for_saving.CopyEntries(self.root_fruit_basket[item_name])
                tree_for_saving.SetNameTitle(copy_tree_name, copy_tree_title)
                write_res = tree_for_saving.Write()
            else:
                write_res = self.root_fruit_basket[item_name].Write()

            if write_res == 0:
                self.log.error("The object {0} cannot be written into {1}".format(item_name, gDirectory.GetPath()))
            else:
                self.log.info("The object {0} has been written into {1}".format(item_name, gDirectory.GetPath()))

        out_file.Close()
        self.log.info(
            "Saved the basket with {1} items into the file: {0}".format(file_name, len(self.root_fruit_basket))
        )
        self.flush_basket()

        return 0
예제 #13
0
class ChainProcessor(object):
    """
    Processes the configuration set for running the creation 
    of Datacards, combine fit and then plot unit
    """
    
    

    def __init__(self, run_data_name, run_dict):
        """Set the values from configuration"""
        
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.run_dict = run_dict
        self.create_cards_dir="/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/"
        self.version= "v1"
        self.sqrts_dc="8TeV"
        self.sqrts_plot="14"
        self.fs="2e2mu"
        self.lumi="19.79"
        self.lumi_zfill="19.79"
        self.discriminant="D_{0-}(90^{o})"
        self.MY_CUR_WWW_SUBDIR=""
        self.append_name_base = run_data_name
        self.MY_CUR_WWW_SUBDIR = self.append_name_base
        self.POIs = list(self.run_dict['POI'])
        self.poi = self.run_dict['POI_setup']
        self.termNames =  self.run_dict['termNames']
        self.templatedir = self.run_dict['templatedir']
        self.discriminant = self.run_dict['discriminant_name']
        self.additional_options = self.run_dict['additional_options']
        self.fs  = self.run_dict['final_state']
        self.www_dir = "/afs/cern.ch/user/r/roko/www/html/Geoloc/{0}".format(self.MY_CUR_WWW_SUBDIR)
        self.do_copy_to_webdir = True
        print "Setting web directory to: {0}".format(self.www_dir)
        self.do_cmd = ""
        #self.poi= {"k2k1_ratio":{"switch":0, "range":"-100,100", "name":"k2k1", "nice_name":"k_{2}/k_{1}", "value":"0"}, 
              #"k3k1_ratio":{"switch":0, "range":"-100,100", "name":"k3k1", "nice_name":"k_{3}/k_{1}", "value":"0"}}
        self.setup_POI_related_stuff()
            
        
        
    def setup_POI_related_stuff(self):
        """Sets up the information which is related 
           to POIs and is needed to run the commands. 
        """
        def _set_POI_range(self,this_poi):
            """We use this function to set ranges to parameters because we set them lumi-dependant.
            """
            if this_poi=="k3k1_ratio":
                theRange=""
                if float(self.lumi) >  50: 
                    theRange = "-10,10"
                elif float(self.lumi) >  20: 
                    theRange = "-20,20"
                else : 
                    theRange = "-30,30"
                self.poi["k3k1_ratio"]["range"] = theRange
            elif this_poi=="k2k1_ratio":
                pass        
        
        #make list out of POI
        if isinstance(self.run_dict['POI'],list):
            pass
        else:
            tmp_list=[]
            tmp_list.append(self.run_dict['POI'])
            self.run_dict['POI'] = tmp_list
        
        self.POIs = self.run_dict['POI']
        self.log.debug('POI: {0}'.format(self.POIs))
        assert len(self.POIs)>0 and len(self.POIs)<3 and ("k2k1_ratio" or "k3k1_ratio" in self.POIs), \
            "You should provide at least one POI (k2k1_ratio, k3k1_ratio), while you provided {0}".format(self.POIs)
        
        if "k3k1_ratio" in self.POIs and "k2k1_ratio" in self.POIs:
            self.poi_physics_model = "HiggsAnalysis.CombinedLimit.GeoLocationModel:K1andK2andK3Model"
            self.poi_ranges_string_t2w = "--PO range_k3k1_ratio={0} --PO range_k2k1_ratio={1}".format(self.poi["k3k1_ratio"]['range'],self.poi["k2k1_ratio"]['range'] )
            _set_POI_range(self,"k3k1_ratio")
            _set_POI_range(self,"k2k1_ratio")
            self.poi_ranges_string_fit = "k3k1_ratio={0}:k2k1_ratio={1}".format(self.poi["k3k1_ratio"]['range'],self.poi["k2k1_ratio"]['range'])
            self.poi_name_value_plot = "{0}={2} {1}={3}".format(self.poi["k3k1_ratio"]['nice_name'],self.poi["k2k1_ratio"]['nice_name'],self.poi["k3k1_ratio"]['value'], self.poi["k2k1_ratio"]['value'])
            self.poi_name_value = "k3k1_ratio={0},k2k1_ratio={1}".format(self.poi["k3k1_ratio"]['value'],self.poi["k2k1_ratio"]['value'])
            self.poi_name_value_filename = "k3k1_ratio_{0}_k2k1_ratio_{1}".format(self.poi["k3k1_ratio"]['value'],self.poi["k2k1_ratio"]['value'])
            self.pois = "k2k1_ratio,k3k1_ratio"
            self.poi_n_points = str(self.poi["k2k1_ratio"]['n_scan_points']*self.poi["k3k1_ratio"]['n_scan_points'])
            self.log.debug('Set up the {0}'.format(self.poi_physics_model))

            
        elif "k2k1_ratio" in self.POIs:
            self.poi_physics_model = "HiggsAnalysis.CombinedLimit.GeoLocationModel:K1andK2Model"
            self.poi_ranges_string_t2w = "--PO range_k2k1_ratio={0}".format(self.poi["k2k1_ratio"]['range'] )
            _set_POI_range(self,"k2k1_ratio")
            self.poi_ranges_string_fit = "k2k1_ratio={0}".format(self.poi["k2k1_ratio"]['range'])
            self.poi_name_value_plot = "{0}={1}".format(self.poi["k2k1_ratio"]['nice_name'],self.poi["k2k1_ratio"]['value'])
            self.poi_name_value = "k2k1_ratio={0}".format(self.poi["k2k1_ratio"]['value'])
            self.poi_name_value_filename = "k2k1_ratio_{0}".format(self.poi["k2k1_ratio"]['value'])
            self.pois = "k2k1_ratio"
            self.poi_n_points = str(self.poi["k2k1_ratio"]['n_scan_points'])
            self.log.debug('Set up the {0}'.format(self.poi_physics_model))

            
        elif "k3k1_ratio" in self.POIs:
            self.poi_physics_model = "HiggsAnalysis.CombinedLimit.GeoLocationModel:K1andK3Model"
            self.poi_ranges_string_t2w = "--PO range_k3k1_ratio={0}".format(self.poi["k3k1_ratio"]['range'] )
            _set_POI_range(self,"k3k1_ratio")
            self.poi_ranges_string_fit = "k3k1_ratio={0}".format(self.poi["k3k1_ratio"]['range'])
            self.poi_name_value_plot = "{0}={1}".format(self.poi["k3k1_ratio"]['nice_name'],self.poi["k3k1_ratio"]['value'])
            self.poi_name_value = "k3k1_ratio={0}".format(self.poi["k3k1_ratio"]['value'])
            self.poi_name_value_filename = "k3k1_ratio_{0}".format(self.poi["k3k1_ratio"]['value'])
            self.pois = "k3k1_ratio"
            self.poi_n_points = str(self.poi["k3k1_ratio"]['n_scan_points'])
            self.log.debug('Set up the {0}'.format(self.poi_physics_model))
        
        self.log.debug("poi_physics_model ={0}".format(self.poi_physics_model))
        self.log.debug("poi_ranges_string_t2w ={0}".format(self.poi_ranges_string_t2w))
        self.log.debug("poi_ranges_string_fit ={0}".format(self.poi_ranges_string_fit))
        self.log.debug("poi_name_value_plot ={0}".format(self.poi_name_value_plot))
        self.log.debug("poi_name_value ={0}".format(self.poi_name_value))
        self.log.debug("poi_name_value_filename ={0}".format(self.poi_name_value_filename))
        self.log.debug("poi_n_points ={0}".format(self.poi_n_points))
        self.log.debug("pois ={0}".format(self.pois))

        
    def process(self, do_cmd=""):
        self.do_cmd = do_cmd
        self.setup_POI_related_stuff()

        import lib.util.MiscTools as misc
        #print self.__dict__
        self.fs_expanded = self.fs
        if self.fs.lower() == "4l":
            self.fs_expanded = "4e,4#mu,2e2#mu"
        os.environ['PLOT_TAG'] = "Asimov data %(poi_name_value_plot)s | Discrim. %(discriminant)s | L=%(lumi)s fb^{-1} @ %(sqrts_plot)s TeV | Fin. state = %(fs_expanded)s" %self.__dict__

        cmd = {}
        cmd['createCards']    = "rm -r cards_%(append_name_base)s; python makeDCsandWSs.py -b -i SM_inputs_8TeV_CJLST -a %(append_name_base)s -t %(templatedir)s --terms %(termNames)s %(additional_options)s" %self.__dict__
        cmd['combCards']      = "rm -rf hzz4l_4lS_%(sqrts_dc)s_ALT.txt; combineCards.py hzz4l_4muS_%(sqrts_dc)s_ALT.txt hzz4l_4eS_%(sqrts_dc)s_ALT.txt hzz4l_2e2muS_%(sqrts_dc)s_ALT.txt> hzz4l_4lS_%(sqrts_dc)s_ALT.txt" %self.__dict__
        cmd['t2w'] 		= "text2workspace.py hzz4l_%(fs)sS_%(sqrts_dc)s_ALT.txt -m 126 -P %(poi_physics_model)s %(poi_ranges_string_t2w)s  --PO muFloating -o combine.ws.%(fs)s.%(version)s.root" %self.__dict__
        cmd['gen'] 		= "combine -M GenerateOnly combine.ws.%(fs)s.%(version)s.root -m 126 -t -1 --expectSignal=1 --saveToys --setPhysicsModelParameters %(poi_name_value)s,cmshzz4l_lumi=%(lumi)s -S 0" %self.__dict__
        cmd['addasimov'] 	= "root -b -l -q %(create_cards_dir)s/addToyDataset.C\(\\\"combine.ws.%(fs)s.%(version)s.root\\\",\\\"higgsCombineTest.GenerateOnly.mH126.123456.root\\\",\\\"toy_asimov\\\",\\\"workspaceWithAsimov_%(poi_name_value_filename)s_lumi_%(lumi)s.root\\\"\)" %self.__dict__
        cmd['fit'] 		= "combine -M MultiDimFit workspaceWithAsimov_%(poi_name_value_filename)s_lumi_%(lumi)s.root --algo=grid --points %(poi_n_points)s  -m 126 -n .asimov.%(fs)s.%(poi_name_value_filename)s.lumi_%(lumi_zfill)s -D toys/toy_asimov -S 0 --setPhysicsModelParameters cmshzz4l_lumi=%(lumi)s --setPhysicsModelParameterRanges %(poi_ranges_string_fit)s" %self.__dict__
        cmd['plot'] 	       = "root -l -b -q %(create_cards_dir)s/plotLimit.C\(\\\"higgsCombine.asimov.%(fs)s.%(poi_name_value_filename)s.lumi_%(lumi_zfill)s.MultiDimFit.mH126.root\\\",\\\"%(pois)s\\\",\\\"environ PLOT_TAG\\\" \)" %self.__dict__
        
        if "createCards" in do_cmd:
            print "--------------------------------------------------------"
            print "Cards for Lumi=%(lumi)s and tag=%(append_name_base)s terms=%(termNames)s template=%(templatedir)s discriminant=%(discriminant)s additional_options=%(additional_options)s" %self.__dict__
            misc.processCmd(cmd['createCards'])
            print "--------------------------------------------------------"
        if "combCards" in do_cmd: misc.processCmd(cmd['combCards']) 
        if "t2w" in do_cmd: misc.processCmd(cmd['t2w']) 
        if "gen" in do_cmd: misc.processCmd(cmd['gen']) 
        if "addasimov" in do_cmd: misc.processCmd(cmd['addasimov']) 
        if "fit" in do_cmd: misc.processCmd(cmd['fit']) 
        if "plot" in do_cmd: 
            misc.processCmd(cmd['plot']) 
            import lib.util.MiscTools as misc
            misc.make_sure_path_exists(self.www_dir)
            shutil.copy("/afs/cern.ch/user/r/roko/www/html/index.php",self.www_dir)
            for self.x in ['AxisK3K1', 'AxisArctanGamK3K1', 'AxisArctanK3K1', 'AxisFa3', 'AxisK2K1']:
                #print "cp higgsCombine.asimov.{fs}.{poi_name_value}.MultiDimFit.mH126.root*png {www_dir}/exp.{fs}.{poi_name_value_filename}.mH126.{x}.png".format(**self.__dict__)
                #shutil.copy("higgsCombine.asimov.{fs}.{poi_name_value_filename}.lumi_{lumi_zfill}.MultiDimFit.mH126.root.{x}.png".format(**self.__dict__), \
                            #"{www_dir}/exp.{fs}.{poi_name_value_filename}.mH126.{x}.{lumi_zfill}ifb.png".format(**self.__dict__))
                file_2_copy = "higgsCombine.asimov.{fs}.{poi_name_value_filename}.lumi_{lumi_zfill}.MultiDimFit.mH126.root.{x}.png".format(**self.__dict__)            
                if os.path.exists(file_2_copy):
                    shutil.copy(file_2_copy,"{www_dir}/exp.{fs}.{poi_name_value_filename}.mH126.{x}.{lumi_zfill}ifb.png".format(**self.__dict__))
            print "{www_dir}/?match=exp.{fs}.{poi_name_value_filename}.mH126.*.png".format(**self.__dict__).replace("/afs/cern.ch/user/r/roko/www/html/","https://roko.web.cern.ch/roko/")
            
    def get_table_row(self, col_names=False) :
        if col_names: 
            return "LUMI BF UL68 UL95 WF\n".lower()
        import lib.util.MiscTools as misc
        limit_file = "higgsCombine.asimov.%(fs)s.%(poi_name_value_filename)s.lumi_%(lumi_zfill)s.MultiDimFit.mH126.root.limits" %self.__dict__
        self.log.debug("Searching for limits info in file: {0}".format(limit_file))
        BF   = misc.grep("BF", limit_file)[0].split(":")[1].strip()
        UL68 = misc.grep("UL68", limit_file)[0].split(":")[1].strip()
        UL95 = misc.grep("UL95", limit_file)[0].split(":")[1].strip()
        WF   = misc.grep("WF", limit_file)[0].split(":")[1].strip()
        table_raw = "{0} {1} {2} {3} {4}\n".format(self.lumi, BF, UL68,UL95, WF)
        print "Table raw: ", table_raw
        return table_raw
        
        
    def set_lumi(self, lumi, n_digits=0) : 
        self.lumi = str(lumi)
        self.lumi_zfill = self.lumi.zfill(n_digits)
        
    def set_sqrts_dc(self, sqrts) : self.sqrts_dc = str(sqrts)
    def set_sqrts_plot(self, sqrts) : self.sqrts_plot = str(sqrts)
    def set_poi_value(self, poi_name, value) : self.poi[poi_name]['value']= str(value)
    def get_webdir(self): return self.www_dir
    def set_do_copy_to_webdir(self, do_copy=True): self.do_copy_to_webdir = do_copy
    def get_sqrts_dc(self): return self.sqrts_dc
    def get_fs(self): return self.fs
    def get_cmd(self): return self.do_cmd
    def get_poi_name_value(self) : return self.poi_name_value
    def get_poi_name_value_filename(self) : return self.poi_name_value_filename
    def get_run_data(self): return self.run_dict
예제 #14
0
    def get_poi_name_value_filename(self) : return self.poi_name_value_filename
    def get_run_data(self): return self.run_dict
    
    

if __name__ == "__main__":
    """
    This script can create datacards, make toy datasets, fit and plt results.
    Finally it is coping the relevalnt output to the web directory...
    
    """
    # parse the arguments and options
    global opt, args
    parseOptions()
    
    log = Logger().getLogger(__name__, 10)

    #read configuration
    import lib.util.UniversalConfigParser as ucp
    cfg_reader = ucp.UniversalConfigParser(cfg_type="YAML",file_list = opt.config_filename)
    cfg_reader.setLogLevel(10)
    full_config = cfg_reader.get_dict()
    run_data = full_config["COMMON"]
    run_data.update(full_config[opt.run_data_name])
    pp = pprint.PrettyPrinter(indent=4)
    #pp.pprint(full_config)
        
    print "@@@@ THE RUN DATA: YAML -> DICT"
    pp.pprint(run_data)
    if opt.print_config_and_exit:
        quit()
예제 #15
0
 def __init__(self):
     self.log = Logger().getLogger(self.__class__.__name__, 10)
     self.pp = pprint.PrettyPrinter(indent=4)
     self.DEBUG = True
예제 #16
0
class RootHelperBase(object):
    """
    Class that helps to pick any object from any root file
    by specifiying the path to the object like:
    path/to/root_file.root/path/to/root_object.
    Class can also check for type of object and return TypeError
    in case the object is not of a desired type.
    """

    def __init__(self):
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.pp = pprint.PrettyPrinter(indent=4)
        self.DEBUG = True

    def check_in_opened_files_table(self, file_name,access):
        """
        Makes a check in the open_files_table dictionary and returns
        pointer to the file if file is opened correct access mode.

        Returns:
        --------
        tuple (is_opened, pointer_to_file).
        In case file doesn't exist (is_opened=False,pointer_to_file=None)

        """
        try:
            self.open_files_table[file_name][access]
        except KeyError:
            self.log.debug('File {0} is not opened in {1} mode.'.format(file_name, access))
            return (False, None)
        else:
            the_file = self.open_files_table[file_name][access]
            if isinstance(the_file,TFile):
                if (the_file.IsOpen() and not the_file.IsZombie()):
                    self.log.debug('File {0} is already opened in {1} mode. Return pointer to file.'.format(file_name, access))
                    return (True, the_file)
                else:
                    self.log.debug('File {0}   in {1} mode is either closed or zombie.'.format(file_name, access))
                    self.open_files_table[file_name][access] = None
                    return (False, None)
            else:
                self.log.debug('File {0} is not opened in {1} mode.'.format(file_name, access))
                return (False, None)

    def update_opened_files_table(self, file_object, access):
        """
        Update the status of files opened.
        file_name: acces : file_pointer structure.
        """
        try:
            self.open_files_table
        except AttributeError:
            #self.open_files_table = collections.OrderedDict()
            self.open_files_table = {}
            self.open_files_table[file_object.GetName()] = {access : file_object}
        else:

            try:
                self.open_files_table[file_object.GetName()]
            except KeyError:
                self.open_files_table[file_object.GetName()] = {access : file_object}
            else:
                self.open_files_table[file_object.GetName()].update({access : file_object})
            #self.open_files_table['dummy'].update({access : file_object})
            #self.open_files_table[file_object.GetName()][access] = file_object

        if self.DEBUG:
            self.pp.pprint(self.open_files_table)

        return 0


    def TFile_safe_open(self, file_name, access = 'READ'):
        """
        Safely open TFile object. Memory is saved by cheking if the file is already
        open by looking up in the list open_files_table.

        """
        #check if file is already openedby looking-up the opend files dict
        is_opened=False
        rootfile= None
        try:
            self.open_files_table
        except AttributeError:
            pass
        else:
            is_opened, rootfile = self.check_in_opened_files_table(file_name,access)
        if is_opened:
            self.log.debug('Returning pointer to ROOT file: {0}'.format(file_name))
            return rootfile

        self.log.debug('Opening ROOT file: {0}'.format(file_name))

        if access.upper() == 'READ' and not os.path.exists(file_name):
            raise IOError, 'File path does not exist: {0}'.format(file_name)
        else:
            base_dir = os.path.dirname(file_name)
            misc.make_sure_path_exists(base_dir)

        rootfile = TFile.Open(file_name,access)
        self.update_opened_files_table(rootfile, access)

        if not rootfile:
            raise IOError, 'The file {0} either doesn\'t exist or cannot be open'.format(file_name)
        return rootfile


    def get_paths(self, path):
        """
        Returns tuple (path_to_root_file, path_to_root_object_in_root_file)
        """
        path_contains_file = ('.root' in path)
        path_segments = path.split('.root')
        if path.endswith('.root'):  #only root file path exists
            return (path,"")

        #print path_segments
        #assert 1<len(path_segments)<=2, 'Path should be in format <path/to/dir>root_object_file.root/path_to_root_object_in_file'
        assert 0<len(path_segments)<=2, 'Path should be in format <path/to/dir>root_object_file.root/path_to_root_object_in_file'
        path_to_file = ""
        if len(path_segments)==2: #path contains file name and object path in the root file
            path_to_file = path_segments[0]+'.root'
            self.log.debug('Src root file: {0}'.format(path_to_file ))
            #path_to_root_object = string.join(path_segments[-1].split('/')[1:],'/') #to remove the '/' after .root

        if path_segments[-1].startswith('/'):
            path_to_root_object = path_segments[-1][1:] #to remove the '/' after .root
        else:
            path_to_root_object = path_segments[-1] #there is no '/' at the beggining
        self.log.debug('Src root_object name: {0}'.format(path_to_root_object))

        return (path_to_file,path_to_root_object)


        #path_to_file = path_segments[0]+'.root'
        #self.log.debug('Src root file: {0}'.format(path_to_file ))
        #path_to_root_object = string.join(path_segments[-1].split('/')[1:],'/') #to remove the '/' after .root
        #self.log.debug('Src root_object name: {0}'.format(path_to_root_object))

        #return (path_to_file,path_to_root_object)

    def get_object(self, path, object_type=None, clone=False):
        """
        Get any root object copy from path and check it's type.
        The object is copied from the file if needed.
        """
        path_to_file, path_to_root_object = self.get_paths(path)
        root_object_file = self.TFile_safe_open(path_to_file, 'READ')
        the_object = root_object_file.Get(path_to_root_object)
        is_TTree = isinstance(the_object,TTree)
        if clone:
            if not is_TTree:
                the_object = copy.deepcopy(root_object_file.Get(path_to_root_object))
                self.log.debug('Coping root_object {0} of type={1}.'.format(path_to_root_object, type(the_object)))
                root_object_file.Close()
            else:
                #FIXME
                self.log.warn('Cloning the full tree {0}. !!! Still not fully tested !!!'.format(path_to_root_object))
                the_object = root_object_file.Get(path_to_root_object).CloneTree()
                #will not close file since it will destroy the object. Better to write the tree down first, then close file.

        else:
            self.log.debug('Pointer to root_object {0} of type={1} is returned.'.format(path_to_root_object, type(the_object)))
        return the_object

    def get_TTree(self,path , cut = None, clone = False):
        """
        Get a tree from the path of format //machine/file_name.root/subdir/tree_name.
        If path is list it will asume TChain. Wildcards can be used but ".root" has
        to exost in the path name, otherwise 'segmentation violation'
        """

        the_tree = TChain()

        if isinstance(path, list):
            tree_name = self.get_paths(path[0])[1]
            the_tree.SetName(tree_name)
            for item in path:
                assert isinstance(item,str),'The tree path should be of string format and not: {0}'.format(type(item))
                add_result = the_tree.Add(item)

        elif isinstance(path, str):
            tree_name = self.get_paths(path)[1]
            the_tree.SetName(tree_name)
            add_result = the_tree.Add(path)
        self.log.debug('TChain has been constructed from {0} files with correct tree names.'.format(add_result))
        if cut:
            assert isinstance(cut, str), 'The TTree cut has to be string value, not {0} !!!'.format(type(cut))
            clone = True
            the_selection_tree = the_tree.CopyTree(cut)
            return the_selection_tree
        else:
            return the_tree


    def get_histogram(self,path, hist_type = TH1, clone = False):
        """
        Get TH1 object or any other that inherits from TH1
        """
        return self.get_object(path,hist_type, clone)

    def get_embedded_object(self, path_to_container, container_type = None, embedded_object = None, object_type = None, clone = False):
        """
        Get an object embedded into another class, like e.g. a TH1 from TCanvas
        saved in file. In case only path_to_container is given, it will return the container
        like with get_object method.
        """
        pass

    def add_to_basket(self,root_object, new_name = None, new_title = None):
        """
        Add object to the basket with new_name and new_title.
        If new_name contains "/" then a directory will be created inside the file. (TODO)
        """

        if new_name:
            #name_in_basket = new_name
            new_name_no_subfolders = new_name.split('/')[-1]  #remove subfolder name from the new_name
            root_object.SetName(new_name_no_subfolders)
            name_in_basket = new_name
        else:
            name_in_basket = root_object.GetName()


        if new_title:
            root_object.SetTitle(new_title)
        try:
            self.root_fruit_basket
        except AttributeError:
            self.root_fruit_basket = collections.OrderedDict()
            self.log.debug('Creating new root-object basket.')
        else:
            if self.DEBUG and len(self.root_fruit_basket)<10:
                self.log.debug('Adding root-object to existing basket. Basket state (printed if less then 10 items):')
                self.pp.pprint(self.root_fruit_basket)
        self.root_fruit_basket[name_in_basket] = root_object


    def _get_subfolders_and_name(self,path):
        """
        Gives back the 2 element tuple with subfolder path and a name of root_object
        """
        path_segments = path.split('/')
        assert len(path_segments)>0, 'The name should not be empty string.'
        if len(path_segments) > 1:
            #check if first is '/'
            if path_segments[0]=='': path_segments.pop(0)
            subfolders = string.join(path_segments[:-1],'/')
            root_object_name = path_segments[-1]
            self.log.debug('Root-subfolder: {0}'.format(subfolders))
            self.log.debug('Root-object name: {0}'.format(root_object_name))
            return (subfolders, root_object_name)
        else:
            root_object_name = path_segments[-1]
            return (None, root_object_name)

    def _get_directory(self,root_file, path):
        """
        Create and cd to the directory if given like a/b/c
        """
        root_file.cd()
        #subfolders = self._get_subfolders_and_name(path)[0]
        if path:
            self.log.debug('Creating root-subfolder {0}'.format(path))
            mkdir_res = root_file.mkdir(path)
            self.log.info('Root-subfolder {0} created with code = {1}'.format(path, mkdir_res))
            root_file.cd(path)
        else:  #no subfolder will be created
            root_file.cd()
        self.log.debug('Current directory: {0}'.format(gDirectory.GetPath()))


    def flush_basket(self):
        """
        Resets the basket content and delets the basket.
        """

        try:
            del self.root_fruit_basket
        except:
            raise RuntimeError, 'Basket cannot be flushed and destroyed! It even doesn\'t exist ...'
        else:
            self.log.info('Basket flushed!')
            return 0

    def dump_basket_to_file(self, file_name, access = 'UPDATE'):
        """
        Save what is in basket to a file. Create directories in the path if needed.
        """
        out_file = self.TFile_safe_open(file_name, access)
        out_file.cd()
        if self.DEBUG:
            self.pp.pprint(self.root_fruit_basket)

        for item_name in self.root_fruit_basket.keys():
            subfolders, root_object_name = self._get_subfolders_and_name(item_name)
            self._get_directory(out_file, subfolders)  #it will create and cd to the directory if given like a/b/c
            self.log.debug('Writing root-object: {0} Object name: {1} ; Object title: {2}'.format(self.root_fruit_basket[item_name],self.root_fruit_basket[item_name].GetName(),self.root_fruit_basket[item_name].GetTitle()))
            is_TTree = isinstance(self.root_fruit_basket[item_name],TTree)
            if is_TTree:
                self.log.debug('This is a TTree object : {0}'.format(self.root_fruit_basket[item_name]))
                copy_tree_name = self.root_fruit_basket[item_name].GetName()
                copy_tree_title = self.root_fruit_basket[item_name].GetTitle()
                tree_for_saving = self.root_fruit_basket[item_name].CloneTree(0)
                copy_res = tree_for_saving.CopyEntries(self.root_fruit_basket[item_name])
                tree_for_saving.SetNameTitle(copy_tree_name,copy_tree_title)
                write_res = tree_for_saving.Write()
            else:
                write_res = self.root_fruit_basket[item_name].Write()

            if write_res == 0 :
                self.log.error('The object {0} cannot be written into {1}'.format(item_name, gDirectory.GetPath()))
            else:
                self.log.info('The object {0} has been written into {1}'.format(item_name, gDirectory.GetPath()))

        out_file.Close()
        self.log.info('Saved the basket with {1} items into the file: {0}'.format(file_name, len(self.root_fruit_basket)))
        self.flush_basket()

        return 0
def prepare_asimov_toy_datasets_for_sync():
    #parseOptions()
    DEBUG = False
    if opt.verbosity!=10:
        os.environ['PYTHON_LOGGER_VERBOSITY'] =  str(opt.verbosity)
    if opt.verbosity >=4:
        DEBUG = True



    #1) RooDataSet with all processes for one sqrts and final state
    #- have RooCategory for sqrts and final state
        #chan  = [1_7, 2_7, 3_7, 1_8,2_8,3_8]
        #cat.defineType(chan[j],j); j=0..6
        #cat.setLabel(chan[j]);
    #2) finally append all toys to same RooDataSet

    log = Logger().getLogger("prepare_toy_datasets_for_sync", 10)

    toy_manager = ToyDataSetManager()
    #toy_manager.set_workspace_path(opt.ws_path)
    #path_to_tree, tree_variables, weight = "1", dataset_name = "my_dataset"):
    #shapes *        ch1_ch1  hzz4l_4muS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch2  hzz4l_4eS_7TeV.input.root w:$PROCESS
    #shapes *        ch1_ch3  hzz4l_2e2muS_7TeV.input.root w:$PROCESS
    #shapes *        ch2_ch1  hzz4l_4muS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch2  hzz4l_4eS_8TeV.input.root w:$PROCESS
    #shapes *        ch2_ch3  hzz4l_2e2muS_8TeV.input.root w:$PROCESS



    model = "trees_SM"
    #model = "trees_Mix_fa2"
    #model = "trees_Mix_fa3"
    toys_dir = "/afs/cern.ch/user/r/roko/wd_datacards/CreateFullSimToys/{0}".format(model)

    #Christoys:
    #toys_dir = "/afs/cern.ch/work/c/chmartin/public/ForEmbedded/Final_v2/"

    #chan_path_dict = {
            #'ch1_ch1' :  "{0}/Toys_CJLSTntuple_7TeV_4mu_*.root/SelectedTree".format(toys_dir),
            #'ch1_ch2' :  "{0}/Toys_CJLSTntuple_7TeV_4e_*.root/SelectedTree".format(toys_dir),
            #'ch1_ch3' :  "{0}/Toys_CJLSTntuple_7TeV_2e2mu_*.root/SelectedTree".format(toys_dir),
            #'ch2_ch1' :  "{0}/Toys_CJLSTntuple_8TeV_4mu_*.root/SelectedTree".format(toys_dir),
            #'ch2_ch2' :  "{0}/Toys_CJLSTntuple_8TeV_4e_*.root/SelectedTree".format(toys_dir),
            #'ch2_ch3' :  "{0}/Toys_CJLSTntuple_8TeV_2e2mu_*.root/SelectedTree".format(toys_dir)
        #}
        
    chan_path_dict = {
        'ch1_ch1' :  "{0}/DTree_7TeV_4mu_*.root/selectedEvents".format(toys_dir),
        'ch1_ch2' :  "{0}/DTree_7TeV_4e_*.root/selectedEvents".format(toys_dir),
        'ch1_ch3' :  "{0}/DTree_7TeV_2e2mu_*.root/selectedEvents".format(toys_dir),
        'ch2_ch1' :  "{0}/DTree_8TeV_4mu_*.root/selectedEvents".format(toys_dir),
        'ch2_ch2' :  "{0}/DTree_8TeV_4e_*.root/selectedEvents".format(toys_dir),
        'ch2_ch3' :  "{0}/DTree_8TeV_2e2mu_*.root/selectedEvents".format(toys_dir)
    }
    
    pp = pprint.PrettyPrinter(indent=4)
    #if DEBUG:
        #log.debug('Initial dictionary:')
        #pp.pprint(chan_path_dict)
    channel_name = sorted(chan_path_dict.keys())
    cat = RooCategory("CMS_channel","CMS_channel")
    for cat_idx, cat_name in enumerate( channel_name ):
            cat.defineType(cat_name,cat_idx);
            cat.setLabel(cat_name);
    if DEBUG:
            log.debug('Category : {0}'.format(cat_idx))
            cat.Print('v')

    my_vars = []
    if ("fa3" in model) and ("fa2" in model):
        my_vars = ['D_bkg','D_0m','D_cp', 'D_0hp','D_int','Weight','mass4l']
    elif "fa3" in model:
        my_vars = ['D_bkg','D_0m','D_cp','Weight','mass4l']
    elif "fa2" in model:
        my_vars = ['D_bkg', 'D_0hp','D_int','Weight','mass4l']
    else:
        my_vars = ['D_bkg','D_0m','D_cp', 'D_0hp','D_int','Weight','mass4l']

    my_rrv = dict()
    my_rrv['D_bkg']     = RooRealVar('D_bkg','D_bkg', 0,1.)


    my_rrv['D_0m']      = RooRealVar('D_0m','D_0m', 0,1.)
    my_rrv['D_cp']      = RooRealVar('D_cp','D_cp', -0.5,0.5)

    my_rrv['D_0hp']      = RooRealVar('D_0hp','D_0hp', 0,1.)
    my_rrv['D_int']      = RooRealVar('D_int','D_int', -0.2,1)

    my_rrv['mass4l']    = RooRealVar('mass4l','mass4l', 100,1000.)
    my_rrv['Weight']    = RooRealVar('Weight','Weight', 1.)


    my_rrv['D_bkg'].setBins(5)
    #my_rrv['D_bkg'].setBins(50)
    my_rrv['D_0m'].setBins(50)
    my_rrv['D_cp'].setBins(50)
    my_rrv['D_0hp'].setBins(50)
    my_rrv['D_int'].setBins(50)



    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')



    if os.path.exists(opt.output_filename):
        log.debug("Removing file: {0}".format(opt.output_filename))
        os.remove(opt.output_filename)

    import lib.RootHelpers.RootHelperBase as rhb
    root_helper = rhb.RootHelperBase()


    my_arg_set = RooArgSet()
    for var_name in my_vars:
        #TODO implement check that branch exist
        #my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
        my_arg_set.add(my_rrv[var_name])
    my_arg_set.add(cat)
    if DEBUG:
        log.debug('RooArgSet is now:')
        my_arg_set.Print('v')

    #import asimov dataset
    for cat_idx, cat_name in enumerate( channel_name ):
            my_path_to_tree = chan_path_dict[cat_name]
            log.debug("my_path_to_tree: {0} ".format(my_path_to_tree ))

            the_dataset = toy_manager.get_dataset_from_tree(path_to_tree = my_path_to_tree, tree_variables = my_vars,
                                                                  weight="(mass4l<140.6&&mass4l>105.6)", dataset_name="toys/toy_asimov_v0_{0}".format(cat_idx), basket = False, weight_var_name = "Weight")

            the_dataset_with_cat = RooDataSet("toys/toy_asimov_{0}".format(cat_idx),"toys/toy_asimov_{0}".format(cat_idx), my_arg_set, RooFit.Index(cat), RooFit.Import(cat_name,the_dataset), RooFit.WeightVar("Weight"))
            log.debug('RooDataSet the_dataset_with_cat contains {0} events'.format(the_dataset_with_cat.sumEntries()))
            the_dataset_with_cat.Print()
            if cat_idx==0:
                combined_dataset = the_dataset_with_cat
            else:
                combined_dataset.append(the_dataset_with_cat)

    log.debug('RooDataSet combined_dataset contains {0} events'.format(combined_dataset.sumEntries()))
    combined_dataset.Print()
    combined_dataset.Print("v")
    #combined_dataset.SetNameTitle("toys/embedded_asimov","toys/embedded_asimov")
    #toy_manager.add_to_basket(combined_dataset)
    toy_manager.add_to_basket(combined_dataset, new_name = "toys/embedded_asimov", new_title = "toys/embedded_asimov")
    toy_manager.dump_datasets_to_file(opt.output_filename,'UPDATE')  #this one can receive both
예제 #18
0
class RootPlottersBase(RootHelperBase):
  """Class as a base class for many plotters containing the structure, common functions...
  """

  def __init__(self,name = "plotters_base_functionality" ):
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.name = name
        #ROOT.gSystem.AddIncludePath("-I$ROOFITSYS/include/");
        #ROOT.gROOT.ProcessLine(".L tdrstyle.cc")
        #from ROOT import setTDRStyle
        #ROOT.setTDRStyle(True)

        #ROOT.gStyle.SetPalette(1)
        #ROOT.gStyle.SetOptStat(0)
        self.copy_to_web_dir = False
        self.webdir = ""
        self.save_extensions = ['png','pdf','eps']
        #self.pp = pprint.PrettyPrinter(indent=4)

  def setName(self, newname): self.name = newname

  def make_plot(self, data):
        print "This is a default method for plotters. It has to be implemented in derived classes"
        pass

  def setCopyToWebDir(self,doCopy=False,webdir=""):
        if doCopy:
            self.copy_to_web_dir = True
            if webdir:
                self.webdir = webdir
            else:
                raise ValueError, "You have to provide a webdir path if you want to copy the files."
        else:
            self.copy_to_web_dir = False
            self.webdir = ""
        return 0

  def get_webdir(self):
        return self.webdir

  def copy_index_html(self, full_path_dir):
        """
        Walk to all subdirs and put index.php if not present.
        """
        for root, dirs, files in os.walk(full_path_dir):
            #print root
            if not os.path.exists("{0}/index.php".format(root)) :
                #shutil.copy("/afs/cern.ch/user/r/roko/www/html/index.php",root)
                self.put_index_php_structure(root)



  def doCopyToWebDir(self,file_name, newname=""):
        if newname=="":
            newname = file_name
        if self.webdir :
            full_path = self.webdir+"/"+newname
            full_path_dir =  os.path.dirname(full_path)
            #misctools.make_sure_path_exists(self.webdir)
            misctools.make_sure_path_exists(full_path_dir)
            #if not os.path.exists("{0}/index.php".format(self.webdir)) :
            self.copy_index_html(self.webdir)
            if not os.path.exists("{0}/index.php".format(full_path_dir)) :
                #shutil.copy("/afs/cern.ch/user/r/roko/www/html/index.php",full_path_dir)
                self.put_index_php_structure(full_path_dir)
            self.log.debug("Copying {0} to webdir {1}".format(file_name,full_path))
            shutil.copy(file_name,full_path)
            self.log.info("Copied {0} to webdir {1}".format(file_name,full_path))
        else :
            raise ValueError, "You have to provide a webdir path if you want to copy the files."
        return 0


  def save(self, canv, plot_name, extensions=['png','root']):
        #extensions = ['.png','.pdf','.eps','.root']
        if len(extensions)==0:
            extensions=['']
        for ext in extensions:
            postfix = "."+ext
            if ext=='':
                postfix=''
            canv.SaveAs(plot_name+postfix)
            self.log.debug("Saving to: {0}.{1}".format(plot_name,ext))
            if self.copy_to_web_dir :
                self.doCopyToWebDir(plot_name+postfix)


  def XtoNDC(self, x):
        gPad.Update() #this is necessary!
        return (x - gPad.GetX1())/(gPad.GetX2()-gPad.GetX1())


  def YtoNDC(self, y):
        gPad.Update() #this is necessary!
        return (y - gPad.GetY1())/(gPad.GetY2()-gPad.GetY1())

  def get_histDim(self,input_hist):
        n_dim = None
        if (isinstance(input_hist, TH3)): n_dim = 3
        elif (isinstance(input_hist, TH2)): n_dim = 2
        elif (isinstance(input_hist, TH1)): n_dim = 1
        else:
            raise TypeError, '[RootPlottersBase::get_histDim] The input to this function should be a histogram. Check your inputs!'
        return n_dim

  def put_index_php_structure(self, www_dir):
      """
      Copies the structure of index.php file to the www_dir.
      """
      index_php = """
        <html>
            <head>
                        <title><?php echo getcwd(); ?></title>
                        <style type='text/css'>
                                body {
                                    font-family: "Candara", sans-serif;
                                    font-size: 9pt;
                                    line-height: 10.5pt;
                                }
                                div.pic h3 { 
                                    font-size: 11pt;
                                    margin: 0.5em 1em 0.2em 1em;
                                }
                                div.pic p {
                                    font-size: 11pt;
                                    margin: 0.2em 1em 0.1em 1em;
                                }
                                div.pic {
                                    display: block;
                                    float: left;
                                    background-color: white;
                                    border: 1px solid #ccc;
                                    padding: 2px;
                                    text-align: center;
                                    margin: 2px 10px 10px 2px;
                                    -moz-box-shadow: 7px 5px 5px rgb(80,80,80);    /* Firefox 3.5 */
                                    -webkit-box-shadow: 7px 5px 5px rgb(80,80,80); /* Chrome, Safari */
                                    box-shadow: 7px 5px 5px rgb(80,80,80);         /* New browsers */  
                                }
                                a { text-decoration: none; color: rgb(80,0,0); }
                                a:hover { text-decoration: underline; color: rgb(255,80,80); }
                                figure {
                                        display: table;
                                        width: 1px; /* This can be any width, so long as it's narrower than any image */
                                        }
                                img, figcaption {
                                        display: table-row;
                                        }
                        </style>

            </head>


            <body>
                    <h4>&copy G. Petrucciani (CERN), R. Plestina (IHEP-CAS)</h4>

                    <?PHP
                        if (file_exists("title.txt")){
                            $page_title = file_get_contents("title.txt");
                            print "<h1>$page_title</h1>";
                        }
                        print "<h3>".getcwd()."</h3>";
                        if (file_exists("basic.info")){
                            print "<h2><a name='basic_info'>Basic information</a></h2>";
                            $file_handle = fopen("basic.info", "rb");

                            while (!feof($file_handle) ) {
                                    $line_of_text = fgets($file_handle);
                                    $parts = explode('=', $line_of_text);
                                    print $parts[0] . $parts[1]. "<BR>";
                            }
                            fclose($file_handle);
                        }
                    ?>
                    

                    
                    <h2><a name="plots">Plots</a></h2>
                    <p>
                        <form>Filter: 
                            <input type="text" name="match" size="30" value="<?php if (isset($_GET['match'])) print htmlspecialchars($_GET['match']);  ?>" /><input type="Submit" value="Search" />
                        </form>
                    </p>

                    <div>
                            <?PHP
                            // ____________________________________________________________________________________________________________
                            $displayed = array();
                            if ($_GET['noplots']) {
                                print "Plots will not be displayed.\n";
                            } else {
                                $other_exts = array('.pdf', '.cxx', '.eps', '.root', '.txt','.C','.gif');
                                $filenames = glob("*.png"); sort($filenames);
                                foreach ($filenames as $filename) {
                                    if (isset($_GET['match']) && !fnmatch('*'.$_GET['match'].'*', $filename)) continue;
                                    array_push($displayed, $filename);
                                    print "<div class='pic'>\n";
                                    print "<h3><a href=\"$filename\">$filename</a></h3>";
        //                             print "<a href=\"$filename\"><img src=\"$filename\" style=\"border: none; width: 300px; \"></a>";
                                    $others = array();
                                    $caption_text = '';
                                    foreach ($other_exts as $ex) {
                                        $other_filename = str_replace('.png', $ex, $filename);
                                        if (file_exists($other_filename)) {
        //                                     array_push($others, "<a class=\"file\" href=\"$other_filename\">[" . $ex . "]</a>");
                                            if ($ex != '.txt') {
                                                array_push($others, "<a class=\"file\" href=\"$other_filename\">[" . $ex . "]</a>");
                                                array_push($displayed, $other_filename);
                                                
                                            }
                                            
                                            else {
                                                $caption_text = file_get_contents($other_filename);
                                            }
                                        }
                                    }
        //                             print "<a href=\"$filename\"><figure><img src=\"$filename\" style=\"border: none; width: 300px; \"><figcaption>$caption_text</figcaption></figure></a>";
                                    print "<figure><a href=\"$filename\"><img src=\"$filename\" style=\"border: none; width: 300px; \"></a><figcaption>$caption_text</figcaption></figure>";
                                    if ($others) print "<p>View as ".implode(', ',$others)."</p>";
                                
                                    
                                    print "</div>";
                                }
                            }
                            // ____________________________________________________________________________________________________________
                            ?>

                    </div>
                    <div style="display: block; clear:both;">
                            <h2><a name="files">Other</a></h2>
                            <ul>
                            <?PHP
                            // ____________________________________________________________________________________________________________
                            foreach (glob("*") as $filename) {
                                if ($_GET['noplots'] || !in_array($filename, $displayed)) {
                                    if (isset($_GET['match']) && !fnmatch('*'.$_GET['match'].'*', $filename)) continue;
                                    if ($filename=='index.php') continue;
                                    if (is_dir($filename)) {
                                        print "<b><li><a href=\"$filename\">$filename</a></li></b>";

                                    } else {
                                        print "<li><a href=\"$filename\">$filename</a></li>";
                                    }
                                }
                            }
                            // ____________________________________________________________________________________________________________
                            ?>
                            </ul>
                    </div>
        
            </body>
        </html>

            """
      with open(www_dir+'/index.php','w') as f:
          f.write(index_php)
          
            
class ToyDataSetManager(RootHelperBase):
    def __init__(self):
        """Initialize whatever is needed"""
        self.my_logger = Logger()
        #self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
        #}
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #initialize RooFit
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        self.output_filename = 'worskapce_with_embedded_toys.root'

    def set_toys_path(self, toys_path):
        """
        Set the path for the toy dataset.There is aleays one active toy held in self.toys.
        """
        self.toys_path = toys_path
        self.toys = self.get_object(path=toys_path,
                                    object_type=RooAbsData,
                                    clone=False)

    def set_workspace_path(self, ws_path):
        """
        Set the path for the workspace where toys will be included.
        There is only one workspace that can be active in the class.
        """
        self.ws = self.get_object(path=ws_path,
                                  object_type=RooWorkspace,
                                  clone=False)

    def set_output_file_name(self, output_filename):
        """
        Set the name of the output root file.
        """
        self.output_filename = output_filename

    def set_new_toys_name(self, new_toys_name):
        """
        Set name for toys in the workspace
        """
        self.new_toys_name = new_toys_name

    def import_toys_to_ws(self,
                          ws_path=None,
                          toys_path=None,
                          output_filename=None,
                          new_toys_name=None):
        """
        Imports a given toys dataset (or multiple toys) into the workspace and dumps to new root file.

        Parameters:
        -----------
        ws_path   : path to exisitng workspace (string)
        toys_path : path or list of paths to toys.TODO add regexp parsing to import matching toys.
        output_filename : file name of the output workspace
        new_toys_name : in case of one toy import, a new name can be set. In case of list, the name is set
                        to be the same as in the source file.

        Returns:
        --------
        Returns 0 in case it goes trough without erorrs(?).

        """
        #TODO set checks for the input provided
        if ws_path:
            self.set_workspace_path(ws_path)

        if output_filename:
            self.set_output_file_name(output_filename)
        if new_toys_name:
            self.set_new_toys_name(new_toys_name)

        try:
            self.ws
        except AttributeError:
            raise AttributeError, 'You need to provide workspace path.'

        if toys_path:
            toys_path_list = []
            if isinstance(toys_path, list):
                toys_path_list = toys_path
            elif isinstance(toys_path, str):
                toys_path_list = [toys_path]

            for the_toy in toys_path_list:
                self.set_toys_path(the_toy)
                toys_name = self.get_paths(the_toy)[
                    -1]  #just getthe name of toys object in the root file.
                self.log.info(
                    'Setting toys name in workspace to: {0}'.format(toys_name))
                self.set_new_toys_name(toys_name)
                self.toys.SetName(self.new_toys_name)
                getattr(self.ws, 'import')(self.toys)
                self.log.info(
                    "Imported DataSet '{0}' into workspace '{1}'.".format(
                        self.toys.GetName(), self.ws.GetName()))
        else:
            try:
                self.toys
            except AttributeError:
                raise AttributeError, 'You need to provide toys path.'

            try:
                self.new_toys_name
            except AttributeError:
                toys_name = self.get_paths(self.toys_path)[
                    -1]  #just getthe name of toys object in the root file.
                self.log.info(
                    'Setting toys name in workspace to: {0}'.format(toys_name))
                self.set_new_toys_name(toys_name)

            self.toys.SetName(self.new_toys_name)
            getattr(self.ws, 'import')(self.toys)
            self.log.info(
                "Imported DataSet '{0}' into workspace '{1}'.".format(
                    self.toys.GetName(), self.ws.GetName()))

        self.ws.data(self.toys.GetName()).Print()
        self.ws.data(self.toys.GetName()).Print("v")

        #write workspace
        self.ws.writeToFile(self.output_filename)
        self.log.info("Writing workspace '{0}' to file {1}".format(
            self.ws.GetName(), self.output_filename))

        return 0

    def set_dataset_name(self, dataset_name):
        """
        Set name of the dataset in workspace.
        """
        self.dataset_name = dataset_name

    def import_dataset_to_ws(self,
                             dataset,
                             workspace,
                             output_filename=None,
                             new_name=None):
        """
        Import dataset to worspace workspace.
        """
        if new_name:
            dataset.SetName(new_name)
        if output_filename:
            self.set_output_file_name(output_filename)

        self.log.info(
            "Imported DataSet '{0}' into workspace '{1}' and written to file {2}."
            .format(dataset.GetName(), workspace.GetName(),
                    self.output_filename))
        pass

    def set_workspace(self, workspace):
        """
        Provide workspace from path naload it to self.ws or
        provide directly workspace and load it to self.ws
        """
        if isinstance(workspace, RooWorkspace):
            self.ws = workspace
            self.log.debug('Loaded in workspace {0}.'.format(
                self.ws.GetName()))
        elif isinstance(workspace, str):
            self.set_workspace_path(self, workspace)
            self.log.debug(
                'Loaded in workspace {0} from path: '.format(workspace))

    def dump_datasets_to_file(self, output_filename=None, access='RECREATE'):
        """
        Write all datasets collected in the basket(RootHelperBase) to a file.
        """
        if output_filename:
            self.set_output_file_name(output_filename)
        self.dump_basket_to_file(self.output_filename, access)
        self.log.info(
            'All items from the basket have been written to file: {0}'.format(
                self.output_filename))
        return 0

    def get_dataset_from_tree(self,
                              path_to_tree,
                              tree_variables,
                              weight="1==1",
                              weight_var_name=0,
                              dataset_name="my_dataset",
                              basket=True,
                              category=None):
        """
        Creates RooDataSet from a plain root tree given:
        - variables name list
        - weight expression. It works in the same way as TTree cut.
        Returns:
        --------
        - RooDataSet
        - also fills the basket with datasets (basket inhereted from RootHelperBase class)

        TODO
        ----
        - add implementation for category setting(check in prepare_toy_datasets_for_sync)
            - check if adding toy dataset to each channel workspace individually behaves well
              after combineCards.py.
        """

        #make RooRelVars from tree_variables
        my_arg_set = RooArgSet()
        my_rrv = dict()
        for var_name in tree_variables:
            #TODO implement check that branch exist
            my_rrv[var_name] = RooRealVar(var_name, var_name, -999999999,
                                          999999999)
            my_arg_set.add(my_rrv[var_name])
        if self.DEBUG:
            self.log.debug('RooArgSet is now:')
            my_arg_set.Print()

        #get the tree from path_to_tree
        my_tree = self.get_TTree(path_to_tree, cut=weight)
        self.log.debug('Selected tree contains {0} events'.format(
            my_tree.GetEntries()))
        #create RooDataSet and reduce tree if needed
        #self.dataset_from_tree =  RooDataSet(dataset_name, dataset_name, my_tree, my_arg_set, weight).reduce(my_arg_set)
        self.dataset_from_tree = RooDataSet(dataset_name, dataset_name,
                                            my_tree, my_arg_set)
        #self.dataset_from_tree =  RooDataSet(dataset_name, dataset_name, my_tree, my_arg_set, "", weight_var_name)
        #data[j]=new RooDataSet(Form("data%d",j),Form("data%d",j),outTree,RooArgSet(rCMS_zz4l_widthKD,rCMS_zz4l_widthMass,rweightFit),"","_weight_");
        self.log.debug('RooDataSet contains {0} events'.format(
            self.dataset_from_tree.sumEntries()))
        #.reduce(ROOT.RooArgSet(self.D0))
        self.current_arg_set = my_arg_set

        #add dataset to basket
        if basket:
            self.add_to_basket(self.dataset_from_tree,
                               new_name=dataset_name,
                               new_title=dataset_name)

        return self.dataset_from_tree

    def get_current_arg_set(self):
        """
        Return last dataset setup used by get_dataset_from_tree().
        """
        return self.current_arg_set
class ToyDataSetManager(RootHelperBase):

    def __init__(self):
        """Initialize whatever is needed"""
        self.my_logger = Logger()
        #self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.log = self.my_logger.getLogger(self.__class__.__name__, 10)
                        #}
        self.DEBUG = self.my_logger.is_debug()
        self.pp = pprint.PrettyPrinter(indent=4)

        #initialize RooFit
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        self.output_filename = 'worskapce_with_embedded_toys.root'



    def set_toys_path(self,toys_path):
        """
        Set the path for the toy dataset.There is aleays one active toy held in self.toys.
        """
        self.toys_path = toys_path
        self.toys = self.get_object(path = toys_path, object_type = RooAbsData,  clone=False)


    def set_workspace_path(self,ws_path):
        """
        Set the path for the workspace where toys will be included.
        There is only one workspace that can be active in the class.
        """
        self.ws = self.get_object(path = ws_path, object_type = RooWorkspace,  clone=False)

    def set_output_file_name(self,output_filename):
        """
        Set the name of the output root file.
        """
        self.output_filename = output_filename


    def set_new_toys_name(self, new_toys_name):
        """
        Set name for toys in the workspace
        """
        self.new_toys_name = new_toys_name


    def import_toys_to_ws(self, ws_path = None, toys_path = None, output_filename = None, new_toys_name = None):
        """
        Imports a given toys dataset (or multiple toys) into the workspace and dumps to new root file.

        Parameters:
        -----------
        ws_path   : path to exisitng workspace (string)
        toys_path : path or list of paths to toys.TODO add regexp parsing to import matching toys.
        output_filename : file name of the output workspace
        new_toys_name : in case of one toy import, a new name can be set. In case of list, the name is set
                        to be the same as in the source file.

        Returns:
        --------
        Returns 0 in case it goes trough without erorrs(?).

        """
        #TODO set checks for the input provided
        if ws_path:
            self.set_workspace_path(ws_path)

        if output_filename:
            self.set_output_file_name(output_filename)
        if new_toys_name:
            self.set_new_toys_name(new_toys_name)

        try:
            self.ws
        except AttributeError:
            raise AttributeError, 'You need to provide workspace path.'



        if toys_path:
            toys_path_list = []
            if isinstance(toys_path,list):
                toys_path_list = toys_path
            elif isinstance(toys_path,str):
                toys_path_list = [toys_path]

            for the_toy in toys_path_list:
                self.set_toys_path(the_toy)
                toys_name = self.get_paths(the_toy)[-1]  #just getthe name of toys object in the root file.
                self.log.info('Setting toys name in workspace to: {0}'.format(toys_name))
                self.set_new_toys_name(toys_name)
                self.toys.SetName(self.new_toys_name)
                getattr(self.ws,'import')(self.toys)
                self.log.info("Imported DataSet '{0}' into workspace '{1}'.".format(self.toys.GetName(), self.ws.GetName()))
        else:
            try:
                self.toys
            except AttributeError:
                raise AttributeError, 'You need to provide toys path.'

            try:
                self.new_toys_name
            except AttributeError:
                toys_name = self.get_paths(self.toys_path)[-1]  #just getthe name of toys object in the root file.
                self.log.info('Setting toys name in workspace to: {0}'.format(toys_name))
                self.set_new_toys_name(toys_name)

            self.toys.SetName(self.new_toys_name)
            getattr(self.ws,'import')(self.toys)
            self.log.info("Imported DataSet '{0}' into workspace '{1}'.".format(self.toys.GetName(), self.ws.GetName()))

        self.ws.data(self.toys.GetName()).Print()
        self.ws.data(self.toys.GetName()).Print("v")

        #write workspace
        self.ws.writeToFile(self.output_filename)
        self.log.info("Writing workspace '{0}' to file {1}".format(self.ws.GetName(), self.output_filename))

        return 0

    def set_dataset_name(self, dataset_name):
        """
        Set name of the dataset in workspace.
        """
        self.dataset_name = dataset_name

    def import_dataset_to_ws(self, dataset, workspace, output_filename = None, new_name = None):
        """
        Import dataset to worspace workspace.
        """
        if new_name:
            dataset.SetName(new_name)
        if output_filename:
            self.set_output_file_name(output_filename)


        self.log.info("Imported DataSet '{0}' into workspace '{1}' and written to file {2}.".format(dataset.GetName(), workspace.GetName(), self.output_filename))
        pass

    def set_workspace(self,workspace):
        """
        Provide workspace from path naload it to self.ws or
        provide directly workspace and load it to self.ws
        """
        if isinstance(workspace,RooWorkspace):
            self.ws = workspace
            self.log.debug('Loaded in workspace {0}.'.format(self.ws.GetName()))
        elif isinstance(workspace,str):
            self.set_workspace_path(self,workspace)
            self.log.debug('Loaded in workspace {0} from path: '.format(workspace))


    def dump_datasets_to_file(self,output_filename = None, access='RECREATE'):
        """
        Write all datasets collected in the basket(RootHelperBase) to a file.
        """
        if output_filename:
            self.set_output_file_name(output_filename)
        self.dump_basket_to_file(self.output_filename, access)
        self.log.info('All items from the basket have been written to file: {0}'.format(self.output_filename))
        return 0


    def get_dataset_from_tree(self,path_to_tree, tree_variables, weight = "1==1", weight_var_name=0, dataset_name = "my_dataset", basket=True, category = None):
        """
        Creates RooDataSet from a plain root tree given:
        - variables name list
        - weight expression. It works in the same way as TTree cut.
        Returns:
        --------
        - RooDataSet
        - also fills the basket with datasets (basket inhereted from RootHelperBase class)

        TODO
        ----
        - add implementation for category setting(check in prepare_toy_datasets_for_sync)
            - check if adding toy dataset to each channel workspace individually behaves well
              after combineCards.py.
        """

        #make RooRelVars from tree_variables
        my_arg_set = RooArgSet()
        my_rrv = dict()
        for var_name in tree_variables:
            #TODO implement check that branch exist
            my_rrv[var_name] = RooRealVar(var_name,var_name,-999999999,999999999)
            my_arg_set.add(my_rrv[var_name])
        if self.DEBUG:
            self.log.debug('RooArgSet is now:')
            my_arg_set.Print()

        #get the tree from path_to_tree
        my_tree = self.get_TTree(path_to_tree, cut = weight)
        self.log.debug('Selected tree contains {0} events'.format(my_tree.GetEntries()))
        #create RooDataSet and reduce tree if needed
        #self.dataset_from_tree =  RooDataSet(dataset_name, dataset_name, my_tree, my_arg_set, weight).reduce(my_arg_set)
        self.dataset_from_tree =  RooDataSet(dataset_name, dataset_name, my_tree, my_arg_set)
        #self.dataset_from_tree =  RooDataSet(dataset_name, dataset_name, my_tree, my_arg_set, "", weight_var_name)
        #data[j]=new RooDataSet(Form("data%d",j),Form("data%d",j),outTree,RooArgSet(rCMS_zz4l_widthKD,rCMS_zz4l_widthMass,rweightFit),"","_weight_");
        self.log.debug('RooDataSet contains {0} events'.format(self.dataset_from_tree.sumEntries()))
        #.reduce(ROOT.RooArgSet(self.D0))
        self.current_arg_set = my_arg_set

        #add dataset to basket
        if basket:
            self.add_to_basket(self.dataset_from_tree, new_name = dataset_name, new_title = dataset_name)

        return self.dataset_from_tree

    def get_current_arg_set(self):
        """
        Return last dataset setup used by get_dataset_from_tree().
        """
        return self.current_arg_set
 def __init__(self):
     self.log = Logger().getLogger(self.__class__.__name__, 10)
     self.pp = pprint.PrettyPrinter(indent=4)
     self.DEBUG = True
예제 #22
0
class TotalPdfPlotter(PlotPolisher, RootPlottersBase):
  """Plots PDF from input templates without using nusance. 
     This is because, sometimes it happens that the templates
     are negative for some values of parameter. In that case
     the RooFit gives errors. 
  """
  def __init__(self,name = "templates" ):
        self.log = Logger().getLogger(self.__class__.__name__, 10)
        self.name = name
        #ROOT.gSystem.AddIncludePath("-I$ROOFITSYS/include/");
        ROOT.gROOT.ProcessLine(".L tdrstyle.cc")
        from ROOT import setTDRStyle
        ROOT.setTDRStyle(True)
        
        ROOT.gStyle.SetPalette(1)
        ROOT.gStyle.SetOptStat(0)
        self.copy_to_web_dir = False
        self.webdir = ""
        self.pp = pprint.PrettyPrinter(indent=4)
        self.k2k1_ratio=0
        self.k3k1_ratio=0
        self.total_pdf=0
        
  def set_k2k1_and_k3k1(self, k2k1, k3k1):
        self.k2k1_ratio = k2k1
        self.k3k1_ratio = k3k1
      
  def get_scale_factors(self):
            
        factors = {'lambda12_cosP' : 0.0,
            'lambda13_cosP' : 0.0,
            'lambda23_cosN' : 0.0,
            'lambda23_cosP' : 0.0,
            'gamma33' : 0.034,
            'gamma13' : 0.0,
            'gamma12' : -0.290992,
            'gamma11' : 1.0,
            'lambda13_cosN' : 0.0,
            'lambda12_sinN' : 0.0,
            'lambda12_cosN' : 0.581984,
            'gamma22' : 0.09,
            'gamma23' : 0.0,
            }
            
        nominator={
            'ggH_shape'      :             '{0}'.format(factors['gamma11']),
            'gg0Ph_shape'    :     '{0}*{1}*{1}'.format(factors['gamma22'],self.k2k1_ratio),     # @0 = k2k1_ratio
            'gg0M_shape'     :     '{0}*{1}*{1}'.format(factors['gamma33'],self.k3k1_ratio),     # @1 = k3k1_ratio
            'ggInt_12P_shape':         '{0}*{1}'.format(factors['lambda12_cosP'],self.k2k1_ratio),  
            'ggInt_12N_shape':    '{0}*{1}*(-1)'.format(factors['lambda12_cosN'],self.k2k1_ratio),
            'ggInt_13P_shape':         '{0}*{1}'.format(factors['lambda13_cosP'],self.k3k1_ratio),  
            'ggInt_13N_shape':    '{0}*{1}*(-1)'.format(factors['lambda13_cosN'],self.k3k1_ratio),  
            'ggInt_23P_shape':     '{0}*{1}*{2}'.format(factors['lambda23_cosP'],self.k2k1_ratio,self.k3k1_ratio),  
            'ggInt_23N_shape':'{0}*{1}*{2}*(-1)'.format(factors['lambda23_cosN'],self.k2k1_ratio,self.k3k1_ratio)  
            }
            
        denominator  =   "1"
        #denominator_geoloc = "({0}+{1}*@0*@0+{2}*@1*@1+2*(({3}*@0)+({4}*@1)+({5}*@0*@1)))" ### added factor 2 for mixed terms
        ##denominator = denominator_geoloc.format(factors['gamma11'],factors['gamma22'],factors['gamma33'],
                                                ##factors['gamma12'],factors['gamma13'],factors['gamma23'])
        #denominator = "{0}+{1}+{2}".format(_getDenominatorSegment(1,denominator_geoloc),_getDenominatorSegment(2,denominator_geoloc),_getDenominatorSegment(3,denominator_geoloc))      
        #denominator = "0.354301097432*(1.0+0.0*@0*@0+0.034*@1*@1+(0.0*@0)+(0.0*@1)+(0.0*@0*@1))
                      #+0.184149324322*(1.0+0.0*@0*@0+0.034*@1*@1+(0.0*@0)+(0.0*@1)+(0.0*@0*@1))
                      #+0.461549578246*(1.0+0.0*@0*@0+0.040*@1*@1+(0.0*@0)+(0.0*@1)+(0.0*@0*@1))"
        norm={}
        for nom in nominator.keys():    
            norm[nom] = eval("({0})/({1})".format(nominator[nom], denominator))
            self.log.debug('Norm for {0}  = {1}'.format(nom, norm[nom]))    
            
        return norm

  def make_animated_gif(self, list_for_animated_gif, name_animated_gif, delay=100, loop=True):
        #import lib.util.MiscTools as misctools
        try:
            delay = int(delay)
        except ValueError:
            raise ValueError, "Delay must be an integer."
        
        loop_string = ""
        if loop:
            loop_string = " -loop 0"
            
        self.log.info('Creating animated gif: {0}'.format(name_animated_gif))    
        misctools.processCmd("convert -delay {2} {0}{3} {1} ".format(" ".join("%s"%(one_gif) for one_gif in list_for_animated_gif), name_animated_gif, delay, loop_string))    
        self.doCopyToWebDir(name_animated_gif)
        
  def makePlot(self, data):

        #self.c_big = ROOT.TCanvas("c_big","Template Components",1200,1200)
        #self.c_big.Divide(len())
        self.c1 =ROOT.TCanvas("cc1","Templates",1000,800)
        #pdf_components = {}
        self.c1.cd()
        ROOT.gPad.SetRightMargin(0.2)
        
        try:
            data['content']
        except KeyError:
            raise KeyError, "Canvas \'content\' dictionary is not provided in config file."
            
        try:
            data['setup']
        except:
            print "@@@@ Canvas \'setup\' dictionary is not provided. "
            self.setup_exist=False
        else:
            self.arrangeCanvas(self.c1, data['setup'])
            self.setup_exist=True
            
        
        try:
            self.leg
        except AttributeError:
            self.leg_exist=False
        else:
            self.leg_exist=True
        print data['setup']['add_text']
        try:
            data['setup']['add_text']
        except KeyError:
            self.add_text_exist=False
        else:
            self.add_text_exist=True
        
        i=0
        for plot in data['content']:
            
                list_for_animated_gif=[]
                list_for_animated_gif_components=[]
                
                
                try:
                    plot['POI']
                except KeyError:
                    raise KeyError, "You chould provide POI dictionary with values in your confguration."
                else:
                    self.plot_total_pdf=True  
                    if isinstance(plot['POI'], dict):
                        if "k2k1_ratio" in plot['POI'].keys():
                            k2k1_list = list(plot['POI']['k2k1_ratio'])
                            self.log.debug('k2k1_list = {0}'.format(", ".join([str(k) for k in k2k1_list])))
                        if "k3k1_ratio" in plot['POI'].keys():
                            k3k1_list = list(plot['POI']['k3k1_ratio'])
                            self.log.debug('k3k1_list = {0}'.format(", ".join([str(k) for k in k3k1_list])))
                
                for k2k1 in k2k1_list:
                    for k3k1 in k3k1_list:
                        self.set_k2k1_and_k3k1(k2k1, k3k1)
                        self.log.debug('Seting parameters to k2k1 = {0} and k3k1 = {1}'.format(self.k2k1_ratio,self.k3k1_ratio) )
                        
                        n_components = len(plot['histos'])+1 #we include the total pdf into computation
                        self.c_big = ROOT.TCanvas("c_big","Template Components",int(500*n_components),400)
                        #self.c_big = ROOT.TCanvas("c_big","Template Components",1600,800)
                        self.c_big.Divide(n_components,1)
                        
                        
                        self.log.debug("Canvas is divided to N = {0}".format(n_components))
                        pdf_components = cols.OrderedDict()
                        pdf_components_minmax = cols.OrderedDict()
                        #pdf_components_order = []
                        
                        for fs in data['setup']['final_states']:
                            print 200*"_"
                            theTemplFile = plot['file']+"_"+fs+".root"
                            
                            if DEBUG:
                                pp.pprint(plot)
                        
                            f = TFile(theTemplFile,"READ")
                            self.log.debug("Running on file: {0}".format(theTemplFile))
                            norm= self.get_scale_factors()
                            
                            for i_pdf in range(len(plot['histos'])):
                                if i_pdf==0:
                                    total_pdf = f.Get(plot['histos'][i_pdf])
                                    total_pdf.Scale(norm[plot['histos'][i_pdf]])
                                    pdf_components[plot['histos'][i_pdf]] = copy.deepcopy(total_pdf)
                                    self.log.debug('TOTAL_PDF -> Picking up the first term {0} and the scale {1}. The histo is TH2: {2}'.format(plot['histos'][i_pdf], norm[plot['histos'][i_pdf]], isinstance(total_pdf,TH2)))
                                    #pdf_components_order.append(plot['histos'][i_pdf])

                                another_template = f.Get(plot['histos'][i_pdf])
                                another_template.Scale(float(norm[plot['histos'][i_pdf]])) 
                                pdf_components[plot['histos'][i_pdf]] = copy.deepcopy(another_template)
                                #pdf_components_order.append(plot['histos'][i_pdf])
                                
                                self.log.debug('TOTAL_PDF -> Adding up term {0} and the scale {1} to histo TH2:{2}'.format(plot['histos'][i_pdf], norm[plot['histos'][i_pdf]],isinstance(total_pdf,TH2)))
                                total_pdf.Add(another_template)
                                
                            self.total_pdf = total_pdf    
                            self.log.debug('TOTAL_PDF -> Added all terms and now the histo is TH2:{0}'.format(isinstance(self.total_pdf,TH2)))
                            bins_x, bins_y = self.total_pdf.GetXaxis().GetNbins(), self.total_pdf.GetYaxis().GetNbins()
                            self.log.debug('Size of template (x,y) = ({0},{1})'.format(bins_x, bins_y))
                            
                            pdf_components["TotalPDF"] = copy.deepcopy(self.total_pdf)

                            
                            for x_bin in range(1,bins_x+1):
                                for y_bin in range(1,bins_y+1):
                                    bin_content = self.total_pdf.GetBinContent(x_bin, y_bin)
                                    #self.log.debug("Bin (i,j) = {0},{1} value = {2}".format(x_bin,y_bin, bin_content))
                                    if bin_content <= 0:
                                        raise ValueError, "!!! PDF CANNOT BE 0 or NEGATIVE !!!"
                                    assert bin_content > 0, "!!! PDF CANNOT BE 0 or NEGATIVE !!! Check your tamplates. Bin (i,j) = {0},{1} value = {2}".format(x_bin,y_bin, bin_content)   
                            
                            ### settup for the plot: text, legend and save    
                            self.c1.cd()
                            if self.setup_exist: 
                                self.arrangeAxis(self.total_pdf,data['setup'])
                            if self.leg_exist:
                                self.leg.AddEntry(self.total_pdf,plot['legend']['text'],plot['legend']['opt']);
                            try:
                                plot['draw_opt']
                            except KeyError:
                                draw_opt = "COLZ"
                            else:
                                draw_opt = str(plot['draw_opt'])
                            if draw_opt.lower()=="surf":
                                self.c1.SetTheta(16.56)
                                self.c1.SetPhi(57.83133)
                            self.total_pdf.SetMaximum(0.033)
                            self.total_pdf.SetMinimum(-0.005)
                            self.total_pdf.Draw(draw_opt)
                            
                            if self.leg_exist: self.leg.Draw()
                            
                                            
                            if self.add_text_exist: 
                                    data['setup']['add_text'][0]['text'] = "Total PDF(X,Y), {0}, GEN level, 8TeV".format(fs)
                                    data['setup']['add_text'][0]['text']+=", k2/k1 ={0: >6.2f}, k3/k1 ={1: >6.2f}".format(self.k2k1_ratio,self.k3k1_ratio)
                                    self.add_text(data['setup']['add_text'])
                            
                            ### saving the plot and copyng to webdir(if configured)
                            plot_name = "TotalPdf_{0}_{1}_k2k1_{2}_k3k1_{3}".format(theTemplFile.replace("/","_"), draw_opt,self.k2k1_ratio,self.k3k1_ratio)
                            #plot_name = self.name
                            self.save_extensions = ['png','pdf','eps', 'gif']
                            try:
                                data['setup']['save_ext']
                            except KeyError:
                                self.log.info("No extensions are provided in setup. Using default: ", self.save_extensions)
                            else:
                                self.save_extensions = list(data['setup']['save_ext'])
                            self.save(self.c1, plot_name, self.save_extensions)
                            list_for_animated_gif.append(plot_name+".gif")
                            
                            #### Plot in the same canvas
                            i_div = 1
                            print "COMPONENTS :", pdf_components.keys()
                            
                            for comp in pdf_components.keys():
                                    self.c_big.cd(i_div)
                                    ROOT.gPad.SetRightMargin(0.2)
                                    ROOT.gPad.SetLeftMargin(0.1)
                                    
                                    i_div+=1
                                    ### settup for the plot: text, legend and save    
                                    if self.setup_exist: 
                                        self.arrangeAxis(pdf_components[comp],data['setup'])
                                    if self.leg_exist:
                                        self.leg.AddEntry(pdf_components[comp],plot['legend']['text'],plot['legend']['opt']);
                                    try:
                                        plot['draw_opt']
                                    except KeyError:
                                        draw_opt = "COLZ"
                                    else:
                                        draw_opt = str(plot['draw_opt'])
                                    if draw_opt.lower()=="surf":
                                        self.c_big.SetTheta(16.56)
                                        self.c_big.SetPhi(57.83133)
                                    pdf_components[comp].SetMaximum(0.033)
                                    pdf_components[comp].SetMinimum(-0.033)
                                    
                                    pdf_components[comp].GetYaxis().SetTitleOffset(0.7)
                                    if (i_div-1)>1:
                                        pdf_components[comp].GetYaxis().SetLabelOffset(999)
                                        pdf_components[comp].GetYaxis().SetLabelSize(0)
                                        pdf_components[comp].GetYaxis().SetTitle("")
                                    
                                    pdf_components[comp].Draw(draw_opt)
                                    
                                    if self.leg_exist: self.leg.Draw()
                                    
                                    if self.add_text_exist: 
                                            data['setup']['add_text'][0]['text'] = "{1}(X,Y), {0}, GEN level, 8TeV".format(fs,comp )
                                            data['setup']['add_text'][0]['text']+=", k2/k1 ={0: >6.2f}, k3/k1 ={1: >6.2f}".format(self.k2k1_ratio,self.k3k1_ratio)
                                            self.add_text(data['setup']['add_text'])
                                    
                            ### saving the plot and copyng to webdir(if configured)
                            plot_name = "TotalPdfComponents_{0}_{1}_k2k1_{2}_k3k1_{3}".format(theTemplFile.replace("/","_"), draw_opt,self.k2k1_ratio,self.k3k1_ratio)
                            #plot_name = self.name
                            self.save_extensions = ['png','pdf','eps', 'gif']
                            try:
                                data['setup']['save_ext']
                            except KeyError:
                                self.log.info("No extensions are provided in setup. Using default: ", self.save_extensions)
                            else:
                                self.save_extensions = list(data['setup']['save_ext'])
                            self.save(self.c_big, plot_name, self.save_extensions)
                            list_for_animated_gif_components.append(plot_name+".gif")
                            
                            
                            
                name_animated_gif = "TotalPdf_{0}_{1}_ANIMATED_k2k1_{2}_k3k1_{3}.gif".format(theTemplFile.replace("/","_"),draw_opt,"_".join("{0}".format(one_k2k1) for one_k2k1 in k2k1_list), "_".join("{0}".format(one_k3k1) for one_k3k1 in k3k1_list))                            
                self.make_animated_gif(list_for_animated_gif, name_animated_gif)      
                name_animated_gif = "TotalPdfComponents_{0}_{1}_ANIMATED_k2k1_{2}_k3k1_{3}.gif".format(theTemplFile.replace("/","_"),draw_opt,"_".join("{0}".format(one_k2k1) for one_k2k1 in k2k1_list), "_".join("{0}".format(one_k3k1) for one_k3k1 in k3k1_list))                            
                self.make_animated_gif(list_for_animated_gif_components, name_animated_gif)