#!/bin/csh

setenv place   $1
setenv owner   $2
setenv dstname $3
setenv run     $4
setenv jumpev  $5
setenv event   $6



if ( $event == "" )   then

echo ' '
echo '#####################################################################'
echo ' '
echo ' A) interactive usage :'
echo ' '
echo '   source run_db.csh place owner dstname run# #evjump #events '
echo ' '
echo ' examples : '
echo '   source run_db.csh cern jet1034PUjm440_CERN jm_hlt010 0 0 2 '
echo '   source run_db.csh cernold 1034Pileup hlt1015 6 10 2 '
echo '   source run_db.csh cernold 1034UniquePileup filt_minbias2k 0 0 2 '
echo '   source run_db.csh cerneg  1034Pileup hlt1015 6 10 2 '
echo '   source run_db.csh fnalpal    lumi1030 jm_pi_pt30 0 0 3 '
echo '   source run_db.csh fnalyujun  lumi1030 jm_wjet 0 0 3 '
echo '   source run_db.csh fnaltest   lumi1030 jm_hlt2030 0 0 3 '
echo '   source run_db.csh fnal jetHit120_2D_FNAL jm_hplus_m400_1034 0 0 500 '
echo ' '
echo '   if you want all the runs, write 0 as run# '
echo ' '
echo '   or if you want to write these things in the script then edit it '
echo '   and replace the parameters with names inside here '
echo '   but do not do that, if you want to use the batch job script '
echo ' '
echo '       some possible owner names : '
echo '       NoPileup  NoPileup2  1034PurePileup  1034Pileup '
echo '       1034IntimeUnique  1034UniquePileup   1033Pileup '
echo ' '
echo ' '
echo ' B) batch usage :'
echo ' '
echo '           bsub [options] run_db_batch.job    (after editing it)'
echo ' '
echo '#####################################################################'

echo ' '
echo '------------------------------------------------------'
echo 'too few parameters provided, follow instructions above'
echo '------------------------------------------------------'
echo ' '
exit 0

else

    setenv MYNAME {$dstname}_{$run}_{$jumpev}
    rm -f $MYNAME.txt
    touch $MYNAME.txt
    echo 'number of parameters for run_db.csh is ok' >> $MYNAME.txt

endif


###########################################################################
# set up runtime environment
###########################################################################

eval `scram runtime -csh`

###########################################################################
# .orcarc control cards
###########################################################################

/bin/rm -f .orcarc
cat > .orcarc <<@EOF
RecApplication:InputRun = $run
RecApplication:FirstEvent = $jumpev
RecApplication:MaxEvents = $event
HepEventG3EventProxyReader:ReadPileup = 0
HepEventG3EventProxyReader:ReadVertices = 0
Configuration:History = 0
@EOF

###########################################################################
# user federation (the data base, which you want to read)
###########################################################################

switch ( $place )

  case cern:

    setenv OO_FD_BOOT cmspf02::/cms/reconstruction/prod/jetPROD0900/jet0900.boot

  breaksw

  case cernold:

    setenv OO_FD_BOOT cmsuf01.cern.ch::/cms/reconstruction/user/jetDIGI0300/jetDIGI0300.boot

  breaksw

  case cerneg:

    setenv OO_FD_BOOT suncmsc::/data/egamma/EGDB/jetDIGI0300.boot

  breaksw

  case fnalyujun:

    setenv OO_FD_BOOT wonder.fnal.gov::/data/yujun/OO440Fed/jet0900.boot

  breaksw

  case fnalpal:

    setenv OO_FD_BOOT wonder.fnal.gov::/data/hidaspal/OO440Fed/jet0900.boot

  breaksw

  case fnaltest:

    setenv OO_FD_BOOT wonder.fnal.gov::/data/hidaspal/OO440Federation/jet0900.boot

  breaksw

  case fnal:

    setenv OO_FD_BOOT cmsun1.fnal.gov::/usr/cmsun1/r4test/UserFederation/jet0900.boot

  case fnalprod:

    setenv OO_FD_BOOT velveeta.fnal.gov::/data/jmFederation/jet0900.boot

  breaksw
endsw

###########################################################################
# objectivity owner name     
###########################################################################

#--- at CERN
# setenv CARF_INPUT_OWNER ZeroPileup
# setenv CARF_INPUT_OWNER NoPileup2
# setenv CARF_INPUT_OWNER NoPileup
# setenv CARF_INPUT_OWNER 1034Pileup
#--- at FNAL
# setenv CARF_INPUT_OWNER lumi1030
# setenv CARF_INPUT_OWNER lumi1034

setenv CARF_INPUT_OWNER $owner

###########################################################################
# where to read PID table
###########################################################################

if (${?EXHEPPDTABLE} == 0)   then
     setenv EXHEPPDTABLE $SCRAMRT_ORCA_DATA_DIR/HepPDTData/particle_table
else
  if ( -e $EXHEPPDTABLE ) then
     echo "Old file for EXHEPPDTABLE exists" >> $MYNAME.txt
  else
     echo "Old file for EXHEPPDTABLE ($EXHEPPDTABLE) does not exists" >> $MYNAME.txt
     setenv EXHEPPDTABLE $SCRAMRT_ORCA_DATA_DIR/HepPDTData/particle_table
  endif
endif

echo "Reading particle data from $EXHEPPDTABLE" >> $MYNAME.txt

###########################################################################
# data set and job name
###########################################################################

setenv CARF_INPUT_DATASET_NAME $dstname
setenv CARF_INPUT_EVCOLL_NAME  $CARF_INPUT_DATASET_NAME
echo ' ' >> $MYNAME.txt
#
#
echo ' MYNAME = ' $MYNAME >> $MYNAME.txt
echo ' CARF_INPUT_DATASET_NAME = ' $dstname >> $MYNAME.txt

###########################################################################
# let time to acces the federation
###########################################################################

setenv OO_RPC_TIMEOUT 1000

###########################################################################
# Run and see what you did ;-)    
#
# The lines below the "JetMet" executable are read by the program
#
# Do not change the place and the number of "comment" lines !!!
# Their content  and length are irrelevant for the job, 
# they are information for you.
#
# Change the values below the comment lines, if you wish, 
# the default (recommended) values are at the end of the comment line
###########################################################################

echo 'start running JetMet' >> $MYNAME.txt

JetMet >>& $MYNAME.txt << NNN
######_partetcut_(generated_particle_Et_cut)_(0.8)
0.8
######_toweretcut_(offline_trigger_tower_Et_cut_in_GeV)_(0.01)
0.01
######_toweretcut2_(extra_cut_for_MET2)_(0.5)
0.5
######_toweretcut4_(extra_cut_for_MET2)_(1.0)
1.0
######_toweretcut6_(extra_cut_for_MET2)_(1.5)
1.5
######_toweretcut8_(extra_cut_for_MET2)_(2.0)
2.0
######_trigprimetcut_(trigger_primitive_Et_cut_in_GeV)_(0.10)
0.10
######_jetetcut_(offline_jet_Et_cut_in_GeV)_(10.)
10.
######_genjetetcut_(generated_jet_Et_cut_in_GeV)_(10.)
10.
######_jetEnergyCone1_(conesize_in_which_energy_sum_is_provided)_(0.13)
0.13
######_jetEnergyCone2_(conesize_in_which_energy_sum_is_provided)_(0.40)
0.40
######_jetEnergyCone3_(conesize_in_which_energy_sum_is_provided)_(0.50)
0.50
######_jetEnergyCone4_(conesize_in_which_energy_sum_is_provided)_(0.70)
0.70
######_digiecutebry_(digi_E_cut_in_GeV_for_ecal_barrel)_(0.06)
0.06
######_digiecutefry_(digi_E_cut_in_GeV_for_ecal_endcap)_(0.30)
0.30
######_digiecuthcal_(digi_E_cut_in_GeV_for_hcal)_(0.01)
0.01
######_digiecutpres_(digi_E_cut_in_GeV_for_preshower)_(0.01)
0.01 
######_digimineta_(minimum_eta_for_digis)_(-7.0)
-7.0
######_digimaxeta_(maximum_eta_for_digis)_(7.0)
7.0
######_algoNumber_(jet_algorithm_number)_(1:iterative_cone|2:simple)_(1)
1
######_conesize_(cone_size_for_jet_finder_algorithm)_(0.5)
0.5
######_seedcut_(seed_cut_for_jet_finder_algorithm)_(1.)
1.
######_jetInput_(input_for_jets)_(1:CaloRecHits|2:EcalPlusHcalTower)_(2)
2
######_conesize2_(second_cone_size_for_jet_finder_algorithm)_(0.7)
0.7
######_seedcut2_(second_seed_cut_for_jet_finder_algorithm)_(1.)
1.
######_L1metMin1_(print_pileup_ID_if_overcome_for_debug)_(50.)
50.
######_L1metMin2_(write_entire_event_to_ntuple_if_overcome_for_debug)_(-1.)
-1.
######_epsilon_(for_various_comparisons)_(0.01)
0.01
######_scedebug_(0:no_debug|1-2-3-...:increasing_debug_level)_(0)
0
######_generatorprint_(0:no_print|1:print_gen_particles_and_trees)_(0)
0
######_stableparticleprint_(0:no_print|1:print_stable_charged_gen_part)_(0)
0
######_towerprint_(0:no_print|1:print_offline_towers)_(0)
0
######_digiprint_(0:no_print|1:print_digis)_(0)
0
######_digimap_(0:no_digimap|1:digimap_is_printed)
0
######_ntuple_name
$MYNAME.ntup
######_____ntuple_blocks
######_dospheri_(if_not_0_put_Spheri_into_ntuple)
1
######_dofox_(if_not_0_calculate_and_put_foxwolfram_into_ntuple)
1
######_domet_(if_not_0_put_MET_into_ntuple)
1
######_domet2_(if_not_0_put_MET2_into_ntuple)
1
######_doavedigi_(if_not_0_put_some_averages_on_digis_into_ntuple)
1
######_dodigi_(if_not_0_put_digis_into_ntuple)
0
######_dotowers_(if_not_0_put_offline_trigger_towers_into_ntuple)
0
######_dojets_(if_not_0_put_offline_jets_into_ntuple)
1
######_dojets2_(if_not_0_put_offline_jets_into_ntuple_second_iteration)
1
######_dogenjets_(if_not_0_put_generated_jets_into_ntuple)
1
######_dogenpart_(if_not_0_put_generated_particles_into_ntuple)
1
######_doparton_(if_not_0_put_partons_into_ntuple)
1
######_dol1t_(if_not_0_put_Level_1_towers_into_ntuple)
1
######_dotrigprim_(if_not_0_put_trigger_primitives_into_ntuple)
1
######_dombid_(if_not_0_put_minbias_pileup_ids_into_ntuple)
0
######_____user_histograms
######_dohistos_(if_not_0_then_some_user_histograms_are_filled)
1
NNN
#-----
exit 0



