Belle II Software  release-05-01-25
train_K_L0_classifier.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Jo-Frederik.krohn@desy.de
4 
5 import sys
6 import os
7 
8 # USE THIS TO TRAIN AND UPLOAD A NEW CLASSIFIER FOR THE KLONG ID
9 # USE THE OTHER SCRIPT TO GENERATE DATA. THEN TRAIN AND UPLOAD WITH &THIS.
10 # only thing to set is the path were you have the data to train the classifier on.
11 
12 try:
13  data_path = sys.argv[1]
14 except BaseException:
15  data_path = "root_files/training/*.root"
16 try:
17  identifier = sys.argv[2]
18 except BaseException:
19  # the default name used in KLMExpert
20  identifier = "KLM_fBDT_10xbkg100k.xml"
21 
22 ex_b = 0 # experiment begin, 0 for all of them
23 ex_e = -1 # experiment end, -1 for all of them
24 run_b = 0 # run begin, 0 for all
25 run_e = -1 # run end, -1 for all of them
26 upload = True # upload to conditions database
27 remove_db = False # delete local db
28 tag_name = "development" # global tag name
29 
30 training_string = ('basf2_mva_teacher --datafiles {data_path} --treename KLMdata --identifier {identifier} ' +
31  '--variable "KLMnLayer" "KLMnInnermostlayer" "KLMglobalZ" "KLMtime" "KLMdistToNextCl" ' +
32  '"KLMenergy" "KLMTrackSepDist" "KLMInitialtrackSepAngle" "KLMTrackRotationAngle" ' +
33  '"KLMTrackSepAngle" "KLMhitDepth" "KLMECLenergy" "KLMECLE9oE25" "KLMECLtiming" ' +
34  '"KLMECLmintrackDist" "KLMECLZMVA" "KLMECLZ40" "KLMECLZ51" --target_variable isSignal ' +
35  '--method FastBDT --nTrees 400 --nCutLevels 8 --nLevels 4').format(data_path=data_path, identifier=identifier)
36 
37 os.system(training_string)
38 
39 # "upload" to localdb
40 os.system(
41  ('basf2_mva_upload --identifier {identifier} --db_identifier {identifier_db}' +
42  '--begin_experiment {ex_b} --end_experiment {ex_e} --begin_run {run_b} --end_run {run_e}').format(
43  identifier=identifier,
44  identifier_db=identifier.split(".xml")[0],
45  ex_b=ex_b,
46  ex_e=ex_e,
47  run_b=run_b,
48  run_e=run_e))
49 
50 here = os.getcwd()
51 data_base_file = here + "/localdb/database.txt"
52 if upload:
53  os.system("b2conditionsdb upload {TAGNAME} {DATABASEFILE}".format(TAGNAME=tag_name, DATABASEFILE=data_base_file))
54 if remove_db:
55  os.system("rm -r {}".format(data_base_file.split("database.txt")[0]))