Skip to content

Commit e9a55ff

Browse files
committed
Add option to copy DB file
1 parent b935ad5 commit e9a55ff

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

include/diagnostics/stage1.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class Stage1ParallelEventVisitor:
6262

6363
static calin::ix::diagnostics::stage1::Stage1Config default_config();
6464

65+
static std::string nectarcam_ancillary_database_filename(const std::string run_filename, uint64_t run_start_time_ns=0,
66+
const std::string forced_filename = "", const std::string forced_directory = "");
67+
6568
private:
6669
calin::ix::diagnostics::stage1::Stage1Config config_;
6770

proto/diagnostics/stage1.proto

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ message Stage1Config {
101101
(CFO).desc = "Configuration of clock regression calculator." ];
102102

103103
string ancillary_database = 200 [
104-
(CFO).desc = "Location of ancillary database if used." ];
104+
(CFO).desc = "Full path to ancillary database including filename if used." ];
105+
string ancillary_database_directory = 201 [
106+
(CFO).desc = "Directory containing ancillary database if used. Overriden by \"ancillary_database\" "
107+
"if this is given. An empty value defaults to directory housing the events data file." ];
105108
};
106109

107110
message NectarCamSpecificStage1 {

proto/scripts/cta_stage1.proto

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ message CommandLineOptions {
6262
int32 nthread = 21 [
6363
(CFO).desc = "Number of computation threads. A value of -1 disables "
6464
"threading completely." ];
65+
string copy_ancillary_db = 22 [
66+
(CFO).desc = "Specify directory to which the ancillary database should be copied "
67+
"before processing the data file. If left blank no copy is made, the ancillary "
68+
"database will be read from its original place. This option is useful "
69+
"when processing data on NSF filesystems as SQLITE3 perfromance is bad "
70+
"on these systems. Autotomatically sents the \"stage1.ancillary_database_directory\" "
71+
"option." ];
6572

6673
calin.ix.iact_data.cta_data_source.CTACameraEventDecoderConfig decoder = 30 [
6774
(CFO).desc = "CTA event decoder options." ];

scripts/cta_stage1.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import traceback
2222
import numpy
2323
import time
24+
import shutil
25+
import os
2426

2527
import calin.ix.io.zmq_data_source
2628
import calin.iact_data.event_dispatcher
@@ -90,7 +92,10 @@
9092
dispatcher = calin.iact_data.event_dispatcher.ParallelEventDispatcher()
9193

9294
# Create the stage1 visitor
93-
s1pev = calin.diagnostics.stage1.Stage1ParallelEventVisitor(opt.stage1())
95+
s1cfg = opt.const_stage1().Clone()
96+
if(opt.const_stage1().ancillary_database_directory() == ""):
97+
s1cfg.set_ancillary_database_directory(opt.copy_ancillary_db())
98+
s1pev = calin.diagnostics.stage1.Stage1ParallelEventVisitor(s1cfg)
9499
dispatcher.add_visitor(s1pev)
95100

96101
# Open SQL file
@@ -107,6 +112,7 @@
107112
dispatcher.process_cta_zmq_run(endpoints, cfg)
108113
sql.insert(opt.db_stage1_table_name(), visitor.stage1_results())
109114
else:
115+
copied_ancillary_db = ''
110116
first_file = True
111117
failed_files = []
112118
nsuccess = 0;
@@ -129,6 +135,25 @@
129135
print(f"Deleting old stage1 results from database, OID : {oid}")
130136
sql.delete_by_oid(opt.db_stage1_table_name(), oid)
131137
print("#%d / %d: processing %s"%(ifile+1,nfile,filename))
138+
if(opt.copy_ancillary_db() != ''):
139+
src_ancillary_db = calin.diagnostics.stage1.Stage1ParallelEventVisitor.nectarcam_ancillary_database_filename(filename,0,
140+
opt.const_stage1().ancillary_database(), opt.const_stage1().ancillary_database_directory())
141+
dst_ancillary_db = calin.diagnostics.stage1.Stage1ParallelEventVisitor.nectarcam_ancillary_database_filename(filename,0,
142+
s1cfg.ancillary_database(), s1cfg.ancillary_database_directory())
143+
if(copied_ancillary_db != '' and copied_ancillary_db != dst_ancillary_db):
144+
print("Deleting %s"%(copied_ancillary_db))
145+
os.unlink(copied_ancillary_db)
146+
copied_ancillary_db = ''
147+
if(os.path.isfile(src_ancillary_db)):
148+
try:
149+
if(not os.path.isfile(dst_ancillary_db)):
150+
print("Copying %s -> %s"%(src_ancillary_db,dst_ancillary_db))
151+
shutil.copyfile(src_ancillary_db, dst_ancillary_db)
152+
copied_ancillary_db = dst_ancillary_db
153+
except Exception as x:
154+
traceback.print_exception(*sys.exc_info())
155+
else:
156+
print("*** Could not copy ancillary DB %s ***"%(src_ancillary_db))
132157
try:
133158
calin.util.log.prune_default_protobuf_log()
134159
calin.provenance.chronicle.prune_the_chronicle()
@@ -148,6 +173,11 @@
148173
failed_files.append(filename)
149174
pass
150175

176+
if(copied_ancillary_db != ''):
177+
print("Deleting %s"%(copied_ancillary_db))
178+
os.unlink(copied_ancillary_db)
179+
copied_ancillary_db = ''
180+
151181
print("")
152182
print("="*80)
153183
if(nsuccess > 0):

src/diagnostics/stage1.cpp

+32-19
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,16 @@ bool Stage1ParallelEventVisitor::leave_telescope_run(
200200
start_time = std::max(start_time-30, int64_t(0));
201201
end_time = end_time+30;
202202

203-
std::string db_filename = config_.ancillary_database();
203+
std::string db_filename;
204204

205205
switch(run_config_->camera_layout().camera_type()) {
206206
case calin::ix::iact_data::instrument_layout::CameraLayout::NECTARCAM:
207207
case calin::ix::iact_data::instrument_layout::CameraLayout::NECTARCAM_TESTBENCH_19CHANNEL:
208208
case calin::ix::iact_data::instrument_layout::CameraLayout::NECTARCAM_TESTBENCH_61CHANNEL:
209-
if(db_filename.empty()) {
210-
db_filename = calin::util::file::dirname(run_config_->filename());
211-
std::string utdate = calin::util::file::basename(db_filename);
212-
db_filename += "/nectarcam_monitoring_db_";
213-
if(utdate.size() == 8) {
214-
db_filename += utdate.substr(0,4);
215-
db_filename += "-";
216-
db_filename += utdate.substr(4,2);
217-
db_filename += "-";
218-
db_filename += utdate.substr(6,2);
219-
} else {
220-
db_filename += calin::util::timestamp::
221-
Timestamp(run_config_->run_start_time().time_ns()).as_string().substr(0,10);
222-
}
223-
db_filename += ".sqlite";
224-
}
225-
if(calin::util::file::is_file(db_filename))
226-
{
209+
db_filename = nectarcam_ancillary_database_filename(run_config_->filename(),
210+
run_config_->run_start_time().time_ns(), config_.ancillary_database(),
211+
config_.ancillary_database_directory());
212+
if(calin::util::file::is_file(db_filename)) {
227213
nectarcam_ancillary_data_ =
228214
calin::iact_data::nectarcam_ancillary_data::
229215
retrieve_nectarcam_ancillary_data(db_filename, run_config_->telescope_id(),
@@ -242,6 +228,33 @@ bool Stage1ParallelEventVisitor::leave_telescope_run(
242228
return good;
243229
}
244230

231+
std::string Stage1ParallelEventVisitor::
232+
nectarcam_ancillary_database_filename(const std::string run_filename, uint64_t run_start_time_ns,
233+
const std::string forced_filename, const std::string forced_directory)
234+
{
235+
std::string db_filename = forced_filename;
236+
if(db_filename.empty()) {
237+
db_filename = forced_directory;
238+
if(db_filename.empty()) {
239+
db_filename = calin::util::file::dirname(run_filename);
240+
}
241+
std::string utdate = calin::util::file::basename(calin::util::file::dirname(run_filename));
242+
db_filename += "/nectarcam_monitoring_db_";
243+
if(utdate.size() == 8) {
244+
db_filename += utdate.substr(0,4);
245+
db_filename += "-";
246+
db_filename += utdate.substr(4,2);
247+
db_filename += "-";
248+
db_filename += utdate.substr(6,2);
249+
} else {
250+
db_filename += calin::util::timestamp::
251+
Timestamp(run_start_time_ns).as_string().substr(0,10);
252+
}
253+
db_filename += ".sqlite";
254+
}
255+
return db_filename;
256+
}
257+
245258
calin::ix::diagnostics::stage1::Stage1* Stage1ParallelEventVisitor::stage1_results(
246259
calin::ix::diagnostics::stage1::Stage1* stage1) const
247260
{

0 commit comments

Comments
 (0)