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