Saving Histograms to File if they exist and are not NULL

Hi forum,

I have a program loading 4 initial distinct TH2F Histograms, doing some work on them and then - if the work succeeded - writing them to an output TFile - each as TH1F Histograms.

The problem is, even Histograms on which the editing fails are written into the TFile. However, they don’t contain their data, but the dataset of the previous of the Histogram.

Ok, so to break this up I have the following class in a extra header file.

TH1* class (TH2* Hist)
//doing some work
if(error) { cout << "error" << endl; return NULL}
return TH1* NewHist;

In my main program this class is then called 4 times to get the corresponding TH1* NewHist for my TH2 *OldHist. Additionally, in case of a non-existing or NULL Histogram the program won’t write the Histogram to the output TFile.

int main (int argc, char** argv) {
//calling the function 4 times to create Histograms
	NewHist1 = NULL;
	NewHist1 = class(OldHist1);
	NewHist2 = NULL;
	NewHist2 = class(OldHist2);
// Opening output File
	fout = new TFile("path","recreate");	
// Check if Histograms are created and only then write them
	if (NewHist1 && NewHist1 != NULL) {
	if (NewHist2 && NewHist2 != NULL) {
// etc.		
	return 0;

running the program works for “healthy” Histograms without disturbance. For Histograms where the class has an error, the error code is printed via cout - which works also fine. However these faulty Histograms will still be written on the file, even tough the class should return NULL and the main function checks for this NULL Histogram before writing it.

I hope I made everything clear and look forward to any constructive help.
I think the problem is somehow linked to the pointers still pointing on the previous Histogram in case of a NULL return, but I am unsure how to correct this.

Also note that I have not written the main file nor the class file. I am just using a preexisting version from my supervisor slightly altered to fulfill my needs.

Thanks in Advance

I guess:

if (NewHist1 && NewHist1 != NULL) { ... }

is equivalent to:

if (NewHist1) { ... }

Histograms are usually automatically written to files, so make sure to call TH1::AddDirectory(false) before creating your historgrams, and make sure that the pointers you are checking are really null. Cheers,

I tested adding TH1::AddDirectory(false) in my main. It did not prevent the Histograms to be written. Now, I have to test if every NULL pointer is indeed NULL.

You do not write what you mean by “the class has an error”.
You do not show how you initialize “OldHist1” and “OldHist2” and “NewHist” (maybe these pointers are rubbish).

Well, “class” is a reserved keyword in C++ so, you should not use it for an identifier.
There is the “return NULL” without a “;”.
And “return TH1* NewHist;” looks suspicious to me.

Thank you for your answer. Basically, I shortened many stuff out, because the whole program is much more complicated.

The “class has an error” part is not meant as a (fatal) root error. It means that the algorithm of the class, which works with Projections of the TH2, provides rubbish data.

The class is also not named “class” - sorry I used these names to make it easily understandable.
Ok, the old Histograms are initialized in the main function by

TFile *f1 = NULL;
f1 = new TFile("Histograms.root");
OldHist1 = (TH2F*)f1->Get("he1");
OldHist2 = (TH2F*)f1->Get("he2");
// etc.

The NewHist is initialized globally before the main function.

TH1F *NewHist = NULL;

In the class they are then used like this. Note I also had an mistake in the original post regarding the return command.

NewHist = (TH1F*)Hist->GetNewHist(); 
//Hist is the name of the parameter Histogram (see above)
//here GetNewHist represents my work on the Histogram mentioned above.
//The GetNewHist() may give an error variable used in the if(error) above with return NULL;
return (TH1F*)NewHist;} //otherwise NewHist is returned

Hope that clears things up.
As mentioned before the creation of Histograms works for correct Histograms.

By putting more “if-cout” in my code I could retrace that the creation of the NewHist1-4 as NULL works, but the class never returns NULL. But I know for sure that the class enters the if(error) because I get a cout about this.

I also tried to reset the NewHist before each usage of the class for the NewHist1-4 like this

NewHist1 = NULL;
NewHist = NULL;
NewHist1 = class(OldHist1);

NewHist2 = NULL;
NewHist = NULL;
NewHist2 = class(OldHist2);