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

Source Code for Module madgraph.madweight.change_tf

  1  #!/usr/bin/env python 
  2   
  3  #Extension 
  4   
  5  import os 
  6  import re 
  7  import string 
  8  import sys 
  9  import xml.sax.handler 
 10   
 11  try: 
 12      import madgraph.madweight.Cards as Cards 
 13      import madgraph.madweight.mod_file as mod_file 
 14      import madgraph.madweight.particle_class as particle_class 
 15      import madgraph.madweight.MW_fct as MW_fct 
 16  except ImportError: 
 17      import internal.madweight.Cards as Cards 
 18      import internal.madweight.mod_file as mod_file 
 19      import internal.madweight.particle_class as particle_class 
 20      import internal.madweight.MW_fct as MW_fct 
 21       
 22   
 23  ###f77 forbiden term 
 24  #forbiden=["goto","return","stop","call","write","read","do","while","end ","continue","asign","pause","print","rewind","backspace","endfile","open","close","inquire","entry","optional","save","equivalence","intent","target","rule","compute","system","enddo"] 
 25   
 26   
27 -def create_TF_main(name,make, MW_dir):
28 print "start main program" 29 TF_file=Full_TF(name) 30 TF_file.read_file("./data/TF_"+name+".dat") 31 32 print "deleting the current TFlib: " 33 os.system("rm ../../../lib/libTF.a >& /dev/null") 34 35 TF_file.create_ordering_file() 36 print "ordering_file.inc created" 37 list_var=TF_file.create_transfer_functions() 38 print "transfer_function.f created" 39 TF_file.create_transfer_card(list_var) 40 print "transfer_card.dat created" 41 create_param_inc(list_var) 42 print "TF_param created" 43 create_ident_card(list_var) 44 print "ident_card created" 45 create_version(name) 46 print 'TransferFunctionVersion created' 47 fsock = open('nb_tf.inc','w').write(' integer nb_tf\n parameter (nb_tf=1)\n') 48 os.chdir('../../../') #go to main 49 50 51 # P_dir,MW_dir=MW_param.detect_SubProcess(P_mode=1) 52 53 for directory in MW_dir: 54 obj=TF_in_SubProcesses(TF_file,directory) 55 obj.write_transfer_function_file() 56 print 'call_TF.f created in for all Subprocesses' 57 os.chdir('./Source/MadWeight/transfer_function') 58 update_dir(name,make,MW_dir) 59 print 'generation completed'
60 61 62 #1 #################################################################################
63 -class XML_input(xml.sax.handler.ContentHandler):
64 """ This class will organize in python obect the TF_param.dat file 65 (written in xml) 66 """ 67 68 #2 #############################################################################
69 - def __init__(self):
70 self.block = {} 71 self.inblock='' 72 self.in_variable='' 73 self.buffer=''
74 75 #2 #############################################################################
76 - def startElement(self, name, attributes):
77 self.buffer='' 78 if name == "block": 79 block_name=attributes["name"] 80 self.block[block_name]=TF_block(block_name) 81 self.inblock=block_name 82 elif name == "variable": 83 self.in_variable=attributes["name"]
84 85 #2 #############################################################################
86 - def characters(self, data):
87 self.buffer += data
88 89 #2 #############################################################################
90 - def endElement(self, name):
91 if name == 'particles': 92 self.block[self.inblock].def_particles(self.buffer) 93 elif name == 'width_type': 94 self.block[self.inblock].def_width_type(self.buffer) 95 elif name == "tf": 96 self.block[self.inblock][self.in_variable].change_tf(self.buffer) 97 elif name == "width": 98 self.block[self.inblock][self.in_variable].change_width(self.buffer) 99 elif name == "info": 100 self.block[self.inblock].def_info(self.buffer) 101 elif name == "include": 102 self.block[self.inblock][self.in_variable].def_include(self.buffer)
103 104 #2 #############################################################################
105 - def read_file(self,filepos):
106 """ parse the file and fulfill the object """ 107 parser = xml.sax.make_parser( ) 108 parser.setContentHandler(self) 109 parser.parse(filepos)
110 111 #2 #############################################################################
112 - def find_label_to_block(self):
113 """ return a dict {label:block_name} store it for efficiency reason""" 114 115 if hasattr(self,'label_to_block'): 116 return self.label_to_block 117 118 self.label_to_block={} 119 for name,block in self.block.items(): 120 for label in block.particles: 121 self.label_to_block[label]=name 122 123 return self.label_to_block
124 125 126 #1 #################################################################################
127 -class TF_with_particles(XML_input):
128 """ this class extend the XML with routine associating the particles.dat file """ 129
130 - def __init__(self,only_add=0):
131 """ standard input but add a tag if the particles.dat is loaded or not """ 132 133 if not only_add: 134 XML_input.__init__(self) 135 self.particles_file=0
136
137 - def load_particles_file(self,filepos='./Source/MODEL/particles.dat'):
138 """ load the particles file if not already loaded """ 139 140 if not self.particles_file: 141 self.particles_file = Cards.Particles_file(filepos)
142
143 - def find_pid_to_label(self):
144 145 if hasattr(self, 'pid_to_label'): 146 return self.pid_to_label 147 148 self.pid_to_label=self.particles_file.give_pid_to_label() 149 return self.pid_to_label
150 151 #1 #################################################################################
152 -class TF_input(XML_input):
153 """ This class extend the XML input containing routines containing writing output """ 154 155 #2 #############################################################################
156 - def __init__(self,name):
157 XML_input.__init__(self) 158 self.tf_name=name
159 160 #2 #############################################################################
161 - def create_ordering_file(self):
162 """ take input form TF_input.dat and insert in ordering_file.inc""" 163 164 #collect particle information 165 thin=[] 166 large=[] 167 with_x=0 168 theta = [] 169 phi = [] 170 for block in self.block.values(): 171 if block.order==1: 172 thin+=block.particles 173 elif block.order==2: 174 large+=block.particles 175 if "x1" in block.particles or "x2" in block.particles: 176 with_x=1 177 if block['THETA'].tf_code.strip() != 'tf=1d0' or \ 178 block['THETA'].width_code.strip() != 'width=0d0': 179 theta += block.particles 180 if block['PHI'].tf_code.strip() != 'tf=1d0' or \ 181 block['PHI'].width_code.strip() != 'width=0d0': 182 phi += block.particles 183 184 #define the rule of how modif file transfer_function/input/ordering_file.inc 185 modif_rule={} 186 modif_rule['THIN']=','.join(thin) 187 modif_rule['LARGE']=','.join(large) 188 modif_rule['X']=str(with_x) 189 modif_rule['NAME_TF']=self.tf_name 190 modif_rule['THETA'] = ','.join(theta) 191 modif_rule['PHI'] = ','.join(phi) 192 193 #modify file 194 mod_file.mod_file('./input/ordering_file.inc',modif_rule,write='./ordering_file.inc')
195 196 #2 #############################################################################
198 """ define for each block the 3 functions TF and the three TF width """ 199 200 def create_optional_variable(text,blockname,variable,list_var=[]): 201 """ replace all #1,#2 by fortran name """ 202 203 output='' 204 Pattern=re.compile(r'''#(\d*)''') 205 prov=Pattern.split(text) 206 i=0 207 while i<len(prov)-1: 208 output+=prov[i] 209 i+=1 210 if not('tf_'+blockname+"_"+variable+"_"+prov[i] in list_var): 211 list_var.append('tf_'+blockname+"_"+variable+"_"+prov[i]) 212 output+='tf_'+blockname+"_"+variable+"_"+prov[i]+"(curr_tf)" 213 i+=1 214 output+=prov[-1] 215 return output,list_var
216 217 template = mod_file.Mod_file(rule_file='./input/mod_generic') 218 list_var=[] #list all variable needed to defined in order to be set in the transfer card 219 220 text='$B$ TF_HEADER $E$' 221 text=mod_file.mod_text(text,template.dico) 222 for block in self.block.values(): 223 for variable in ['E','THETA','PHI']: 224 tf_var='tf_'+variable+'_'+block.name 225 new_text='$B$ GENERIC_TF $E$' 226 width_var='width_'+variable+'_'+block.name 227 new_text+='$B$ GENERIC_WIDTH $E$' 228 new_text=mod_file.mod_text(new_text,template.dico) #extend generic 229 new_rule={'tf_var':tf_var,'width_var':width_var} 230 new_rule['tf_definition']=block[variable].tf_code 231 new_rule['width_definition']=block[variable].width_code 232 new_rule['tf_include']=block[variable].includetext 233 new_text=mod_file.mod_text(new_text,new_rule) #change default variable in real one 234 new_text,list_var=create_optional_variable(new_text,block.name,variable,list_var) 235 new_text=MW_fct.put_in_fortran_format(new_text) 236 text+=new_text 237 238 text=MW_fct.put_in_fortran_format(text) 239 ff=open('./transfer_function.f','w') 240 ff.writelines(text) 241 return list_var
242 243 #2 ###################################################################################
244 - def create_transfer_card(self,list_var):
245 """ create the generic transfer_card linked to this transfer_functions """ 246 247 in_card=open("./input/transfer_card_generic.dat",'r') 248 text=in_card.read() 249 in_card.close() 250 text += '$b$ S-COMMENT_# $b$\n' 251 text+='To change the transfer function run ./bin/change_tf.py \n' 252 text+='Current parametrization :'+self.tf_name+'\n' 253 text += '$e$ S-COMMENT_# $e$' 254 255 current_block='' 256 current_var='' 257 for fortran_var in list_var: 258 tag,block,var,number=fortran_var.split('_',3) 259 if block != current_block: 260 current_block=block 261 current_var='' 262 text += '$b$ S-COMMENT_# $b$' 263 text += self.block[block].info() 264 text += '$e$ S-COMMENT_#$e$' 265 if var!=current_var: 266 current_var=var 267 text+='BLOCK TF_'+block+'_'+var+'\n' 268 text+='\t'+number+'\t 1d0 \t #\n' 269 270 text=mod_file.mod_text(text,{}) #only S-COMMENT mod 271 out=open("transfer_card.dat",'w') 272 out.writelines(text) 273 out.close()
274
275 -class Full_TF(TF_input,TF_with_particles):
276 """ class containing both extension of XML_input """ 277
278 - def __init__(self,name):
279 """ load the different module""" 280 TF_input.__init__(self,name) 281 TF_with_particles.__init__(self,only_add=1)
282
283 -class TF_block(dict):
284 """ class containing the information for a specific block from TF_param.dat """ 285 286 #2 #############################################################################
287 - def __init__(self,name):
288 self.name=name 289 #init all to delta 290 dict.__init__(self) 291 self.update({'E':TF_on_var(),'THETA':TF_on_var(),'PHI':TF_on_var()}) 292 self.order=0 293 self.infotext='' 294 self.name=name
295 296 #2 #############################################################################
297 - def def_particles(self,particles):
298 """ define self.particles content """ 299 """ still to define how to store this """ 300 particles=particles.replace(' ','') 301 particles=particles.replace('\t','') 302 particles=particles.replace('\n','') 303 304 self.particles=particles.split(',')
305 306 #2 #############################################################################
307 - def def_width_type(self,text):
308 """ define self.order content (0:delta/1:thin/2:large) """ 309 310 if "thin" in text.lower(): 311 self.order=1 312 elif "large" in text.lower(): 313 self.order=2
314 315 #2 #############################################################################
316 - def def_info(self,text):
317 """ store the information on the block (comming from the xml) """ 318 self.infotext=text
319 320 #2 #############################################################################
321 - def info(self):
322 text = 'Parameter for particles: '+','.join(self.particles)+'\n' 323 text += 'Information:'+self.infotext+'\n' 324 325 return text
326 327 328 329 #1 ##################################################################################
330 -class TF_on_var:
331 """ class containing the TF/WIDTH for a specific type """ 332 333 #2 #############################################################################
334 - def __init__(self):
335 """ initialize the content to delta """ 336 self.tf_code=' tf=1d0' 337 self.width_code=' width=0d0' 338 self.includetext= ''
339 340 #2 #############################################################################
341 - def def_include(self,text):
342 """ store the information on the block (comming from the xml) """ 343 self.includetext=text
344 345 346 #2 #############################################################################
347 - def change_tf(self,text):
348 self.tf_code=text
349 350 #2 #############################################################################
351 - def change_width(self,text):
352 self.width_code=text
353 354 #1 #################################################################################
355 -class TF_in_SubProcesses:
356 """ Class for building transfert functions routine in a specific SubProcess""" 357 358 #2 #############################################################################
359 - def __init__(self,TF_data,MW_sub):
360 """ TF_input: object of class TF_with_particles containing all the information of TF_param.dat 361 MW_sub: string of the name of the MW subprocess in which we are going to work 362 """ 363 364 self.TF_data=TF_data 365 TF_data.load_particles_file() #load particles.dat (if not already done) 366 self.sub=MW_sub 367 self.dir='./SubProcesses/'+MW_sub 368 pid_list=self.charge_particle_content() 369 self.blockname_list=self.define_tf_block_for_particles(pid_list)
370 371 #2 #############################################################################
372 - def charge_particle_content(self):
373 """ find the ingoing and outgoing particles for this Subprocess 374 store this information in self.particles=[list of PID] 375 return the dictionary 376 """ 377 self.particles = Cards.read_leshouches_file(self.dir+'/leshouche.inc') 378 return self.particles
379 380 #2 #############################################################################
381 - def define_tf_block_for_particles(self,pid_list):
382 """find for all particles in which tf_block she is in 383 return [list of tf_block]. tf_block is the string name of the tf block, 384 0 if it's delta related and -1 if it's an invisible particles 385 """ 386 out=[] 387 out.append(self.block_for_thispid('x1')) 388 out.append(self.block_for_thispid('x2')) 389 for pid in pid_list[2:]: 390 out.append(self.block_for_thispid(pid)) 391 return out
392 393 #2 #############################################################################
394 - def block_for_thispid(self,pid):
395 """ find in which TF, the particle pid is 396 if it is delta return 0 397 if it's a invisible particles return -1 398 """ 399 try: 400 pid_to_label=self.TF_data.find_pid_to_label() #return {pid:label} 401 except: 402 pid_to_label={1: 'd', 2: 'u', 3: 's', 4: 'c', 5: 'b', 6: 't', 11: 'e', 12: 've', 13: 'mu', 14: 'vm', 15: 'ta', 16: 'vt', 21: 'g', 22: 'A', 23: 'Z', 24: 'W', 25: 'h'} 403 404 label_to_block=self.TF_data.find_label_to_block() #return {label:block} 405 406 if pid in ['x1','x2']: #treat special case for initial particles 407 if label_to_block.has_key(pid): 408 return label_to_block[pid] 409 else: 410 return -1 411 pid=abs(int(pid)) 412 if pid in particle_class.invisible_list: 413 return -1 414 if label_to_block.has_key(pid_to_label[pid]): 415 return label_to_block[pid_to_label[pid]] 416 else: 417 return 0
418 419 #2 #############################################################################
421 """ write transfer_function.f file for the specific subprocesses """ 422 423 text='$B$ TF_HEADER $E$\n' 424 text+=self.text_get_central_point()+'\n' 425 text+=self.text_transfer_fct()+'\n' 426 text+=self.text_tf_E_for_part()+'\n' 427 428 template = mod_file.Mod_file(rule_file='./Source/MadWeight/transfer_function/input/mod_generic') 429 text=mod_file.mod_text(text,template.dico) 430 text=MW_fct.put_in_fortran_format(text) 431 432 ff=open(self.dir+'/call_TF.f','w') 433 ff.writelines(text) 434 ff.close()
435 436 437 #2 #############################################################################
438 - def text_get_central_point(self):
439 """ return the get_centralPoint function in a unformated text 440 (need to pass in mod_file for comment, and to f77_format 441 """ 442 443 #Comment-subroutine-definition 444 text='$B$ START_CENTRAL_POINT $E$\n' 445 446 external_done=[] 447 #add the definition for external function 448 for block in self.blockname_list: 449 if isinstance(block, basestring): 450 name_list='width_E_'+block+', width_THETA_'+block+', width_PHI_'+block 451 else: 452 continue 453 454 #avoiding to redefine the same thing twice 455 if block not in external_done: 456 text+=' external '+name_list+'\n' 457 text+=' double precision '+name_list+'\n' 458 external_done.append(block) 459 text+= ' do perm =1,NPERM\n call get_perm(perm, perm_id)\n' 460 #start the definition 461 text+='\n$b$ S-COMMENT_C $b$ Start the definition $e$ S-COMMENT_C $e$\n' 462 for i in range(0,len(self.blockname_list)): 463 blockname=self.blockname_list[i] 464 465 #define central point 466 if blockname != -1: #particle is visible 467 text+=' c_point(perm, %s,1,1)=theta(pexp_init(0,2+perm_id(%s)))\n' % (i+1,i-1) 468 text+=' c_point(perm, %s,2,1)=phi(pexp_init(0,2+perm_id(%s)))\n' % (i+1,i-1) 469 text+=' c_point(perm, %s,3,1)=rho(pexp_init(0,2+perm_id(%s)))\n' %(i+1,i-1) 470 471 #define width 472 variable=['THETA','PHI','E'] 473 for j in range(1,4): 474 if blockname == -1: #particle is invisible 475 text+=' c_point(perm,%s,%s,2)=-1d0\n' % (i+1,j) 476 elif blockname == 0: # in delta mode 477 text+=' c_point(perm,%s,%s,2)=0d0\n' % (i+1,j) 478 else: #visible particles with TF define 479 text+=' c_point(perm,%s,%s,2)=width_%s_%s(pexp_init(0,2+perm_id(%s)),tag_lhco(%s))\n' % (i+1,j,variable[j-1],blockname,i-1,i+1) 480 text+='\n' #space between particles definition 481 482 text+='\n enddo \n return\n end\n' 483 return text
484 485 #2 #############################################################################
486 - def text_transfer_fct(self):
487 """ return the transfer_fct function in a unformated text 488 (need to pass in mod_file for comment, and to f77_format 489 """ 490 491 #Comment-subroutine-definition 492 text='$B$ START_TRANSFER_FCT $E$\n' 493 494 met=0 495 if self.TF_data.label_to_block.has_key('met'): 496 met = self.TF_data.label_to_block['met'] 497 #add new definition 498 text+=' double precision p_met_exp(0:3),p_met_rec(0:3)\n' 499 text+=' integer tag_init(3:nexternal),type(nexternal),run_number,trigger\n' 500 text+=" double precision eta_init(nexternal),phi_init(nexternal),pt_init(nexternal),j_mass(nexternal),ntrk(nexternal),btag(nexternal),had_em(nexternal),dummy1(nexternal),dummy2(nexternal)\n" 501 text+=" common/LHCO_input/eta_init,phi_init,pt_init,j_mass,ntrk,btag,had_em,dummy1,dummy2,tag_init,type,run_number,trigger\n" 502 text+=" integer met_lhco,opt_lhco\n common/LHCO_met_tag/met_lhco,opt_lhco\n" 503 #init met 504 text+=" do i=0,3\n p_met_rec(i)=0d0\n enddo\n" 505 506 text+=' weight=1d0\n' 507 for i in range(0,len(self.blockname_list)): 508 blockname=self.blockname_list[i] 509 510 if not isinstance(blockname, basestring): 511 if met and blockname == -1 and i>2: #invisible particlule but not the initial part 512 text+=' do i=0,3\n p_met_rec(i)=p_met_rec(i)+p(i,%s)\n enddo\n' %(i+1) 513 continue 514 515 text+=' n_lhco=tag_lhco(%s)\n' % (i+1) 516 for var in ['E','THETA','PHI']: 517 text+=' call tf_%s_%s(pexp(0,%s),p(0,%s),n_lhco,weight)\n' %(var,blockname,i+1,i+1) 518 text+='\n'#space between particles definition 519 520 if met: 521 text+=' k=met_lhco\n' 522 text+=' call four_momentum_set2(eta_init(k),phi_init(k),pt_init(k),j_mass(k),p_met_exp)\n' 523 text+=' call tf_E_%s(p_met_exp,p_met_rec,met_lhco,weight)\n' %(met) 524 525 text+="\n call check_nan(weight)\n return \n end\n" 526 return text
527 528 #2 #############################################################################
529 - def text_tf_E_for_part(self):
530 """ return the different tf_E_for_XX function in a unformated text 531 (need to pass in mod_file for comment, and to f77_format 532 """ 533 534 text2='' #text containing all the single call 535 #Comment-subroutine-definition-init weight 536 text='$B$ START_TF_E_FOR_PART $E$\n' 537 538 for i in range(0,len(self.blockname_list)): 539 text2+='\n'+self.text_tf_E_for_one_part(i)+'\n' 540 blockname=self.blockname_list[i] 541 542 if not isinstance(blockname, basestring): 543 text+=' if(MG_num.eq.%s) then\n tf_E_for_part=1d0\n return\n endif\n' % (i+1) 544 else: 545 text+=' if(MG_num.eq.%s) then\n' %(i+1) 546 text+=' tf_E_for_part=1d0\n' 547 text+=' n_lhco=tag_lhco(%s)\n'% (i+1) 548 text+=' call tf_E_%s(pexp(0,%s),momenta(0,%s),n_lhco,tf_E_for_part)\n' % (blockname,i+1,i+1) 549 text+='\n return\n endif\n' 550 551 text+="\n return \n end\n" 552 return text+text2
553 554 #2 #############################################################################
555 - def text_tf_E_for_one_part(self,i):
556 """ return the different tf_E_for_XX function in a unformated text 557 (need to pass in mod_file for comment, and to f77_format 558 """ 559 560 text='$B$ S-COMMENT_C $B$ Subroutine: tf_E_for_XX()\n' 561 text+='\n purpose: returns the value of the transfer function (in energy)\n' 562 text+='$E$ S-COMMENT_C $E$\n' 563 text+=' double precision function tf_E_for_%s()\n\n' % (i+1) 564 565 #Comment-subroutine-definition-init weight 566 text+='$B$ DEF_TF_E_FOR_ONE_PART $E$\n' 567 568 blockname=self.blockname_list[i] 569 if not isinstance(blockname, basestring): 570 text+=' tf_E_for_%s=1d0\n' %(i+1) 571 else: 572 text+=' tf_E_for_%s=1d0\n' %(i+1) 573 text+=' n_lhco=tag_lhco(%s)\n'% (i+1) 574 text+=' call tf_E_%s(pexp(0,%s),momenta(0,%s),n_lhco,tf_E_for_%s)\n' % (blockname,i+1,i+1,i+1) 575 576 text+="\n return \n end\n" 577 return text
578 579 580 #1 #################################################################################
581 -def create_param_inc(list_var):
582 583 out=open("TF_param.inc",'w') 584 file_in=open("./input/TF_param_generic.inc",'r') 585 out.writelines(file_in.read()) 586 file_in.close() 587 588 589 if list_var==[]: 590 print "TF_param created (no input)" 591 return 592 593 common_text='' 594 for name in list_var: 595 name = name.replace('(curr_tf)','') 596 line=" double precision "+name+"(nb_tf)\n" 597 out.writelines(line) 598 common_text+=name+',' 599 common_text=common_text[:-1] #suppress last coma 600 601 line=" Common/to_TF_param/"+common_text 602 line=MW_fct.put_in_fortran_format(line) 603 out.writelines(line) 604 out.close() 605 return
606
607 -def create_ident_card(list_var):
608 609 ff=open("./input/ident_mw_card_generic.dat",'r') 610 out=open("ident_mw_card.dat",'w') 611 #copy generic file 612 out.writelines(ff.read()) 613 614 for name in list_var: 615 data=name.split('_') 616 line="TF_"+data[1].upper()+'_'+data[2].upper() 617 line+=' '+data[3]+" "+name+" real \n" 618 out.writelines(line) 619 620 return
621 622
623 -def create_rw(list_var):
624 625 file_in=open("./input/rw_tf_generic.f","r") 626 file_out=open("./rw_tf.f","w") 627 628 Pattern=re.compile(r'''\$\$ADD_HERE\$\$''') 629 while 1: 630 line=file_in.readline() 631 if line=="": 632 break 633 if not(Pattern.search(line)): 634 file_out.writelines(line) 635 else: 636 #We are in position to put data info 637 for name in list_var: 638 file_out.writelines(" call get_real_t(npara,param,value,\""+name+"\" ,"+name+", 1d0)\n") 639 file_out.writelines(file_in.read()) 640 break 641 file_in.close() 642 file_out.close() 643 644 return
645
646 -def create_version(name):
647 """ standard version number DIRNAME_X.Y written in Transfer_FctVersion.txt 648 DIRNAME: name of the directory 649 X and Y: version number coming from the new_transfer function 650 """ 651 652 #load version number: 653 ff=open('./Transfer_FctVersion.txt','r') 654 line=ff.readline().split(':',1) 655 ff.close() 656 657 #dirname: 658 # dirname=os.path.basename(os.getcwd()) 659 660 #writefile: 661 ff=open('./Transfer_FctVersion.txt','w') 662 ff.writelines(name+':'+line[1]) 663 ff.close() 664 665 return
666 667
668 -def update_dir(name,make,MW_dir):
669 670 main='../../../' 671 672 os.system("cp ./ident_mw_card.dat "+main+"/Cards/") 673 os.system("cp transfer_card.dat "+main+"/Cards/") 674 os.system("cp data/transfer_card_"+name+".dat "+main+"/Cards/transfer_card.dat &>/dev/null") 675 os.system("cp data/transfer_card_"+name+".dat "+main+"/Cards/transfer_card_default.dat &>/dev/null") 676 if make: 677 os.chdir(main+"/Source/") 678 os.system("make") 679 for directory in MW_dir: 680 os.chdir(main+"/SubProcesses/"+directory) 681 os.system("ln -s ../../Source/MadWeight/transfer_function/TF_param.inc TF_param.inc") 682 os.system("ln -s ../../Source/MadWeight/transfer_function/nb_tf.inc nb_tf.inc") 683 os.system("make") 684 os.chdir('../../') 685 else: 686 os.system("make ") # always compile libTF ... 687 os.chdir(main) 688 for directory in MW_dir: 689 os.chdir("SubProcesses/"+directory) 690 os.system("ln -s ../../Source/MadWeight/transfer_function/TF_param.inc TF_param.inc") 691 os.system("ln -s ../../Source/MadWeight/transfer_function/nb_tf.inc nb_tf.inc") 692 os.chdir('../../') 693 694 #charge card 695 ident=Cards.Card('./Cards/ident_mw_card.dat') 696 madweight=Cards.Card('./Cards/MadWeight_card.dat') 697 transfer=Cards.Card('./Cards/transfer_card.dat') 698 699 #create output 700 madweight.create_include_file(ident,'./Source/madweight_card.inc') 701 transfer.create_include_file_tf(ident,'./Source/MadWeight/transfer_function') 702 703 os.chdir('./Source/MadWeight/transfer_function')
704 705 706 #def check_valid(text): 707 # for name in forbiden: 708 # Pattern=re.compile(name,re.I) 709 # if Pattern.search(text): 710 # print "ERROR: invalid usage of statement: ",name 711 # print "for security reason transfer functions can't use this statement" 712 # sys.exit() 713 # return 714
715 -def extract_tf_name(filepos='./Cards/proc_card.dat'):
716 """ read the file to find the requested change of variable""" 717 718 found=0 719 for line in file(filepos): 720 if found: 721 name=line.split()[0] #remove blank spae,end of line,... 722 return name 723 if line.startswith('# Begin transfer_function'): 724 found=1
725 726 727 ########################### TEST ################################# 728 if(__name__=="__main__"): 729 730 import MW_param 731 MW_param.go_to_main_dir() 732 733 P_dir,MW_dir=MW_param.detect_SubProcess(P_mode=1) 734 735 opt=sys.argv 736 if len(opt)<2: 737 listdir=os.listdir('./Source/MadWeight/transfer_function/data') 738 print 'Available TF function:\n ', 739 print '\n '.join([content[3:-4] for content in listdir if (content.startswith('TF') and content.endswith('dat'))]) 740 name=raw_input('Choose your Transfer Function\n') 741 else: 742 name=opt[1] 743 if name in ['proc_card.dat','auto']: 744 name=extract_tf_name('./Cards/proc_card.dat') 745 if len(opt)==3: 746 made_make=int(opt[2]) 747 else: 748 made_make=0 749 os.chdir('./Source/MadWeight/transfer_function') 750 create_TF_main(name,made_make,MW_dir) 751 752 ## file=raw_input("file: ") 753 ## ff=open(file,'r') 754 ## gg=open(file+'_70.f','w') 755 ## text=ff.read() 756 ## text=MW_fct.put_in_fortran_format(text) 757 ## gg.writelines(text) 758