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
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>© 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 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)
chain_process.set_do_copy_to_webdir(opt.do_copy_to_webdir) for poi in list(run_data['POI']): log.debug('Setting POI value for {0}'.format(poi)) chain_process.set_poi_value(str(poi), str(run_data[poi])) lumi_list = run_data['lumi_list'] this_dir=os.getcwd() cards_dir = "cards_{0}/HCG/126".format(opt.run_data_name) #out_file_name_base = "{0}.{1}.asimov.k3k1.{2}".format(opt.run_data_name, run_data['final_state'],run_data['k3k1_ratio']) out_file_name_base = "{0}.{1}.asimov.{2}".format(opt.run_data_name, run_data['final_state'],chain_process.get_poi_name_value_filename()) if 'info' not in run_data.keys(): run_data['info']='Not available...' log.info(run_data['info']) info_name = out_file_name_base+".info" tab_name = out_file_name_base+".tab" log.debug("tab_name={0}".format(tab_name)) import yaml with open('data_config.yaml', 'w') as fdata: fdata.write(yaml.dump(run_data, default_flow_style=False)) log.info('Writing used configuration to data_config.yaml') if opt.datacard: chain_process.process("createCards") try: os.remove(info_name) except OSError , exception:
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
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)