1
2
3
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
24
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('../../../')
49
50
51
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
124
125
126
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
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
242
243
274
275 -class Full_TF(TF_input,TF_with_particles):
276 """ class containing both extension of XML_input """
277
282
284 """ class containing the information for a specific block from TF_param.dat """
285
286
295
296
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
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
317 """ store the information on the block (comming from the xml) """
318 self.infotext=text
319
320
322 text = 'Parameter for particles: '+','.join(self.particles)+'\n'
323 text += 'Information:'+self.infotext+'\n'
324
325 return text
326
327
328
329
331 """ class containing the TF/WIDTH for a specific type """
332
333
335 """ initialize the content to delta """
336 self.tf_code=' tf=1d0'
337 self.width_code=' width=0d0'
338 self.includetext= ''
339
340
342 """ store the information on the block (comming from the xml) """
343 self.includetext=text
344
345
346
349
350
353
354
356 """ Class for building transfert functions routine in a specific SubProcess"""
357
358
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()
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
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
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
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()
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()
405
406 if pid in ['x1','x2']:
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
435
436
437
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
444 text='$B$ START_CENTRAL_POINT $E$\n'
445
446 external_done=[]
447
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
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
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
466 if blockname != -1:
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
472 variable=['THETA','PHI','E']
473 for j in range(1,4):
474 if blockname == -1:
475 text+=' c_point(perm,%s,%s,2)=-1d0\n' % (i+1,j)
476 elif blockname == 0:
477 text+=' c_point(perm,%s,%s,2)=0d0\n' % (i+1,j)
478 else:
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'
481
482 text+='\n enddo \n return\n end\n'
483 return text
484
485
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
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
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
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:
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'
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
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=''
535
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
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
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
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]
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
621
622
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
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
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
653 ff=open('./Transfer_FctVersion.txt','r')
654 line=ff.readline().split(':',1)
655 ff.close()
656
657
658
659
660
661 ff=open('./Transfer_FctVersion.txt','w')
662 ff.writelines(name+':'+line[1])
663 ff.close()
664
665 return
666
667
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 ")
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
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
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
707
708
709
710
711
712
713
714
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]
722 return name
723 if line.startswith('# Begin transfer_function'):
724 found=1
725
726
727
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
753
754
755
756
757
758