import os import sys import pprint # Replace the header of primaryParticles_d1_mum_E10-100_theta0...dat files def replace_header(fileName, header): file_old = "oldsensorsFiles/"fileName file_new = fileName # Open primaryParticles_d1_mum_E10-100_theta0...dat and read line by line with open(file_old) as f1: header = f1.readline() print("header=",header) # Open the file to fill with the extracted information with open(file_new, "w") as f2: f2.write(new_header) for line in f1: f2.write(line) def main(sensorsFile, primPartFile): primPart_header = "eventID,particleName,energy(GeV),thetaGunDir(deg),phiGunDir(deg),xHitPos(mm),yHitPos(mm),zHitPos(mm),globalTime(s)\n" sensors_header = "eventID,cellID,energy(MeV),x(mm),y(mm),z(mm)\n" replace_header(primPartFile, primPart_header) replace_header(sensorsFile, sensors_header) if __name__=='__main__': import optparse import importlib usage = 'usage: %prog [options]' parser = optparse.OptionParser(usage) parser.add_option('--sensorsFile', dest='sensorsFile', help='data file from the sensors ', default='sensors_hit.dat') parser.add_option('--primParticlesFile', dest='primParticlesFile', help='file with primary particles informations ', default='primParticles_infos.dat') (opt, args) = parser.parse_args() main(opt.sensorsFile, opt.primParticlesInfosFile)