#########################################################################
#
# Script: generate_fixeddir_oneovere_particles.py
#
# Author: Igor Volobouev, Feb 2005
#
# Purpose: invokes a simple cosmic ray MC generator which makes
#          cosmic rays with fixed direction and 1/E energy spectrum.
#          The output is written to stdout in the format acceptable
#          by Gleam.
#
#########################################################################

"""
Usage: generate_fixeddir_oneovere_particles nevents particleName emin emax
                              max_impact_par backoff_distance theta phi t0
"""

import random_seed
import sys
import math

from cr_sources import *
from source_utils import *
from basic_kinematics import *
from simple_generator import *
import coords

def main(argv=None):
    # parse the command line
    if argv is None:
        argv = sys.argv
    argc = len(argv)-1
    if (argc == 0):
        print __doc__
        return 0
    elif (argc == 9):
        i = 1
        nevents = int(argv[i]); i+=1
        particleName = argv[i]; i+=1
        emin = float(argv[i]); i+=1
        emax = float(argv[i]); i+=1
        max_impact_par = float(argv[i]); i+=1
        backoff_distance = float(argv[i]); i+=1
        theta = float(argv[i])/180.0*math.pi; i+=1
        phi = float(argv[i])/180.0*math.pi; i+=1
        t0 = int(argv[i]); i+=1
    else:
        print __doc__
        return 1
    # create the cosmic ray generator
    dir = V3(math.sin(theta)*math.cos(phi), \
             math.sin(theta)*math.sin(phi), \
             math.cos(theta))
    label = CosmicRayLabel("test", ())
    generator = DirectedTubeSource(label, coords.LOCAL, particleName, \
                                   PowerLawSpectrum(-1, emin, emax), \
                                   max_impact_par, backoff_distance, dir)
    # write "nevents" events to the standard output
    generate_simple_events(t0, generator, nevents, sys.stdout)
    return 0

if __name__=='__main__':
    sys.exit(main())
