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