Skip to content

Commit

Permalink
refactor: use stack in WriteToFile and ReadFromFile
Browse files Browse the repository at this point in the history
  • Loading branch information
furkan-bilgin committed Nov 29, 2024
1 parent 3f33a9d commit bc96727
Showing 1 changed file with 91 additions and 142 deletions.
233 changes: 91 additions & 142 deletions UserTools/MonitorLAPPDData/MonitorLAPPDData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ bool MonitorLAPPDData::Finalise()
}
delete graph_pps_accumulated_number_vs_psec_timestamp;
delete graph_pps_time_vs_accumulated_number;
delete graph_pps_event_counter;
delete graph_pps_interval_drift;

// multi-graphs
delete multi_ped_lappd;
Expand Down Expand Up @@ -1080,21 +1078,21 @@ void MonitorLAPPDData::WriteToFile()
root_option = "UPDATE";
TFile *f = new TFile(root_filename.str().c_str(), root_option.c_str());

std::vector<ULong64_t> *t_time = new std::vector<ULong64_t>;
std::vector<ULong64_t> *t_end = new std::vector<ULong64_t>;
std::vector<double> *t_pps_rate = new std::vector<double>;
std::vector<double> *t_frame_rate = new std::vector<double>;
std::vector<double> *t_beamgate_rate = new std::vector<double>;
std::vector<double> *t_int_charge = new std::vector<double>;
std::vector<double> *t_buffer_size = new std::vector<double>;
std::vector<int> *t_board_idx = new std::vector<int>;
std::vector<int> *t_chkey = new std::vector<int>;
std::vector<double> *t_rate = new std::vector<double>;
std::vector<double> *t_ped = new std::vector<double>;
std::vector<double> *t_sigma = new std::vector<double>;
std::vector<uint64_t> *t_raw_lappd_data_pps_counts = new std::vector<uint64_t>;
std::vector<uint64_t> *t_raw_lappd_data_pps_timestamps = new std::vector<uint64_t>;
std::vector<long> *t_data_event_timestamps = new std::vector<long>;
std::vector<ULong64_t> t_time;
std::vector<ULong64_t> t_end;
std::vector<double> t_pps_rate;
std::vector<double> t_frame_rate;
std::vector<double> t_beamgate_rate;
std::vector<double> t_int_charge;
std::vector<double> t_buffer_size;
std::vector<int> t_board_idx;
std::vector<int> t_chkey;
std::vector<double> t_rate;
std::vector<double> t_ped;
std::vector<double> t_sigma;
std::map<int, std::vector<uint64_t>> t_raw_lappd_data_pps_counts;
std::map<int, std::vector<uint64_t>> t_raw_lappd_data_pps_timestamps;
std::vector<long> t_data_event_timestamps;

int t_run, t_subrun, t_partrun;
int t_pps_count, t_frame_count;
Expand Down Expand Up @@ -1164,9 +1162,9 @@ void MonitorLAPPDData::WriteToFile()
for (int i_entry = 0; i_entry < n_entries; i_entry++)
{
t->GetEntry(i_entry);
if (t_board_idx->at(0) == -1)
if (t_board_idx.at(0) == -1)
continue;
if (t_end->at(0) == t_file_end.at(0))
if (t_end.at(0) == t_file_end.at(0))
{
Log("WARNING (MonitorLAPPDData): WriteToFile: Wanted to write data from file that is already written to DB. Omit entries", v_warning, verbosity);
omit_entries = true;
Expand All @@ -1178,21 +1176,6 @@ void MonitorLAPPDData::WriteToFile()
{
// don't write file again, but still delete TFile and TTree object!!!
f->Close();
delete t_time;
delete t_end;
delete t_pps_rate;
delete t_frame_rate;
delete t_beamgate_rate;
delete t_int_charge;
delete t_buffer_size;
delete t_board_idx;
delete t_chkey;
delete t_rate;
delete t_ped;
delete t_sigma;
delete t_raw_lappd_data_pps_counts;
delete t_raw_lappd_data_pps_timestamps;
delete t_data_event_timestamps;
delete f;

gROOT->cd();
Expand All @@ -1201,33 +1184,33 @@ void MonitorLAPPDData::WriteToFile()
}

// If we have vectors, they need to be cleared
t_time->clear();
t_end->clear();
t_pps_rate->clear();
t_frame_rate->clear();
t_beamgate_rate->clear();
t_int_charge->clear();
t_buffer_size->clear();
t_board_idx->clear();
t_chkey->clear();
t_rate->clear();
t_ped->clear();
t_sigma->clear();
t_raw_lappd_data_pps_counts->clear();
t_raw_lappd_data_pps_timestamps->clear();
t_data_event_timestamps->clear();
t_time.clear();
t_end.clear();
t_pps_rate.clear();
t_frame_rate.clear();
t_beamgate_rate.clear();
t_int_charge.clear();
t_buffer_size.clear();
t_board_idx.clear();
t_chkey.clear();
t_rate.clear();
t_ped.clear();
t_sigma.clear();
t_raw_lappd_data_pps_counts.clear();
t_raw_lappd_data_pps_timestamps.clear();
t_data_event_timestamps.clear();

// Get data that was processed
for (int i_current = 0; i_current < (int)current_pps_rate.size(); i_current++)
{
t_time->push_back(first_timestamp.at(i_current));
t_end->push_back(last_timestamp.at(i_current));
t_pps_rate->push_back(current_pps_rate.at(i_current));
t_frame_rate->push_back(current_frame_rate.at(i_current));
t_beamgate_rate->push_back(current_beamgate_rate.at(i_current));
t_int_charge->push_back(current_int_charge.at(i_current));
t_buffer_size->push_back(current_buffer_size.at(i_current));
t_board_idx->push_back(current_board_index.at(i_current));
t_time.push_back(first_timestamp.at(i_current));
t_end.push_back(last_timestamp.at(i_current));
t_pps_rate.push_back(current_pps_rate.at(i_current));
t_frame_rate.push_back(current_frame_rate.at(i_current));
t_beamgate_rate.push_back(current_beamgate_rate.at(i_current));
t_int_charge.push_back(current_int_charge.at(i_current));
t_buffer_size.push_back(current_buffer_size.at(i_current));
t_board_idx.push_back(current_board_index.at(i_current));

ULong64_t time = first_timestamp.at(i_current);
boost::posix_time::ptime starttime = *Epoch + boost::posix_time::time_duration(int(time / MSEC_to_SEC / SEC_to_MIN / MIN_to_HOUR), int(time / MSEC_to_SEC / SEC_to_MIN) % 60, int(time / MSEC_to_SEC / 1000.) % 60, time % 1000);
Expand All @@ -1241,15 +1224,13 @@ void MonitorLAPPDData::WriteToFile()
int lappd_id = it->first;
std::vector<uint64_t> current_pps_timestamps = it->second;
std::vector<int> current_pps_counts = raw_lappd_data_pps_counts.at(lappd_id);

t_raw_lappd_data_pps_timestamps.emplace(lappd_id, std::vector<uint64_t>());
t_raw_lappd_data_pps_counts.emplace(lappd_id, std::vector<uint64_t>());

// Because ROOT doesn't support serializing std::map, we need to pack it ourselves
// to a vector
for (int i_current = 0; i_current < current_pps_timestamps.size(); i_current++) {
t_raw_lappd_data_pps_timestamps->push_back(lappd_id);
t_raw_lappd_data_pps_counts->push_back(lappd_id);

t_raw_lappd_data_pps_timestamps->push_back(current_pps_timestamps.at(i_current));
t_raw_lappd_data_pps_counts->push_back(current_pps_counts.at(i_current));
t_raw_lappd_data_pps_counts.at(lappd_id).push_back(current_pps_counts.at(i_current));
t_raw_lappd_data_pps_timestamps.at(lappd_id).push_back(current_pps_timestamps.at(i_current));
}
}

Expand All @@ -1268,15 +1249,15 @@ void MonitorLAPPDData::WriteToFile()

// Push data event timestamps
for (int i_current = 0; i_current < (int)data_event_timestamps.size(); i_current++) {
t_data_event_timestamps->push_back(data_event_timestamps.at(i_current));
t_data_event_timestamps.push_back(data_event_timestamps.at(i_current));
}

for (int i_current = 0; i_current < (int)current_ped.size(); i_current++)
{
t_chkey->push_back(current_chkey.at(i_current));
t_rate->push_back(current_rate.at(i_current));
t_ped->push_back(current_ped.at(i_current));
t_sigma->push_back(current_sigma.at(i_current));
t_chkey.push_back(current_chkey.at(i_current));
t_rate.push_back(current_rate.at(i_current));
t_ped.push_back(current_ped.at(i_current));
t_sigma.push_back(current_sigma.at(i_current));
}

t_run = current_run;
Expand All @@ -1301,25 +1282,8 @@ void MonitorLAPPDData::WriteToFile()
if (verbosity > 3)
std::cout << "t_time" << std::endl;
if (verbosity > 3)
std::cout << "t_time: " << t_time << std::endl;
if (verbosity > 3)
std::cout << "t_time->size(): " << t_time->size() << std::endl;

// Delete potential vectors
delete t_time;
delete t_end;
delete t_pps_rate;
delete t_frame_rate;
delete t_beamgate_rate;
delete t_int_charge;
delete t_buffer_size;
delete t_board_idx;
delete t_chkey;
delete t_rate;
delete t_ped;
delete t_sigma;
delete t_raw_lappd_data_pps_counts;
delete t_raw_lappd_data_pps_timestamps;
std::cout << "t_time->size(): " << t_time.size() << std::endl;

delete f;

gROOT->cd();
Expand Down Expand Up @@ -1417,21 +1381,21 @@ void MonitorLAPPDData::ReadFromFile(ULong64_t timestamp, double time_frame)

Log("MonitorLAPPDData: Tree exists, start reading in data", v_message, verbosity);

std::vector<ULong64_t> *t_time = new std::vector<ULong64_t>;
std::vector<ULong64_t> *t_end = new std::vector<ULong64_t>;
std::vector<double> *t_pps_rate = new std::vector<double>;
std::vector<double> *t_frame_rate = new std::vector<double>;
std::vector<double> *t_beamgate_rate = new std::vector<double>;
std::vector<double> *t_int_charge = new std::vector<double>;
std::vector<double> *t_buffer_size = new std::vector<double>;
std::vector<int> *t_board_idx = new std::vector<int>;
std::vector<int> *t_chkey = new std::vector<int>;
std::vector<double> *t_rate = new std::vector<double>;
std::vector<double> *t_ped = new std::vector<double>;
std::vector<double> *t_sigma = new std::vector<double>;
std::vector<uint64_t> *t_raw_lappd_data_pps_counts = new std::vector<uint64_t>;
std::vector<uint64_t> *t_raw_lappd_data_pps_timestamps = new std::vector<uint64_t>;
std::vector<long> *t_data_event_timestamps = new std::vector<long>;
std::vector<ULong64_t> t_time;
std::vector<ULong64_t> t_end;
std::vector<double> t_pps_rate;
std::vector<double> t_frame_rate;
std::vector<double> t_beamgate_rate;
std::vector<double> t_int_charge;
std::vector<double> t_buffer_size;
std::vector<int> t_board_idx;
std::vector<int> t_chkey;
std::vector<double> t_rate;
std::vector<double> t_ped;
std::vector<double> t_sigma;
std::vector<uint64_t> t_raw_lappd_data_pps_counts;
std::vector<uint64_t> t_raw_lappd_data_pps_timestamps;
std::vector<long> t_data_event_timestamps;

int t_run, t_subrun, t_partrun;
int t_pps_count, t_frame_count;
Expand Down Expand Up @@ -1477,10 +1441,10 @@ void MonitorLAPPDData::ReadFromFile(ULong64_t timestamp, double time_frame)
for (int i_entry = 0; i_entry < nentries_tree; i_entry++)
{
t->GetEntry(i_entry);
if (t_time->at(i_board) >= timestamp_start && t_end->at(i_board) <= timestamp)
if (t_time.at(i_board) >= timestamp_start && t_end.at(i_board) <= timestamp)
{
vector_timestamps.push_back(t_time->at(i_board));
map_timestamp_entry.emplace(t_time->at(i_board), i_entry);
vector_timestamps.push_back(t_time.at(i_board));
map_timestamp_entry.emplace(t_time.at(i_board), i_entry);
}
}

Expand All @@ -1499,19 +1463,19 @@ void MonitorLAPPDData::ReadFromFile(ULong64_t timestamp, double time_frame)

t->GetEntry(next_entry);

// std::cout <<"i_board: "<<i_board<<", t_time->at(i_board): "<<t_time->at(i_board)<<std::endl;
// std::cout <<"i_board: "<<i_board<<", t_time.at(i_board): "<<t_time.at(i_board)<<std::endl;
// std::cout <<"timestamp_start: "<<timestamp_start<<", timestamp: "<<timestamp<<std::endl;
if (t_time->at(i_board) >= timestamp_start && t_end->at(i_board) <= timestamp && t_end->at(i_board) != 0)
if (t_time.at(i_board) >= timestamp_start && t_end.at(i_board) <= timestamp && t_end.at(i_board) != 0)
{
data_times_plot.at(board_nr).push_back(t_time->at(i_board));
data_times_end_plot.at(board_nr).push_back(t_end->at(i_board));
pps_rate_plot.at(board_nr).push_back(t_pps_rate->at(i_board));
frame_rate_plot.at(board_nr).push_back(t_frame_rate->at(i_board));
beamgate_rate_plot.at(board_nr).push_back(t_beamgate_rate->at(i_board));
int_charge_plot.at(board_nr).push_back(t_int_charge->at(i_board));
buffer_size_plot.at(board_nr).push_back(t_buffer_size->at(i_board));

boost::posix_time::ptime boost_tend = *Epoch + boost::posix_time::time_duration(int(t_end->at(i_board) / MSEC_to_SEC / SEC_to_MIN / MIN_to_HOUR), int(t_end->at(i_board) / MSEC_to_SEC / SEC_to_MIN) % 60, int(t_end->at(i_board) / MSEC_to_SEC / 1000.) % 60, t_end->at(i_board) % 1000);
data_times_plot.at(board_nr).push_back(t_time.at(i_board));
data_times_end_plot.at(board_nr).push_back(t_end.at(i_board));
pps_rate_plot.at(board_nr).push_back(t_pps_rate.at(i_board));
frame_rate_plot.at(board_nr).push_back(t_frame_rate.at(i_board));
beamgate_rate_plot.at(board_nr).push_back(t_beamgate_rate.at(i_board));
int_charge_plot.at(board_nr).push_back(t_int_charge.at(i_board));
buffer_size_plot.at(board_nr).push_back(t_buffer_size.at(i_board));

boost::posix_time::ptime boost_tend = *Epoch + boost::posix_time::time_duration(int(t_end.at(i_board) / MSEC_to_SEC / SEC_to_MIN / MIN_to_HOUR), int(t_end.at(i_board) / MSEC_to_SEC / SEC_to_MIN) % 60, int(t_end.at(i_board) / MSEC_to_SEC / 1000.) % 60, t_end.at(i_board) % 1000);
struct tm label_timestamp = boost::posix_time::to_tm(boost_tend);
//
TDatime datime_timestamp(1900 + label_timestamp.tm_year, label_timestamp.tm_mon + 1, label_timestamp.tm_mday, label_timestamp.tm_hour, label_timestamp.tm_min, label_timestamp.tm_sec);
Expand All @@ -1523,10 +1487,10 @@ void MonitorLAPPDData::ReadFromFile(ULong64_t timestamp, double time_frame)
for (int i = 0; i < 30; i++)
{
int chankey = min_board + i;
ped_plot.at(chankey).push_back(t_ped->at(chankey));
sigma_plot.at(chankey).push_back(t_sigma->at(chankey));
// std::cout <<"chankey "<<chankey<<", sigma: "<<t_sigma->at(chankey)<<std::endl;
rate_plot.at(chankey).push_back(t_rate->at(chankey));
ped_plot.at(chankey).push_back(t_ped.at(chankey));
sigma_plot.at(chankey).push_back(t_sigma.at(chankey));
// std::cout <<"chankey "<<chankey<<", sigma: "<<t_sigma.at(chankey)<<std::endl;
rate_plot.at(chankey).push_back(t_rate.at(chankey));
}
if (i_board == 0)
{ // No need to have separate run information for different boards
Expand All @@ -1545,43 +1509,28 @@ void MonitorLAPPDData::ReadFromFile(ULong64_t timestamp, double time_frame)
t->GetEntry(i_entry);

// Unpack vector to map
for (int i = 0; i < t_raw_lappd_data_pps_timestamps->size(); i += 2) {
for (int i = 0; i < t_raw_lappd_data_pps_timestamps.size(); i += 2) {
// This could further be broken down to use a singular vector
// but I'd say the current solution is already hacky enough
int lappd_id = t_raw_lappd_data_pps_timestamps->at(i);
uint64_t pps_timestamp = t_raw_lappd_data_pps_timestamps->at(i + 1);
uint64_t pps_count = t_raw_lappd_data_pps_counts->at(i + 1);
int lappd_id = t_raw_lappd_data_pps_timestamps.at(i);
uint64_t pps_timestamp = t_raw_lappd_data_pps_timestamps.at(i + 1);
uint64_t pps_count = t_raw_lappd_data_pps_counts.at(i + 1);

raw_lappd_data_pps_timestamps[lappd_id].push_back(pps_timestamp);
raw_lappd_data_pps_counts[lappd_id].push_back(pps_count);
}

// Initialise vector of timestamps per partrun
data_event_timestamps_per_partrun.emplace(t_partrun, std::vector<uint64_t>());
for (int i = 0; i < t_data_event_timestamps->size(); i++) {
data_event_timestamps_per_partrun.at(t_partrun).push_back(t_data_event_timestamps->at(i));
for (int i = 0; i < t_data_event_timestamps.size(); i++) {
data_event_timestamps_per_partrun.at(t_partrun).push_back(t_data_event_timestamps.at(i));
}

accumulated_pps_count += t_pps_count;
pps_accumulated_psec_timestamp.push_back(t_pps_accumulated_psec_timestamp);
pps_accumulated_number.push_back(accumulated_pps_count);
raw_lappd_data_pps_timestamp_per_accumulated_number.push_back(t_raw_lappd_pps_timestamp);
}
// Delete vectors, if we have any
delete t_time;
delete t_end;
delete t_pps_rate;
delete t_frame_rate;
delete t_beamgate_rate;
delete t_int_charge;
delete t_buffer_size;
delete t_board_idx;
delete t_chkey;
delete t_rate;
delete t_ped;
delete t_sigma;
delete t_raw_lappd_data_pps_counts;
delete t_raw_lappd_data_pps_timestamps;
}

f->Close();
Expand Down

0 comments on commit bc96727

Please sign in to comment.