Exemple #1
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 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 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
Exemple #4
0
 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 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/")
     os.chdir(this_dir)
     log.debug("Current dir = {0}".format(os.getcwd()))        
     f.write(table_raw)
     #print "%({lumi} %({bf} %({ul68} %({ul95} %({wf}" >> %(tab_name
     
 f.close()
 #copy info, table and DC to webdir
 shutil.copy("{0}/{1}".format(this_dir,tab_name),"{0}/{1}".format(cards_dir, tab_name))
 shutil.copy("{0}/{1}".format(this_dir,info_name),"{0}/{1}".format(cards_dir, info_name))
 shutil.copy("{0}/data_config.yaml".format(this_dir,info_name),"{0}/data_config.yaml".format(cards_dir))
 
 
 if opt.do_copy_to_webdir:
     import lib.util.MiscTools as misc
     webdir = chain_process.get_webdir()
     misc.make_sure_path_exists(webdir)
     shutil.copy("/afs/cern.ch/user/r/roko/www/html/index.php",webdir)
     
     file_list_for_moving = {
         "{0}/{1}".format(this_dir,tab_name) : "{0}/{1}".format(webdir, "results.tab"),
         "{0}/{1}".format(this_dir,info_name) : "{0}/{1}".format(webdir, "basic.info"),
         "{0}/data_config.yaml".format(this_dir) : "{0}/data_config.yaml".format(webdir)
         }
         
     for file in file_list_for_moving.values():
         if os.path.exists(file) and os.path.isfile(file): 
             os.remove(file)
     for file in file_list_for_moving.keys():
         shutil.move(file,file_list_for_moving[file])
     
     if opt.datacard:
def make_refurbished_hist(template_file, dir_2D):
    
    print "------------- Unfolding  templates"
    #dir_2D = '/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/Sandbox/templates_compare2Pedja/Templates2D_D0M_Dint13/'
    dir_2D_refurbished = dir_2D+"/refurbished/"
    misc.make_sure_path_exists(dir_2D_refurbished)
    #f=ROOT.TFile('/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/Sandbox/templates2D_D0M_Dint13/Dsignal_2e2mu.root')
    #f=ROOT.TFile('/afs/cern.ch/work/r/roko/Stat/CMSSW_611_JCP/src/HZZ4L_Combination/CombinationPy/CreateDatacards/Sandbox/Templates2D_D0Ph_Dint12/DSignal_4l.root')
    f=ROOT.TFile(dir_2D +"/"+template_file,"READ")
    print f.GetName()
    #f.cd()
    #ggH_proj = f.Get('ggH_shape').ProjectionX("ggH_shape_D0M",0,-1,"e")
    allowedTermNames = ['ggH',    'gg0Ph','gg0M' ,'ggInt_12P','ggInt_12N','ggInt_13P','ggInt_13N','ggInt_23P','ggInt_23N',
                        'qqZZ','ggZZ','zjets']
    new_names = {
        'gg0M' : 'gg0Ph',
        'ggInt_13P':'ggInt_12P',
        'ggInt_13N':'ggInt_12N',
        }
                        
                        
    #allowedTermNames = ['ggH',   'gg0M', 'ggInt_13P','ggInt_13N']
    fnew=ROOT.TFile(dir_2D_refurbished +"/"+template_file,"RECREATE")
    print fnew.GetName()
    refurbished = None
    #th2 = None
    found_th2 = False
    th2 = None
    for term in allowedTermNames:
        print "Searching for :", term
        try :
              th2_name = f.Get('{0}_shape'.format(term)).GetTitle()
        except ReferenceError:
              print "Doesn't exist : ", term
        else:
              print "----", th2_name 
              found_th2=True
              print "Found term for axis definiton: ", term
              th2 = f.Get('{0}_shape'.format(term))
              break
        #if found_th2 : break
              
          
    print "Axis x = {0} y = {1}".format(th2.GetXaxis().GetTitle(), th2.GetYaxis().GetTitle())
    for term in allowedTermNames:
        try:
            #th2 = f.Get('{0}_shape'.format(term))
            refurbished = f.Get('{0}_shape'.format(term)).GetName()
        except:
            print "@@@@ Shape {0}_shape doesn't exist.".format(term)
        else:
            print 'Working with {0}_shape'.format(term)
            refurbished = refurbishedHist(f.Get('{0}_shape'.format(term)),'{0}_shape_refurbished'.format(term),norm=-1)
            #refurbished = f.Get('{0}_shape'.format(term))
            #refurbished.SetTitle('{0}_shape_refurbished'.format(term))
            fnew.cd()
            
            if 'Dbackground_ggZZ' in template_file: 
                print "---------------> Changing name to : ggZZ_shape"
                refurbished.SetNameTitle("ggZZ_shape","ggZZ_shape_refurbished")
            elif 'Dbackground_ZJets' in template_file: 
                print "---------------> Changing name to : zjets_shape"
                refurbished.SetNameTitle("zjets_shape","zjets_shape_refurbished")
            else: 
                term_name = term
                if copy_13_templates_to_12 and term in new_names.keys():
                        term_name = new_names[term]
                refurbished.SetNameTitle('{0}_shape'.format(term_name),"{0}_shape_refurbished".format(term))
                
            print "New template name/title: {0}/{1}".format(refurbished.GetName(),refurbished.GetTitle())
            c1 =ROOT.TCanvas("Templates","Templates",800,800)
            c1.cd()
            ROOT.gPad.SetRightMargin(0.085)
            refurbished.Draw("colz")
            misc.make_sure_path_exists(dir_2D_refurbished+"/fig")
            for ext in [".png", ".pdf", ".root",".C"]: c1.SaveAs(dir_2D_refurbished +"/fig/"+template_file+"_"+term+ext)            
            refurbished.Write()
            
            if not 'Dbackground' in template_file: 
                copy_tree(f, fnew,"factors")
    
    
    f.Close()
    fnew.Close()