Three Column TextFile TH2D

Hi there,
I’ve got a txtfile that holds 3 columns of data. Column1 would be the x-axis (date), column2 is y-axis(sensor #) and column3 is the temp. I would like to make a 2D plot where the Temperature readings are plotted with respect to the sensor number and date. I’ve attached my textfile for reference.

#include <iostream>
#include <fstream>
#include <string>
#include <list>

void sensortempvsdate()
{

	string x;
	int y;
	double z;


	TCanvas *c = new TCanvas();


	//xbin, xmin, xmax, ybin, ymin, ymax
	TH2D *hist = new TH2D("h1", "Temperature Readings", 100, '2022-04-28', '2022-06-30', 100, 30, 61);


	hist->GetXaxis()->SetTitle("Date");
	hist->GetXaxis()->CenterTitle();
	
	hist->GetYaxis()->SetTitle("Temperature Sensor");
	hist->GetYaxis()->CenterTitle();

	fstream file;
	file.open("/project/avgtemp.txt", ios::in);

	while(1)
	{
		file >> x >> y >> z;

		hist->Fill(x, y, z);
		cout << "x is " << x << endl;
		cout << "y is " << y << endl;
		cout << "z is " << z << endl;

		if(file.eof()) break;

	}

	file.close();

	hist->SetStats(0);

	hist->Draw("COLZ");
	//c->Print("Example.png");

}

When I run my script on root, I just get a blank canvas with not even the axis, labels, etc…

Error: Can’t call TH2D::Fill(x,y,z) in current scope sensortempvsdate.C:51:
Possible candidates are…
protected: virtual Int_t TH2::Fill(Double_t); //MayNotUse
protected: virtual Int_t TH2::Fill(const char*,Double_t); //MayNotUse
public: virtual Int_t TH2::Fill(Double_t x,Double_t y);
public: virtual Int_t TH2::Fill(Double_t x,Double_t y,Double_t w);
public: virtual Int_t TH2::Fill(Double_t x,const char* namey,Double_t w);
public: virtual Int_t TH2::Fill(const char* namex,Double_t y,Double_t w);
public: virtual Int_t TH2::Fill(const char* namex,const char* namey,Double_t w);
public: virtual Int_t TH1::Fill(Double_t x);
public: virtual Int_t TH1::Fill(Double_t x,Double_t w);
public: virtual Int_t TH1::Fill(const char* name,Double_t w);
*** Interpreter error recovered ***

But I know that the variable type are not wrong b/c it prints them correctly.

cout << "x is " << x << endl;
cout << "y is " << y << endl;
cout << "z is " << z << endl;

avgtemp.txt (76.5 KB)

If x is a string, then you should do

		hist->Fill(x.c_str(), y, z);

But also, here:

TH2D *hist = new TH2D("h1", "Temperature Readings", 100, '2022-04-28', '2022-06-30', 100, 30, 61);

the ‘2022-04-28’ and ‘2022-06-30’ should not work. For time on axis, check here (or also search this forum for examples on time axis):
https://root.cern/doc/master/timeonaxis2_8C.html

1 Like