Vector(stl) of TH1F* objects

Hello!

This is my first post. I don’t know much about C++.

Well, I have a problem with vectors of TH1F* objects. I would like to know if it’s possible and how to store in a vector a number of histograms, each one with a different identificator. For example,

vector a;

for(int i=0;i<100;i++){

TH1F *a= new TH1F(...);
 v.push_back(a);

}

But, in this case all the vectors hace the same identificator. Also, in this case I have memory leaks.

Also, is another possibility to make that thing? Tlist, TObjArray? I’m trying with TObjArray but I’m having problems.

I would like to remark that I want to use vector of histograms because the number of histograms I’d like to store is not fixed in all the program, so I can’t use “normal” arrays. Cint can’t do this.

Thank you very much.

Bye!

You can create an array of histograms in many ways

1-using a dynamic array of C++ pointers

TH1F** array = new TH1F*[nhist]; for (int i=0;i<nhist;i++) { array[i] = new TH1F(Form("h%d",i),"test",100,-3,3); }
2-using a ROOT collection

TList *list = new TList; for (int i=0;i<nhist>Add(h); }
3-using a vector of pointers to histograms

Rene

std::vector<TH1F> vlist; for (int i=0;i<nhist;i++) { TH1F *h = new TH1F(Form("h%d",i),"test",100,-3,3); vlist.push_back(h); }

Hello,

I’m attempting to use the STL vector option, but it doesn’t seem to be working. A sample piece of code below should demonstrate the problem.

# include <TApplication.h>
# include <TCanvas.h>
# include <TH1D.h>

# include <chrono>
# include <thread>
# include <vector>

int   Argc = 1;
char* Argv = "app";
TApplication app ("app",&Argc,&Argv);

TCanvas canvas;

int main ( int argc, char* argv[] )
{
	std::vector <TH1D*> hvec;
	TH1D* h = new TH1D("h","h",10,0,10);
	hvec.push_back(h);
	hvec.at(0)->Fill(4);
	hvec.at(0)->Draw();
	canvas.Update();
	canvas.Draw();
	
	std::this_thread::sleep_for( std::chrono::seconds(2) );
	
	return 0;
}

The application compiles fine, but then crashes immediately on running.

I’m compiling with VC++ on a Windows 7.

I’d glady provide more detail if you think that it would prove helpful.

1 Like

Hi,

on lxplus (SLC 6.6) with ROOT v5.34.30 and gcc v4.8 your code compiles & runs just fine.

Okay; this example was motivated by my trying to port an application from Unix to Windows. It worked fine on Unix, compiling with g++, but now I’m trying to figure out what’s wrong with it. This turned out to be one of the issues.

Try to move two lines:
TApplication app (“app”,&Argc,&Argv);
TCanvas canvas;
into your “main” (i.e. these should be the first two lines in your “main”).

No luck, but thank you.

Hi,

On Windows, you have to make sure you compile your code the same way than ROOT has been built, with the exact same version of Visual Studio. For example, this code:

# include <TApplication.h>
# include <TCanvas.h>
# include <TH1D.h>

# include <chrono>
# include <thread>
# include <vector>

int main ( int argc, char* argv[] )
{
   TApplication theApp("app", &argc, argv);
   TCanvas canvas;

   std::vector <TH1D*> hvec;
   TH1D* h = new TH1D("h","h",10,0,10);
   hvec.push_back(h);
   hvec.at(0)->Fill(4);
   hvec.at(0)->Draw();
   canvas.Update();
   canvas.Draw();
   
   std::this_thread::sleep_for( std::chrono::seconds(2) );

   theApp.Run();

   return 0;
}

built with:

cl -nologo -DWIN32 -W3 -D_WINDOWS -Z7 -MDd -GR -EHsc main.cxx -I %ROOTSYS%\include -FIw32pragma.h /link -debug -LIBPATH:%ROOTSYS%\lib libCore.lib libRIO.lib libHist.lib libGpad.lib
And the head of ROOT v5-34-00-patches, with Visual Studio 2012, works just fine.

Cheers, Bertrand.

That did it! I’ll see if I can apply this to my larger issue. Thank you very much!