Here's a possible implementation using C++ and CERN ROOT:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "TCanvas.h"
#include "TGraph.h"
using namespace std;
struct DataPoint {
unsigned long long timestamp;
double current;
unsigned long long count;
};
vector<DataPoint> parseInputFile(const string& filename) {
vector<DataPoint> data;
ifstream infile(filename);
string line;
while (getline(infile, line)) {
if (line.empty() || line[0] == '#') continue;
unsigned long long timestamp, count;
double current;
sscanf(line.c_str(), "%llu\t*lf\t*lf\t%llu", ×tamp, ¤t, &count);
if (data.empty() || timestamp < data.back().timestamp) {
// Restart the time axis when a new section begins
if (!data.empty()) data.back().timestamp = 0;
data.emplace_back();
}
data.back().timestamp = timestamp;
data.back().current = current;
data.back().count = count;
}
if (!data.empty()) data.back().timestamp = 0; // Ensure last section restarts time axis
return data;
}
void plotCountVsTime(const vector<DataPoint>& data) {
TCanvas canvas("count_vs_time", "Count vs Time");
vector<double> times, counts;
for (const auto& point : data) {
times.push_back(point.timestamp);
counts.push_back(point.count);
}
TGraph graph(times.size(), times.data(), counts.data());
graph.SetTitle("Count vs Time");
graph.GetXaxis()->SetTitle("Time [s]");
graph.GetYaxis()->SetTitle("Count");
graph.Draw("AL");
canvas.Draw();
}
void plotCurrentVsTime(const vector<DataPoint>& data) {
TCanvas canvas("current_vs_time", "Current vs Time");
vector<double> times, currents;
for (const auto& point : data) {
times.push_back(point.timestamp);
currents.push_back(point.current);
}
TGraph graph(times.size(), times.data(), currents.data());
graph.SetTitle("Current vs Time");
graph.GetXaxis()->SetTitle("Time [s]");
graph.GetYaxis()->SetTitle("Current [A]");
graph.Draw("AL");
canvas.Draw();
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " INPUT_FILE" << endl;
return 1;
}
const auto data = parseInputFile(argv[1]);
plotCountVsTime(data);
plotCurrentVsTime(data);
return 0;
}
The parseInputFile() function reads the input file line by line and extracts the timestamp, current, and count from each row using the sscanf() function. The function also detects when a new section of data begins (i.e., when a row with a hashtag is found) and restarts the time axis by setting the timestamp of the last data point of the previous section to zero.
The plotCountVsTime() and plotCurrentVsTime() functions take the parsed data points and create TGraph objects that can be used to plot the count and current vs time, respectively. The TCanvas class is used to display the plots. Note that the timestamps are assumed to be in seconds, but this can be adjusted if necessary.
To compile and run the code, you will need to have CERN ROOT installed and linked appropriately. Assuming the code is saved in a file named plot_data.cpp, you can compile it with the following command:
g++ -o plot_data plot_data.cpp `root-config --libs --cflags`
Then, to plot the data in a file named input.txt, you can run:
./plot_data input.txt
This will create two plots: one with the count vs time, and one with the current vs time. The time axis of each plot will restart at the beginning of each section of data indicated by a hashtag.