ATLAS Offline Software
Loading...
Searching...
No Matches
python.MGC.ParamCard Class Reference
Collaboration diagram for python.MGC.ParamCard:

Public Member Functions

 __init__ (self, param_card_input=None, param_card_backup=None, process_dir=MADGRAPH_GRIDPACK_LOCATION, output_location=None)
 read_paramCard (self)
 read_decayParams (self, cardloc=None)
 modify_paramCardDict (self, params={})
 write_paramCard (self)

Public Attributes

str paramCard_loc = process_dir+'/Cards/param_card.dat'
str paramCard_default_loc = param_card_backup
 output_location = output_location
 process_dir = process_dir
dict paramCardDict = {}

Detailed Description

Definition at line 714 of file MGC.py.

Constructor & Destructor Documentation

◆ __init__()

python.MGC.ParamCard.__init__ ( self,
param_card_input = None,
param_card_backup = None,
process_dir = MADGRAPH_GRIDPACK_LOCATION,
output_location = None )

Definition at line 715 of file MGC.py.

715 def __init__(self, param_card_input=None, param_card_backup=None, process_dir=MADGRAPH_GRIDPACK_LOCATION,output_location=None):
716
717 if param_card_input is None:
718 self.paramCard_loc = process_dir+'/Cards/param_card.dat'
719 elif param_card_input is not None and not os.access(param_card_input, os.R_OK):
720 self.paramCard_loc = param_card_input
721
722
723
724 self.paramCard_default_loc = param_card_backup
725 self.output_location = output_location
726 self.process_dir = process_dir
727
728 #read in the paramCard and store as a dictionary
729 self.read_paramCard()
730

Member Function Documentation

◆ modify_paramCardDict()

python.MGC.ParamCard.modify_paramCardDict ( self,
params = {} )
 Simple function to update the paramCardDictionary that uses nested dictionaries.
The input params should also be a set of nested dictionaries

Definition at line 849 of file MGC.py.

849 def modify_paramCardDict(self,params={}):
850 """ Simple function to update the paramCardDictionary that uses nested dictionaries.
851 The input params should also be a set of nested dictionaries
852 """
853
854 dict_lower = [v.lower() for v in self.paramCardDict]
855 for block in params: #for each block in the params dictionary
856 if block.strip().lower() in dict_lower:# if the block is found
857 # need to make this case-insensitive
858 for value in self.paramCardDict:
859 if block.strip().lower() == value.strip().lower():
860 name = value
861
862 for key in params[block]: #look at each key in the block sub-dictionary
863 k = key.strip()
864 if k in self.paramCardDict[name]: # if the key is in the paramCardDict block then update it
865 self.paramCardDict[name][key] = params[block][key]
866 elif len(k.split(' ',1)) > 1:
867 new_k = k.split(' ',1)[0].strip() + ' ' + k.split(' ',1)[1].strip()
868 if new_k in self.paramCardDict[name]:
869 self.paramCardDict[name][new_k] = params[block][key]
870 else:
871 mglog.warning("Looks like the parameter "+str(block)+" : "+str(key)+" isn't in the parameter card dictionary. Adding now!")
872 self.paramCardDict[name][new_k] = params[block][key]
873 else:
874 # if we can't find it we may be trying to update the parameter based on the name, not the block number
875 for value in self.paramCardDict[name]:
876 found = False
877 if '# '+str(key)+' ' in str(self.paramCardDict[str(name)][str(value)]): # look at the values for each element in the sub-dictionary
878 self.paramCardDict[name][value] = params[block][key] # if we find the key in the value, we will update that value
879 found = True
880 continue
881 if not found: # if we can't find that value, we will add a new one
882 mglog.warning("Looks like the parameter "+str(block)+" : "+str(key)+" isn't in the parameter card dictionary. Adding now!")
883 self.paramCardDict[name][key] = params[block][key]
884
885 else:# if the block is not in the paramCardDict, we will add the whole block
886 self.paramCardDict[block] = params[block]
887
888

◆ read_decayParams()

python.MGC.ParamCard.read_decayParams ( self,
cardloc = None )
 The DECAY parameters are written out differently in param_card.dat compared to the other parameter blocks
This funciton reads in the Decay parameters and adds them to the self.paramCardDict

Definition at line 783 of file MGC.py.

783 def read_decayParams(self, cardloc = None):
784 """ The DECAY parameters are written out differently in param_card.dat compared to the other parameter blocks
785 This funciton reads in the Decay parameters and adds them to the self.paramCardDict
786 """
787
788 if cardloc is None:
789 if os.access(self.paramCard_loc, os.R_OK):
790 mglog.info('Copying default param card from '+str(self.paramCard_loc))
791 cardloc = self.paramCard_loc
792 elif os.access(self.paramCard_default_loc, os.R_OK):
793 mglog.info('Copying default param card from '+str(self.paramCard_default_loc))
794 cardloc = self.paramCard_default_loc
795 else:
796 raise RuntimeError('You did not give a card location for reading in DECAY parameters and we cannot find defualt param_card.dat or param_card_default.dat! I was looking here: %s'%self.paramCard_loc)
797
798 with open(cardloc, 'r') as f:
799 card = f.read()
800 # Break the card up by lines
801 param_lines = card.split('\n')
802 decay_params = {}
803 setting = {}
804 key = None
805 value = None
806 for line in param_lines:
807 # Get rid of and leading or trailing spaces
808 l = line.strip()
809
810 # If the Line starts with Decay
811 if l.lower().startswith('decay'):
812 decay = l[:5].strip()
813 #check to see if there is already a key and value
814 if key is not None and value is not None: # In other words, if we have already recorded a decay parameter, we want to add that to the settings
815 setting.update({key:value})
816 # Reset the key and value
817 key = None
818 value = None
819 # Record the PDG ID for the particular decay
820 key = l[5:].strip().split(' ',1)[0]
821 # We keep the entire line as the value
822 value = l
823 # Sometimes the decay parameter spans several lines, we want to make sure we get all of it.
824 # If the line is not a a new Decay parameter, it is not an empty line and we do have a key + value saved:
825 elif not l.lower().startswith('decay') and not l == '\n' and key is not None and value is not None and not l.lower().startswith('block'):
826 # Add the current line to the value (making sure we include the new line)
827 value = value + '\n' + l
828 elif l.lower().startswith('block') and len(setting) != 0: # if we reach a new block after reading in the decays then we can just stop running
829 # add the last setting before adding to a decay_params dictionary
830 setting.update({key:value})
831
832 decay_params[decay] = setting
833 # add the decay parameters to the paramCardDict
834 self.paramCardDict.update(decay_params)
835
836 mglog.info("Successfully read in Decay parameters")
837 return
838 else:
839 continue
840 # if the decay block is the last block in the card, add the last setting before adding to a decay_params dictionary
841 setting.update({key:value})
842
843 decay_params[decay] = setting
844 # add the decay parameters to the paramCardDict
845 self.paramCardDict.update(decay_params)
846
847 mglog.info("Successfully read in Decay parameters")
848
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179

◆ read_paramCard()

python.MGC.ParamCard.read_paramCard ( self)

Definition at line 731 of file MGC.py.

731 def read_paramCard(self):
732 if os.access(self.paramCard_loc, os.R_OK):
733 mglog.info('Copying default param card from '+str(self.paramCard_loc))
734 param_card = self.paramCard_loc
735 elif os.access(self.paramCard_default_loc, os.R_OK):
736 mglog.info('Copying default param card from '+str(self.paramCard_default_loc))
737 param_card = self.paramCard_default_loc
738 else:
739 raise RuntimeError('Cannot find defualt param_card.dat or param_card_default.dat! I was looking here: %s'%self.paramCard_loc)
740
741 with open(param_card, 'r') as f:
742 card = f.read()
743
744 param_blocks = card.split('\n\n')
745
746 self.paramCardDict = {}
747 for block in param_blocks:
748 name = None
749 nParams = 0
750 setting = {}
751 for line in block.split('\n'):
752 if line.lower().startswith('block'):
753 if name is not None and setting != {}:
754 self.paramCardDict[name] = setting
755 nParams = 0
756 name = line.split(' ',1)[1].strip()
757 setting ={}
758 nParams+=1
759 elif line.startswith('#'):
760 continue
761 elif line.lower().startswith('decay'):
762 continue #temp while I write function to get DECAY params
763 else:
764 l = line.strip()
765 data, separator, comment = l.partition('#')
766 columns = data.split()
767 if len(columns) < 2:
768 continue
769 key, value = ' '.join(columns[:-1]), columns[-1]
770 if separator:
771 value += ' # ' + comment.strip() + ' '
772
773 setting.update({key.strip() : value})
774
775 if name is not None and setting != {}:
776 self.paramCardDict[name] = setting
777 nParams = 0
778
779 self.read_decayParams(cardloc = param_card)
780 mglog.info("Successully read param_card.dat as a dictionary paramCardDict")
781
782

◆ write_paramCard()

python.MGC.ParamCard.write_paramCard ( self)
Write out paramCardDict to disk. 
The function will copy the layout and format from the default card. 

Definition at line 889 of file MGC.py.

889 def write_paramCard(self):
890 """Write out paramCardDict to disk.
891 The function will copy the layout and format from the default card.
892 """
893 if self.paramCard_default_loc is None or not os.path.isfile(self.paramCard_default_loc):
894 self.paramCard_default_loc = self.paramCard_loc +'.old_to_be_deleted'
895 os.rename(self.paramCard_loc, self.paramCard_default_loc)
896
897 with open(self.paramCard_default_loc,'r') as f:
898 oldCard = f.read()
899
900 newCard = open(self.paramCard_loc,'w')
901 dict_blocks = [v.lower() for v in self.paramCardDict]
902
903 oldCard_blocks = oldCard.split('\n\n')
904
905 for block in oldCard_blocks:
906 name = None
907 nParams = []
908
909 for line in block.split('\n'):
910 l = line.strip()
911 if l.startswith('#'):
912 newCard.write(f"{line} \n")
913 elif l == '':
914 newCard.write("\n")
915 elif l.lower().startswith('block'):
916 if name is not None and len(nParams) == len(self.paramCardDict[name]):
917 name = None
918 nParams = []
919 # If we are at a new block and we have not finished writing all the params from the dictionary
920 elif name is not None and len(nParams) != len(self.paramCardDict[name]):
921 # going through each entry in the param card dictionary
922 for key in self.paramCardDict[name]:
923 # if key is in nParams, it means we have already written it
924 if key in nParams:
925 continue
926 elif key not in nParams:
927 newCard.write(f" {key} {self.paramCardDict[name][key]}\n")
928 nParams.append(key)
929
930 name = l.split(' ',1)[1].strip()
931 nParams = []
932 if name.lower() not in dict_blocks:
933 raise RuntimeError("Cannot find %s in paramCardDict"%str(name))
934 elif name not in self.paramCardDict:
935 for b in self.paramCardDict:
936 if b.lower() == name.lower():
937 name = b
938 else:
939 continue
940
941 newCard.write(f"Block {name}\n")
942 elif l.lower().startswith('decay'):
943 # just to make sure we have written everthing down from the previous section
944 if name is not None and name.lower() != 'decay':
945 if len(nParams) == len(self.paramCardDict[name]):
946 continue
947 elif len(nParams) != len(self.paramCardDict[name]):
948 # going through each entry in the param card dictionary
949 for key in self.paramCardDict[name]:
950 # if key is in nParams, it means we have already written it
951 if key in nParams:
952 continue
953 elif key not in nParams:
954 newCard.write(f" {key} {self.paramCardDict[name][key]}\n")
955 nParams.append(key)
956
957 nParams = []
958
959 name = l[:5].strip()
960
961 command = l[5:].strip()
962 ID = command.split(' ',1)[0]
963 nParams.append(ID)
964
965 newCard.write(f"{self.paramCardDict[name][ID]} \n")
966
967 elif l == '\n':
968 newCard.write(l)
969
970 else:
971 if name.lower() == 'decay':
972 continue
973 else:
974 ID = ' '.join(l.partition('#')[0].split()[:-1])
975 newCard.write(f" {ID} {self.paramCardDict[name][ID]}\n")
976 nParams.append(ID)
977
978
979 # at end of block
980 if name is not None and len(nParams) == len(self.paramCardDict[name]):
981 name = None
982 nParams = []
983 # If we are at a new block and we have not finished writing all the params from the dictionary
984 elif name is not None and len(nParams) != len(self.paramCardDict[name]):
985 # going through each entry in the param card dictionary
986 for key in self.paramCardDict[name]:
987 # if key is in nParams, it means we have already written it
988 if key in nParams:
989 continue
990 elif key not in nParams and key is not None and key.strip() != '':
991 newCard.write(f" {key} {self.paramCardDict[name][key]}\n")
992 nParams.append(key)
993 elif key is None or key.strip() == '':
994 continue
995
996 mglog.info("Finished writing paramCardDict to param_card.dat")

Member Data Documentation

◆ output_location

python.MGC.ParamCard.output_location = output_location

Definition at line 725 of file MGC.py.

◆ paramCard_default_loc

python.MGC.ParamCard.paramCard_default_loc = param_card_backup

Definition at line 724 of file MGC.py.

◆ paramCard_loc

python.MGC.ParamCard.paramCard_loc = process_dir+'/Cards/param_card.dat'

Definition at line 718 of file MGC.py.

◆ paramCardDict

dict python.MGC.ParamCard.paramCardDict = {}

Definition at line 746 of file MGC.py.

◆ process_dir

python.MGC.ParamCard.process_dir = process_dir

Definition at line 726 of file MGC.py.


The documentation for this class was generated from the following file: