Dynamically create histograms

Hi ROOTers,
I am reading a data file, and some of the events are bad. I need to plot the bad ones in a TH1F histogram, and save them to a root file. Now, if I knew how many bads I will have, I would just create an array of histos like

TH1F *hist[10]; for (int i=0; i<11; i++) hist[i] = new TH1F(Form("blah[%d]",i),"blah",4096, 0, 4095);

But the problem is I don’t know how many bads I will have. Is there a way to create histograms on the fly, as soon I encounter a bad flag I do something similar to

if (bad)
TH1F *hist[EventNumber]=new TH1F(Form("blah[%d]",EventNumber),"blah",4096, 0, 4095) ;
for(int ii=0; ii<=256 ; i++) hist[EventNumber]->Fill(ii,eventdata[ii]);

Thanks!
Artur

OK I figured it. I use of TH1F’s.

why not to use ROOT classes like TList, TObjArray, etc?
root.cern.ch/root/htmldoc/TList.html

[quote]why not to use ROOT classes like TList, TObjArray, etc?
root.cern.ch/root/htmldoc/TList.html[/quote]

Because C++ programmer should prefer C++ standard library where possible. Of course, if someone wants to hold THs in a container (by value)and frequently remove some elements, vector is bad choice.

Well, yeah, the vector does what I need.
Thanks guys!

[quote]Well, yeah, the vector does what I need.
Thanks guys![/quote]

Yes, but if you have std::vector and want to remove some elements, it can be expensive - a lot of copying == possible dynamic allocation.

[quote=“tpochep”][quote]Well, yeah, the vector does what I need.
Thanks guys![/quote]

Yes, but if you have std::vector and want to remove some elements, it can be expensive - a lot of copying == possible dynamic allocation.[/quote]
Yes, one knows better :unamused: what he wants :wink:. He wants to remove something doesn’t he?
One knows better he wants std::list
However the ROOTForum eats :blush: the symbol “star”, that’s confusing. As result one reads “value” where the guy said “pointer” :bulb:

I am using a

and then

if (bad) badCounter++; for(int ii=0; ii<=256; ii++) BadEvent->Fill(ii,eventdata[ii]); BadHistos.push_back(BadEvent);
is that a bad idea to do it this way? I mean considering I don’t need to access, modify or remove any of the histos, except for writing them to the root file.

It is Ok.
However if you choose using the ROOT container you may have written the entire container rather each histogram separately in loop.
Another comment. I think you may avoid the counter. The container “counts” its own size anyway.
I hope the real code does

if (bad) { badCounter++; bla . . . . bla BadHistos.push_back(BadEvent); }

Hello, Valeri.

[quote=“fine”]
However the ROOTForum eats :blush: the symbol “star”, that’s confusing. As result one reads “value” where the guy said “pointer” :bulb:[/quote]

Yes, you are right, for example in my reply somewhere I wrote

static_cast<pointer_to_TGeoSphere>(vol->GetShape()) and the result in the forum was static_cast<TGeoSphere>(vol->GetShape()).

[quote=“fine”]
I hope the real code does

if (bad) { badCounter++; bla . . . . bla BadHistos.push_back(BadEvent); }[/quote]
Yeah, that’s what it does, sorry :blush: