"""Class to process neut setup. Neut is run as a program external to the main comet code. This module does normalization calculations that need only be done once per job type. The results should be fed back in via the config file for neutMC """ import cometModule import cometGeometry import cometRun import geomNameCheck import os class neutSetup(cometModule.cometModule): """A class to run neut Monte Carlo setup """ ########################################################### def getOutputFilename(self): """Get the output filename""" self.outname = self.createFileName("dummy") return self.outname ########################################################### def getModuleName(self): """Return module name""" return "neutSetup" ########################################################### def getShortModuleName(self): """Return module name""" return "nesp" ####################################################################### def checkModuleConfig(self): """Checks the configuration""" # Make sure that the enviromnet setup script exists. if self.config.options.has_option("software","neut_setup_script"): script = self.config.options.get("software","neut_setup_script") if not os.path.isfile(script): self.config.log.error("Cannot locate specified neut setup script "+script) return False else: self.config.log.error("Neut MC being run, but setup script not set in the config file. Please use the software/neut_setup_script option") return False if self.config.options.has_option("neutrino","flux_file_path"): # Place holder for compilation path = self.config.options.get("neutrino","flux_file_path") if self.config.options.has_option("neutrino","flux_file_start"): path = self.config.options.get("neutrino","flux_file_path") first = self.config.options.get("neutrino","flux_file_start") fname = path + "." + first + ".root" if not os.path.exists(fname): self.config.log.error("Cannot find file "+fname+" please check neutrino/flux_file_path and neutrino/flux_file_start. Cannot continue") return False else: self.config.log.error("Neut MC being run, but neutrino/flux_file_start not specified. Cannot continue") return False if self.config.options.has_option("neutrino","flux_file_stop"): path = self.config.options.get("neutrino","flux_file_path") lastf = self.config.options.get("neutrino","flux_file_stop") fname = path + "." + lastf + ".root" if not os.path.exists(fname): self.config.log.error("Cannot find file "+fname+" please check neutrino/flux_file_path and neutrino/flux_file_stop. Cannot continue") return False else: self.config.log.error("Neut MC being run, but neutrino/flux_file_stop not specified. Cannot continue") return False elif self.config.options.has_option("neutrino","flux_file"): flux_file_name = self.config.options.get("neutrino","flux_file") else: self.config.log.error("Neut MC being run, but neutrino/flux_file_path not specified. Cannot continue") return False if self.config.options.has_option("neutrino","neutrino_type"): nu_type = self.config.options.get("neutrino","neutrino_type").upper() if nu_type == "BEAM" or nu_type == "NUE" or nu_type == "NUEBAR"or nu_type == "NUMU" or nu_type == "NUMUBAR": path = "hello" else: self.config.log.error("Unrecognised neutrino type requested. Cannot continue") return False if self.config.options.has_option("neutrino","flux_region"): region = self.config.options.get("neutrino","flux_region").upper() if not(region == "MAGNET" or region == "BASKET" or region == "INGRID" or region == "SAND"): self.config.log.error("Neut MC being run with invalid fluxregion. Please selecet magnet, basket or sand Cannot continue") return False # Unless the the force_geom_name has been specified check that the # master_volume name matches to one in a list of known volumes. if geomNameCheck.CheckTopVolumeName(self.config) == False: return False return True ########################################################### def process(self): """Run the jobs for Neut""" # Get the geometry; either use geometry from SimG4 or use an override geometry (for sand simulation) geoFile = "" region = "MAGNET" if self.config.options.has_option("neutrino","flux_region"): region = self.config.options.get("neutrino","flux_region").upper() if region == "SAND": geoFile = self._generateSandGeometry() else: geoFile = self._generateGeometry() if not geoFile: self.config.log.stop("neutSetup: Error generating geometry file") if not os.path.isfile(geoFile): self.config.log.stop("neutSetup: Error generating geometry file") # Setup the job commands. # Start by executing the setup script script = self.config.options.get("software","neut_setup_script") job = "source "+script+"\n" # Run the setup script inside $NEUTGEOM job += "$NEUTGEOM/setup.sh\n" # Copy the neut.card file to the working directory if self.config.options.has_option("neutrino","neut_card"): card = self.config.options.get("neutrino","neut_card") else: card = "$NEUTGEOM/neut.card" job += "cp "+card+" neut.card\n " # Setup the random seed file ran1 = str(-1290784521) ran2 = str(387739155) ran3 = str(23874124) ran4 = str(39827) ran5 = str(0) ranseed = "\t" + ran1 + "\t" + ran2 + "\t" +ran3 + "\t" + ran4 + "\t" + ran5 + " " self.config.log.output("neutSetup using random seed "+ranseed) job += "echo "+ranseed+">random.tbl\n" # Run Neut job += "$NEUTGEOM/event_rate" # Specify Geometry job += " -g "+geoFile # Specify jnubeam path if self.config.options.has_option("neutrino","flux_file_path"): path = self.config.options.get("neutrino","flux_file_path") first = self.config.options.get("neutrino","flux_file_start") lastf = self.config.options.get("neutrino","flux_file_stop") job += " -s "+path+" "+first+" "+lastf elif self.config.options.has_option("neutrino","flux_file"): flux_file_name = self.config.options.get("neutrino","flux_file") job += " -f "+flux_file_name # Specify flux region in job command if (region =="BASKET"): job += " -d 5 " elif (region =="MAGNET"): job += " -d 6 " elif (region =="INGRID"): job += " -d 3 " elif (region =="SAND"): job += " -d 13 " #Specify the flux tree if self.config.options.has_option("neutrino","flux_tree"): tree = self.config.options.get("neutrino","flux_tree") job += " -t "+tree+" " # Define the master volume for generation. masterVolume = "" if self.config.options.has_option("neutrino","master_volume"): masterVolume = self.config.options.get("neutrino","master_volume") if masterVolume: job += " -v +"+masterVolume # Specify the neutrino type if self.config.options.has_option("neutrino","neutrino_type"): nu_type = self.config.options.get("neutrino","neutrino_type").upper() if nu_type == "BEAM": job += " " elif nu_type == "NUE": job += " -p 12" elif nu_type == "NUEBAR": job += " -p -12" elif nu_type == "NUMU": job += " -p 14" elif nu_type == "NUMUBAR": job += " -p -14" # Output maximum interaction probability file if self.config.options.has_option("neutrino","maxint_file"): outfile = self.config.options.get("neutrino","maxint_file") else: outfile = "neut_comet_"+masterVolume+"_setup_flux_"+region+"_"+str(first)+"_"+str(lastf)+".root" outfile = outfile.lower() job += " -o "+outfile # Done job += "\n" # Create an emtpy output file to finish things off. job += "touch "+outfile+"\n" self.run = cometRun.cometRun(job,self.config) self.run.setCallName(self.getModuleName()) # We don't want to configure cmt programs this time. self.run.setup = False self.run.run() self.config.log.output(self.run.stdout) self.config.log.error(self.run.stderr) ############################################################ def _generateGeometry(self): """Method to generate the geometry file We run a very quick SimG4 job to do this.""" geoname = self.createFileName("geo") macname = self.createFileName("mac") macContents = "" # Geometry Switches. cometGeom = cometGeometry.cometGeometry(self.config) geometryControl = cometGeom.getMACCommands() if geometryControl: macContents += geometryControl else: # Warning already printed. return False # Now generate a single muon to run macContents += """ /comet/update """ try: macFile = open(macname,"w") macFile.write(macContents) except: self.config.log.stop("SimG4: Cannot write mac file "+macname) macFile.close() job = "SIMG4.exe " + " -o " + geoname + " " + macname self.run = cometRun.cometRun(job,self.config) self.run.setCallName(self.getModuleName()+"-geo") self.run.run() self.config.log.output(self.run.stdout) self.config.log.error(self.run.stderr) # Remove the mac file if required. cleanup = True if self.config.options.has_option("neutrino","cleanup"): cleanup = self.config.options.getboolean("neutrino","cleanup") if cleanup: os.unlink(macname) return geoname+".root" ############################################################ def _generateSandGeometry(self): """Method to generate the sand geometry file""" job = "root -q -b -l ${SIMSANDROOT}/scripts/pit_geo_mm.C" self.run = cometRun.cometRun(job,self.config) self.run.setCallName(self.getModuleName()+"-sandgeo") self.run.run() self.config.log.output(self.run.stdout) self.config.log.error(self.run.stderr) return "pit_geometry_v3.root"