Selecting time when drawing graph from a TTree

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 :slight_smile: 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 :slight_smile:
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


Hi @NikDee ,
I think @moneta or @Axel have experience working with time series in ROOT, let’s ping them.

Cheers,
Enrico

This is actually for @couet who knows best all the intricacies of plotting with TDatime. Olivier, could you help, please?

I see you are storing the date as a string. I would stored it as an integer computed by TDatime. That way you will be able to do some selection on date in the myTree->Draw() line in atlasData .

Thanks for the referrals everyone :slight_smile:
and Thanks for the tip @couet . I’ll try it and let you know how it goes.

Cheers,
Nikkie

Sorry @couet I don’t understand.

I read the string because that is the only way I know how to use the TTree->ReadFile() easily. But then I convert this string into 6 integers with stoi() which I set in the TDatime object.

I end up with two branches for the date, one as a string (first branch), and one as a TDatime object (last branch). Then I was hoping to use the TDatime object to make the selection on.

******************************************************************************
*Tree    :          :                                                        *
*Entries :     9098 : Total =          920675 bytes  File  Size =          0 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :Date      : Date/C                                                 *
*Entries :     9098 : Total  Size=     256693 bytes  All baskets in memory   *
*Baskets :        9 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :HW101     : HW101/D                                                *
*Entries :     9098 : Total  Size=      73717 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :HW103     : HW103/D                                                *
*Entries :     9098 : Total  Size=      73717 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    3 :HW105     : HW105/D                                                *
*Entries :     9098 : Total  Size=      73717 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    4 :HW107     : HW107/D                                                *
*Entries :     9098 : Total  Size=      73717 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    5 :HW109     : HW109/D                                                *
*Entries :     9098 : Total  Size=      73717 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    6 :HWCpHWCm  : HWCpHWCm/D                                             *
*Entries :     9098 : Total  Size=      73747 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    7 :MSSAQH    : MSSAQH/D                                               *
*Entries :     9098 : Total  Size=      73727 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    8 :MSSBQH    : MSSBQH/D                                               *
*Entries :     9098 : Total  Size=      73727 bytes  All baskets in memory   *
*Baskets :        2 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    9 :time      : TDatime                                                *
*Entries :     9098 : Total  Size=      73861 bytes  All baskets in memory   *
*Baskets :        3 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*

This is the head of my data file:

Date/C:HW101/D:HW103/D:HW105/D:HW107/D:HW109/D:HWCpHWCm/D:MSSAQH/D:MSSBQH/D 2021/02/03 15:43:19.000,-0.0063999998383224,0.0024999999441206,0.0013999999500811,0.00069999997504056,0.00049999996554106,0,0,0 2021/02/03 15:43:20.000,-0.0063999998383224,0.0024999999441206,0.0013999999500811,0.00069999997504056,0.00049999996554106,0,0,0 2021/02/03 15:43:26.000,-0.0063999998383224,0.0024999999441206,0.0013999999500811,0.00069999997504056,0.00049999996554106,0,0,0 2021/02/03 15:46:16.000,-0.0084999995306134,0.003000000026077,0.00099999993108213,0.00059999997029081,0.00069999997504056,0,0,0 2021/02/03 15:56:16.000,-0.0050999997183681,0.0024999999441206,0.00049999996554106,0.00069999997504056,0.00059999997029081,0,0,0 2021/02/03 16:06:16.000,-0.0087999999523163,0.0026999998372048,0.00049999996554106,0.00049999996554106,0.00049999996554106,0,0,0 2021/02/03 16:16:16.000,-0.0093999998643994,0.0020999999251217,0.00069999997504056,0.00069999997504056,0.00059999997029081,0.0011999999405816,0,0 2021/02/03 16:26:16.000,-0.0087000001221895,0.0024999999441206,0.00069999997504056,0.00049999996554106,0.0002999999851454,0,0,0 2021/02/03 16:35:49.000,-0.0083999997004867,0.0023000000510365,0.00059999997029081,0.00059999997029081,0.00039999998989515,0,0,0

Cheers,
Nikkie

What I meat is not to store TDatime but the integer you get via convert:

root [0] auto T = new TDatime(2021,2,3,15,43,20)
root [1] T->Convert()
(unsigned int) 1612363400

Like in this example . Then you have an int and you can easily make selection on it. To ease the process you would need a function like atlasData to make the plot.

1 Like

Thank you, that worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.