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.
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.
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”).
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.