Package madgraph :: Package madweight :: Module MW_fct
[hide private]
[frames] | no frames]

Source Code for Module madgraph.madweight.MW_fct

  1  ########################################################################## 
  2  ##                                                                      ## 
  3  ##                  TOOL FOR MW (fonction and class)                    ## 
  4  ##                                                                      ## 
  5  ########################################################################## 
  6  ## 
  7  ## Content: 
  8  ## 
  9  ##     - Class Multi_list 
 10  ##     - fct   permutate 
 11  ##     - fct   put_in_fortran_format 
 12  ##     - fct   put_line_in_fortran_format 
 13  ##     - Class BackRead 
 14  ## 
 15  ########################################################################## 
 16  from __future__ import absolute_import 
 17  import sys 
 18  import os 
 19  import string 
 20  import collections 
 21  import itertools 
 22  import copy 
 23  from six.moves import range 
 24  import six 
 25  if six.PY3: 
 26      import io 
 27      file = io.IOBase 
 28   
29 -class Multi_list(list):
30 """ a list of list """ 31
32 - def give_combinaison(self):
33 """ return all the combinatory possibility in a list """ 34 35 if not self: #test void input 36 return [[]] 37 38 len_vector=[] 39 status_vector=[] 40 for i in range(0,len(self)): 41 status_vector.append(0) 42 if type(self[i])==list: 43 len_vector.append(len(self[i])) 44 else: 45 len_vector.append(1) 46 47 solution=[] 48 while 1: 49 #print 'add solution' 50 solution.append(self.extract_vector(status_vector)) 51 curent_pos=0 52 while 1: 53 #print 'enter in while' 54 if status_vector[curent_pos]==len_vector[curent_pos]-1: 55 #print 'pass to step',curent_pos+1 56 curent_pos+=1 57 if(curent_pos==len(status_vector)): 58 #print 'break' 59 break 60 else: 61 #print 'update' 62 status_vector[curent_pos]+=1 63 for i in range(0,curent_pos): 64 status_vector[i]=0 65 break 66 if (curent_pos==len(status_vector)): 67 break 68 69 return solution
70
71 - def extract_vector(self,pos_list):
72 """ return the list of element corresponding at position in pos_list """ 73 74 if len(pos_list)!=len(self): 75 return 76 77 solution=[] 78 #print 'a',pos_list 79 for i in range(0,len(pos_list)): 80 solution.append(self[i][pos_list[i]]) 81 82 return solution
83 84
85 - def give_list_possiblity(self,list,opt=''):
86 """ return all permutation of list where each element belongs to corresponding list of the multilist 87 88 example: multilist=[ [a,b,c],[a,b,d],[c,d] ] 89 list=[a,b,c] 90 ->return=[ [a,b,c],[b,a,c] ] 91 92 multilist=[ [a,b,c],[a,b,d],[a,c,d] ] 93 list=[a,b,d] 94 ->return=[ [a,b,d],[b,a,d],[b,d,a] ] 95 96 option: if the list is made of object, but multilist are data of this object, 97 you can use opt to check if list[i].opt in self[i] 98 """ 99 100 if len(list)!=len(self): 101 return 102 103 perm_pos=permutate(list) 104 sol=[] 105 for perm in perm_pos: 106 find=1 107 for i in range(0,len(list)): 108 if opt=='': 109 if perm[i] not in self[i]: 110 find=0 111 break 112 else: 113 value=eval('perm[i].'+opt) 114 if value not in self[i]: 115 find=0 116 break 117 if find: 118 sol.append(perm) 119 return sol
120 121 122 123 124 125 126 127 128 129 130 131 # 132 # other function 133 # 134 135 136
137 -def permutate(seq):
138 """permutate a sequence and return a list of the permutations""" 139 140 if not seq: 141 return [seq] # is an empty sequence 142 else: 143 temp = [] 144 for k in range(len(seq)): 145 part = seq[:k] + seq[k+1:] 146 #print k, part # test 147 for m in permutate(part): 148 temp.append(seq[k:k+1] + m) 149 #print m, seq[k:k+1], temp # test 150 return temp
151
152 -def put_in_fortran_format(text):
153 out='' 154 if text.count('\n')>1: 155 part=text.split('\n') 156 for line in part: 157 # print "line",[line] 158 if len(line)>0: 159 out+=put_line_in_fortran_format(line)+"\n" 160 else: 161 out+='\n' 162 else: 163 out=put_line_in_fortran_format(text) 164 165 return out
166
167 -def put_line_in_fortran_format(text):
168 "take formula and split in 50-90 columns" 169 170 171 #take car of special begin with fortran 172 if text[0]=="c" or text[0]=="C": 173 return text 174 if(text[:6].count('&')): 175 return text 176 if(text[:6]!=" "): 177 try: 178 a=int(text[:6]) #test for tag 179 except: 180 text=" "+text 181 182 #delete final space 183 while (text[-1]==" "): 184 text=text[:-1] 185 if len(text)==0: 186 break 187 # print "pass here: len " ,len(text) 188 comment_mode = 0 189 out="" 190 while(len(text)>90): 191 tag=len(text) 192 list_split=["\n",",","+","-",'('," "] 193 i=0 194 while 1: 195 if text[50:90].count(list_split[i]): 196 out+=text[:50] 197 text=text[50:] 198 tag=text.index(list_split[i])+1 199 break 200 i+=1 201 if '!' in text[:tag]+out[-50:]: 202 comment_mode = 1 203 out+=text[:tag]+"\n" 204 if(tag<len(text)): 205 if comment_mode==0: 206 text=" &"+text[tag:] 207 else: 208 text=" &!"+text[tag:] 209 210 out+=text 211 212 return out
213 214 215 ## class list(list): 216 ## "new list" 217 218 ## def __str__(self,opt=''): 219 ## if opt=='': 220 ## return list.__str__(self) 221 ## else: 222 ## text='[' 223 ## for content in list: 224 ## text+=str(getattr(A,opt))+',' 225 ## text=text[:-1]+']' 226
227 -class BackRead(file):
228 """read a file returning the lines in reverse order for each call of readline() 229 This actually just reads blocks (4096 bytes by default) of data from the end of 230 the file and returns last line in an internal buffer. I believe all the corner 231 cases are handled, but never can be sure...""" 232 233
234 - def readline(self):
235 while len(self.data) == 1 and ((self.blkcount * self.blksize) < self.size): 236 self.blkcount = self.blkcount + 1 237 line = self.data[0] 238 try: 239 self.seek(-self.blksize * self.blkcount, 2) # read from end of file 240 self.data = string.split(self.read(self.blksize) + line, '\n') 241 except IOError: # can't seek before the beginning of the file 242 self.seek(0) 243 self.data = string.split(self.read(self.size - (self.blksize * (self.blkcount-1))) + line, '\n') 244 245 if len(self.data) == 0: 246 return "" 247 248 # self.data.pop() 249 # make it compatible with python <= 1.5.1 250 line = self.data[-1] 251 self.data = self.data[:-1] 252 return line + '\n'
253
254 - def __init__(self, filepos, blksize=4096):
255 """initialize the internal structures""" 256 257 # get the file size 258 self.size = os.stat(filepos)[6] 259 # how big of a block to read from the file... 260 self.blksize = blksize 261 # how many blocks we've read 262 self.blkcount = 1 263 file.__init__(self, filepos, 'rb') 264 # if the file is smaller than the blocksize, read a block, 265 # otherwise, read the whole thing... 266 if self.size > self.blksize: 267 self.seek(-self.blksize * self.blkcount, 2) # read from end of file 268 self.data = string.split(self.read(self.blksize), '\n') 269 # strip the last item if it's empty... a byproduct of the last line having 270 # a newline at the end of it 271 if not self.data[-1]: 272 # self.data.pop() 273 self.data = self.data[:-1]
274 275 # def close(self): 276 # """ close correctly file """ 277 # try: 278 # self.close() 279 # except: 280 # pass 281
282 -def get_perms_from_id(pid_list, bjet_is_jet):
283 """ """ 284 285 assert isinstance(pid_list, list) 286 287 assert isinstance(bjet_is_jet, bool) or bjet_is_jet in [0,1] 288 289 list_id = [] 290 for i,pid in enumerate(pid_list): 291 if abs(pid) in [1,2,3,4]: 292 list_id.append('j') 293 elif abs(pid) == 5: 294 if bjet_is_jet: 295 list_id.append('j') 296 else: 297 list_id.append('b') 298 elif abs(pid) in [12,14,16,18,1000022,1000023,1000025,1000035]: 299 list_id.append('%s_%s' % (i, pid)) 300 else: 301 list_id.append(pid) 302 303 #get the id permutations 304 return get_all_permutations(list_id)
305
306 -def get_all_permutations(cat_list):
307 """ """ 308 309 # create the category names and the id to permute for each category 310 nb_cat = collections.defaultdict(list) 311 for i,cat in enumerate(cat_list): 312 nb_cat[cat].append(i+1) #+1 in order to be in Fortan convention 313 cat_names = list(nb_cat.keys()) 314 # build an iterator for each category 315 iterator = dict([(cat, itertools.permutations(value)) 316 for cat,value in nb_cat.items()]) 317 318 permutations = [] # all possibility 319 current = 0 # position of the last modify category 320 #initialize all iterator to have starting point value 321 current_value = dict([(cat, list(next(it))) for cat,it in iterator.items()]) 322 323 while current < len(iterator): 324 #store the current value 325 perm = [] 326 curr = copy.deepcopy(current_value) 327 for cat in cat_list: 328 perm.append(curr[cat].pop(0)) 329 permutations.append(perm) 330 #update the iterator to have the next value 331 while current < len(iterator): 332 cat = cat_names[current] 333 it = iterator[cat] 334 try: 335 new_val = next(it) 336 except StopIteration: 337 iterator[cat] = itertools.permutations(nb_cat[cat]) 338 current_value[cat] = list(next(iterator[cat])) 339 current +=1 340 else: 341 current_value[cat] = list(new_val) 342 current = 0 343 break 344 345 return permutations
346