1
2
3
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
29
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('../../../')
54
55
56
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
129
130
131
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
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
247
248
279
280 -class Full_TF(TF_input,TF_with_particles):
281 """ class containing both extension of XML_input """
282
287
289 """ class containing the information for a specific block from TF_param.dat """
290
291
300
301
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
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
322 """ store the information on the block (comming from the xml) """
323 self.infotext=text
324
325
327 text = 'Parameter for particles: '+','.join(self.particles)+'\n'
328 text += 'Information:'+self.infotext+'\n'
329
330 return text
331
332
333
334
336 """ class containing the TF/WIDTH for a specific type """
337
338
340 """ initialize the content to delta """
341 self.tf_code=' tf=1d0'
342 self.width_code=' width=0d0'
343 self.includetext= ''
344
345
347 """ store the information on the block (comming from the xml) """
348 self.includetext=text
349
350
351
354
355
358
359
361 """ Class for building transfert functions routine in a specific SubProcess"""
362
363
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()
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
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
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
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()
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()
410
411 if pid in ['x1','x2']:
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
440
441
442
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
449 text='$B$ START_CENTRAL_POINT $E$\n'
450
451 external_done=[]
452
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
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
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
471 if blockname != -1:
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
477 variable=['THETA','PHI','E']
478 for j in range(1,4):
479 if blockname == -1:
480 text+=' c_point(perm,%s,%s,2)=-1d0\n' % (i+1,j)
481 elif blockname == 0:
482 text+=' c_point(perm,%s,%s,2)=0d0\n' % (i+1,j)
483 else:
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'
486
487 text+='\n enddo \n return\n end\n'
488 return text
489
490
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
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
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
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:
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'
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
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=''
540
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
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
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
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]
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
626
627
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
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
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
658 ff=open('./Transfer_FctVersion.txt','r')
659 line=ff.readline().split(':',1)
660 ff.close()
661
662
663
664
665
666 ff=open('./Transfer_FctVersion.txt','w')
667 ff.writelines(name+':'+line[1])
668 ff.close()
669
670 return
671
672
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 ")
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
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
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
712
713
714
715
716
717
718
719
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]
727 return name
728 if line.startswith('# Begin transfer_function'):
729 found=1
730
731
732
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
758
759
760
761
762
763