00001 '''
00002 This module stores and accesses the datasets
00003 to be used in NtupleWriter.
00004 '''
00005 __version__ = '$Revision: 1$'
00006 __author__ = 'Steffen.Kaiser'
00007
00008
00009
00010
00011
00012 import os, sys
00013
00014 class DataLib(object):
00015 '''Class which stores and finds the datasets'''
00016
00017 def __init__(self):
00018 self.itemsitems = []
00019
00020 def __str__(self):
00021 return_string = ''
00022 for i in range(len(self.itemsitems)):
00023 return_string += str(self.itemsitems[i]) + '\n'
00024 return_string = return_string.replace('DataSet:','DataSet[%i]:' % i)
00025 return return_string
00026
00027 def __len__(self):
00028 return len(self.itemsitems)
00029
00030 def check_existence(self, item):
00031 '''Check if dataset already exists in the library;
00032 return True if it exists'''
00033 for dataset in self.itemsitems:
00034 if(item.compare(dataset) == True):
00035 return True
00036 return False
00037
00038 def add(self, item):
00039 '''Add dataset to the library'''
00040 assert (self.check_existencecheck_existencecheck_existencecheck_existence(item) == False),\
00041 str(item) + '\nalready exists'
00042 self.itemsitems.append(item)
00043
00044 def add_dataset(self, d_short='', d_long='',path=''):
00045 '''Create dataset and add it to the library'''
00046 self.addaddaddadd( DataSet(d_short, d_long, path) )
00047
00048 def get(self, i):
00049 '''Return dataset i'''
00050 return self.itemsitems[i]
00051
00052 def find_dataset(self, description):
00053 '''Return dataset with the short or long description: description'''
00054 print 'find dataset with short/long description: ', description
00055
00056 ds = DataSet()
00057 for dataset in self.itemsitems:
00058
00059 if (dataset.get_short() == description) or\
00060 (dataset.get_long() == description):
00061
00062 ds = dataset
00063 break
00064
00065 if(ds.is_empty() == True):
00066 print 'Error: Couldn\'t find datset!'
00067 sys.exit()
00068 else:
00069 return ds
00070
00071
00072 class DataSet(object):
00073 '''Class which stores the information for one dataset'''
00074
00075 def __init__(self, d_short='', d_long='',path=''):
00076 self.resetresetresetreset(d_short, d_long, path)
00077
00078 def __str__(self):
00079 return_string = 'DataSet:\n'
00080 return_string += ' d_short: ' + self.d_shortd_short + ',\n'
00081 return_string += ' d_long: ' + self.d_longd_long + ',\n'
00082 return_string += ' path: ' + self.pathpath
00083 return return_string
00084
00085 def reset(self, d_short, d_long, path):
00086 assert (type(d_short) is str), 'd_short is not a string'
00087 assert (type(d_long) is str), 'd_long is not a string'
00088 assert (type(path) is str), 'path is not a string'
00089 self.d_shortd_short = d_short
00090 self.d_longd_long = d_long
00091 self.pathpath = path
00092
00093 def is_empty(self):
00094 '''Return True if dataset is empty'''
00095 if(self.d_shortd_short != ''):
00096 return False
00097 if(self.d_longd_long != ''):
00098 return False
00099 if(self.pathpath != ''):
00100 return False
00101 return True
00102
00103 def compare(self, item):
00104 '''Compare this dataset to another; return True if d_short or d_long agree'''
00105 if (self.d_shortd_short == item.d_short) or\
00106 (self.d_shortd_short == item.d_long):
00107 return True
00108 if (self.d_longd_long == item.d_short) or\
00109 (self.d_longd_long == item.d_long):
00110 return True
00111 return False
00112
00113 def get_short(self):
00114 '''Return short description of the dataset'''
00115 return self.d_shortd_short
00116
00117 def get_long(self):
00118 '''Return long description of the dataset'''
00119 return self.d_longd_long
00120
00121 def get_path(self):
00122 '''Return path of the dataset'''
00123 return self.pathpath
00124
00125
00126
00127
00128
00129
00130
00131 def include_files(path, EventSelector):
00132 '''Method which appends all .root files in path/ to InputCollections'''
00133
00134 assert (os.path.exists(path)), path + ' doesn\'t exist'
00135
00136
00137 files=os.listdir(path)
00138
00139 for filename in files:
00140
00141
00142
00143 if filename.find(".root") != -1:
00144
00145 EventSelector.InputCollections += [path + '/' + filename]
00146
00147
00148 print '\n************************************************************\n'
00149 print ' Runing on the following files: \n'
00150
00151 print 'path: ', path, '\n'
00152 print 'files: ', files, '\n'
00153
00154 print 'InputColl:', EventSelector.InputCollections
00155 print '\n************************************************************\n'