Dear All,
I have enormous data sets from sensors that measure for instance current at a certain time interval. To plot the data, I load it in a TTree so that I can easily plot histograms.
I’ve recently learned that there is such a thing as TDatime, so I want to use that to have the time/date on my x-axis because that is relevant for me.
When I try to make the plots, I can’t figure our how to select a certain time range. The data I have is from multiple days, and I want to select for instance data between 2021-03-11 09h00 to 2021-03-11 12h00, but it does not look right. Meaning it looks like it is plotting all the data from each day for the time interval between 09h00 and 12h00, but I only want it from 2021-03-11.
In addition, the time is not correct on the x-axis I’ve searched online, and I found several examples but I’m afraid I don’t completely understand how it works. Therefore, I’ve put my code here below, and the data file that I use can be downloaded here: data. As you can see from the code, eventully I would like to have a function in which I can just give the date/time range as input, see line with void atlasData().
I hope you can tell me what I’m doing wrong
Best regards,
Nikkie
#include <iostream>
#include "TCanvas.h"
#include "TPad.h"
#include "TGraph.h"
#include "TMultiGraph.h"
#include "TList.h"
#include "TTree.h"
#include "TString.h"
#include "TDatime.h"
#include "TAxis.h"
#include "TStyle.h"
TTree* makeTree ()
{
TTree* myTree = new TTree();
myTree->ReadFile(Form("./lCCData_22032021/xQH.csv"), "", ',');
//Int_t Index = 0, Year = 0, Month = 0, Day = 0, Hour = 0, Minute = 0, Second = 0;
TDatime time;
char Date[40];
TBranch *bTime = myTree->Branch("time", &time);
myTree->SetBranchAddress("Date", &Date[0]);
for (Long64_t i=0; i<myTree->GetEntries(); i++)
//for (Long64_t i=0; i<200; i++)
{
myTree->GetEntry(i);
// Get the date and make it into a string, cut into pieces
std::string sDate(Date);
time.Set(std::stoi(sDate.substr(0,4)),
std::stoi(sDate.substr(5,2)),
std::stoi(sDate.substr(8,2)),
std::stoi(sDate.substr(11,2)),
std::stoi(sDate.substr(14,2)),
std::stoi(sDate.substr(17,2)));
bTime->Fill();
}
myTree->ResetBranchAddresses(); // Disconnect from local variables
myTree->Print();
return myTree;
}
void atlasData(const char* pSensor = "HW101", const int pYear = 2021, const int pMonth = 3, const int pDay = 11, const int pStartHour = 9, const int pStopHour = 11)
{
// Define the lowest histogram limit
TDatime T1(pYear, pMonth, pDay, pStartHour, 0, 0);
int X1 = T1.Convert(true);
// Define the highest histogram limit as 2003, March 7th
TDatime T2(pYear, pMonth, pDay, pStopHour, 0, 0);
int X2 = T2.Convert(true);
// Get all the graphs
TCanvas* c1 = new TCanvas();
TTree* myTree = makeTree();
myTree->Draw(Form("%s:time>>htemp(1000000,X1,X2)", pSensor));
TGraph* htemp = (TGraph*)gPad->GetPrimitive("Graph");
htemp->GetXaxis()->SetTimeDisplay(1);
htemp->GetXaxis()->SetTimeFormat("%H:%M:%S");
TCanvas* c3 = new TCanvas();
htemp->Draw();
// myTree->Draw(Form("%s:Second+60*Minute+3600*Hour", pSensor), Form("Month==%d&&Day==%d&&Hour>=%d&&Hour<%d", pMonth, pDay, pStartHour, pStopHour));
// TGraph* myHist101 = (TGraph*)gPad->GetPrimitive("Graph");
// TCanvas* c3 = new TCanvas();
// pSensor = "HW103";
// myTree->Draw(Form("%s:Second+60*Minute+3600*Hour", pSensor), Form("Month==%d&&Day==%d&&Hour>=%d&&Hour<%d", pMonth, pDay, pStartHour, pStopHour));
// TGraph* myHist103 = (TGraph*)gPad->GetPrimitive("Graph");
// TCanvas* c5 = new TCanvas();
// pSensor = "HW105";
// myTree->Draw(Form("%s:Second+60*Minute+3600*Hour", pSensor), Form("Month==%d&&Day==%d&&Hour>=%d&&Hour<%d", pMonth, pDay, pStartHour, pStopHour));
// TGraph* myHist105 = (TGraph*)gPad->GetPrimitive("Graph");
// TCanvas* c7 = new TCanvas();
// pSensor = "HW107";
// myTree->Draw(Form("%s:Second+60*Minute+3600*Hour", pSensor), Form("Month==%d&&Day==%d&&Hour>=%d&&Hour<%d", pMonth, pDay, pStartHour, pStopHour));
// TGraph* myHist107 = (TGraph*)gPad->GetPrimitive("Graph");
// TCanvas* c9 = new TCanvas();
// pSensor = "HW109";
// myTree->Draw(Form("%s:Second+60*Minute+3600*Hour", pSensor), Form("Month==%d&&Day==%d&&Hour>=%d&&Hour<%d", pMonth, pDay, pStartHour, pStopHour));
// TGraph* myHist109 = (TGraph*)gPad->GetPrimitive("Graph");
// // Make a nice multigraph
// TCanvas *cMult = new TCanvas();
// TMultiGraph *mg = new TMultiGraph();
// mg->SetTitle("Heater current;Time [s];Current [A]");
// mg->Add(myHist101, "p"); myHist101->SetTitle("HW101"); myHist101->SetMarkerStyle(52); myHist101->SetMarkerColor(801);
// mg->Add(myHist103, "p"); myHist103->SetTitle("HW103"); myHist103->SetMarkerStyle(52); myHist103->SetMarkerColor(809);
// mg->Add(myHist105, "p"); myHist105->SetTitle("HW105"); myHist105->SetMarkerStyle(52); myHist105->SetMarkerColor(632);
// mg->Add(myHist107, "p"); myHist107->SetTitle("HW107"); myHist107->SetMarkerStyle(52); myHist107->SetMarkerColor(861);
// mg->Add(myHist109, "p"); myHist109->SetTitle("HW109"); myHist109->SetMarkerStyle(52); myHist109->SetMarkerColor(863);
// mg->Draw("ap");
// cMult->BuildLegend(0.74, 0.64,0.89,0.89, "","p");
// cMult->SaveAs(Form("%s_2019-%d-%d-from_%dh-to_%dh.pdf", pMeasurement, pMonth, pDay, pStartH, pStopH));
}
_ROOT Version: 6.22/06
_Platform: Linux Manjaro
_Compiler:Root