Re: [ROOT] Drawing multiple times to same temp histogram

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Mar 28 2002 - 17:51:21 MET


Hi Vuko,

Ok, my second guess was correct.

The problem is that you save a pointer to srhist, but the corresponding
histogram with name "srhist" is deleted at the next call to t1->Draw.
By changing the name in Clone immediatly after t1->Draw, you bypass the problem.

Rene Brun

Vuko Brigljevic wrote:
> 
> Hi Rene,
> 
> Rene Brun wrote:
> 
> > It is difficult to come with a diagnostic with only 4 lines of your code.
> 
> You're most welcome to look at the whole of my script in my afs
> public directory at CERN:
> 
> /afs/cern.ch/user/v/vuko/public/root/makePlots.C
> 
> You should be allowed to run it from there. If you want
> to run it from somewehre else, you need also to copy
> the file rootfilelist from the same directory. Anwyay,
> read on before doing so since it looks like you've solved it.
> 
> > So far, I have assumed that somewhere in your code, you have a
> > declaration like
> >   TH2F *srhist = new TH2F("srhist",...)
> 
> No I didn't have a declaration like that, the reason being that
> I didn't want to declare the histogram boundaries a priori,
> because I want to leave them open if at all possible.
> 
> > If this is not the case, you should add the following statement after
> >    t1->Draw
> >    TH2F *srhist = (Th2F*)gDirectory->GetList()->FindObject("srhist");
> 
> I did and now it works :) !
> 
> 2 questions:
> - What does that line do? It was working without it if I am drawing to
> srhist
>   only once.
> - Did I understand correctly that when you write twice to the same
> histogram,
>   that you did NOT define, i.e.:
> 
>  t1->Draw("var1 >> outhist");
>  t1->Draw("var2 >> outhist");
> 
>  the first histogram is being deleted? Could I repeat that an infinite
> number of times
>  without problem?
> 
> Thanks a lot,
> 
> Vuko
> 
> > If this does not work, please send me something that I can test.
> >
> > Rene Brun
> >
> > On Wed, 27 Mar 2002, Vuko Brigljevic wrote:
> >
> > > Rene,
> > >
> > > Rene Brun wrote:
> > >
> > > > I suspect that your program crashes when computing the first argument of
> > > > TTree::Draw. Could you do;
> > > >   TString expression = scalerName(ilay+1)+":time >> srhist";
> > > >   t1->draw(expression,cutString);
> > > >
> > > > Check the definition of your variable scalerName. It seems that you have an
> > > > access out of range in the last iteration.
> > > >
> > >
> > > I did what you suggest, but it still fails. I am printing out the
> > > TString
> > > which I am now passing as first argument to Draw() and it looks
> > > perfectly ok
> > > as far as I can tell. Actually the printout rather suggests that I am
> > > crashing in the clone() command. I append again the code and output.
> > >
> > > Vuko
> > >
> > > code:
> > >   TH2F *srhistos[10];
> > >
> > >   int ilay;
> > >   TString hname = "rhist_";
> > >   for (ilay=0; ilay<4; ilay++) {
> > >     TString expression=scalerName(ilay+1)+":time >> srhist";
> > >     cout << "Drawing layer : " << ilay
> > >        << "\t expression: " << expression  << endl;
> > >     t1->Draw(expression,cutString);
> > >     cout << "Cloninng layer : " << ilay << endl;
> > >     srhistos[ilay]= (TH2F*)srhist->Clone(hname+ilay);
> > >     cout << "Done Cloninng layer : " << ilay << endl;
> > >   }
> > >
> > > output:
> > > ...
> > > Drawing layer : 0        expression:
> > > scaler1_0+scaler1_1+scaler1_2+scaler1_3:time >> srhist
> > > <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
> > > Cloninng layer : 0
> > > Done Cloninng layer : 0
> > > Drawing layer : 1        expression:
> > > scaler1_4+scaler1_1+scaler1_6+scaler1_7:time >> srhist
> > > Cloninng layer : 1
> > > Done Cloninng layer : 1
> > > Drawing layer : 2        expression:
> > > scaler1_8+scaler1_9+scaler1_10+scaler1_11:time >> srhist
> > > Cloninng layer : 2
> > >
> > >  *** Break *** segmentation violation
> > > Root > Function makePlots() busy flag cleared
> > > Function doPlots() busy flag cleared
> > >
> > >
> > >
> > >
> > >
> > > > Vuko Brigljevic wrote:
> > > > >
> > > > > Hi Rene,
> > > > >
> > > > > I think I now understand what you meant. However, the
> > > > > code with your modification still crashes, though
> > > > > only in the last iteration. What I run (your suggestion
> > > > > and some printout):
> > > > >
> > > > >   TH2F *srhistos[10];
> > > > >    int ilay;
> > > > >    TString hname = "rhist_";
> > > > >    for (ilay=0; ilay<4; ilay++) {
> > > > >      cout << "Drawing layer : " << ilay << endl;
> > > > >      t1->Draw(scalerName(ilay+1)+":time >> srhist",cutString);
> > > > >      cout << "Cloninng layer : " << ilay << endl;
> > > > >      srhistos[ilay]= (TH2F*)srhist->Clone(hname+ilay);
> > > > >      cout << "Done Cloninng layer : " << ilay << endl;
> > > > >    }
> > > > >
> > > > > The output:
> > > > >
> > > > > Drawing layer : 0
> > > > > Cloninng layer : 0
> > > > > Done Cloninng layer : 0
> > > > > Drawing layer : 1
> > > > > Cloninng layer : 1
> > > > > Done Cloninng layer : 1
> > > > > Drawing layer : 2
> > > > > Cloninng layer : 2
> > > > > Done Cloninng layer : 2
> > > > > Drawing layer : 3
> > > > >
> > > > >  *** Break *** segmentation violation
> > > > > Root > Function scalerName() busy flag cleared
> > > > > Function makePlots() busy flag cleared
> > > > > Function doPlots() busy flag cleared
> > > > >
> > > > > Any idea?
> > > > >
> > > > > Vuko
> > > > >
> > > > > Rene Brun wrote:
> > > > > >
> > > > > > Vuko,
> > > > > >
> > > > > > Sorry if I was not clear enough in my previous mail. I suggest to replace:
> > > > > >    int ilay;
> > > > > >    for (ilay=0; ilay<4; ilay++) {
> > > > > >      t1->Draw(scalerName(ilay+1)+":time >> srhist",cutString);
> > > > > >      srhistos[ilay]= new TH2F(*srhist);
> > > > > > by
> > > > > >    int ilay;
> > > > > >    TString hname = "rhist_";
> > > > > >    for (ilay=0; ilay<4; ilay++) {
> > > > > >      t1->Draw(scalerName(ilay+1)+":time >> srhist",cutString);
> > > > > >      srhistos[ilay]= (TH2F*)srhist->Clone(hname+ilay);
> > > > > >
> > > > > > Rene Brun
> > > > > >
> > > > > >
> > > > > > Vuko Brigljevic wrote:
> > > > > > >
> > > > > > > Hi Rene,
> > > > > > >
> > > > > > > Thanks for helping!
> > > > > > >
> > > > > > > Rene Brun wrote:
> > > > > > > >
> > > > > > > > Hi Vuko,
> > > > > > > >
> > > > > > > > Give a different name to each of your histograms.
> > > > > > >
> > > > > > > To which histogram should I give a different name, to the one named
> > > > > > > "srhist" (see my code below in my 1st message) to which I am copying the
> > > > > > > output of Tree->Draw? But then I can't use a loop, but that would be
> > > > > > > rather
> > > > > > > annoying. I am probably misunderstanding...
> > > > > > >
> > > > > > > > In your case, t1->Draw will delete the previous histogram named "srhist"
> > > > > > > > and your pointers in srhistos[] will be dangling pointers.
> > > > > > >
> > > > > > > Really? I am confused, I would have thought that the line:
> > > > > > >
> > > > > > >      srhistos[ilay]= new TH2F(*srhist);
> > > > > > >
> > > > > > > creates a new TH2F (srhistos is an array of pointers to TH2F
> > > > > > > objects) using the copy constructor, i.e. *(srhistos[ilay])
> > > > > > > is a COPY of *srhist, and not the same object, or does the copy
> > > > > > > constructor just let some parts of TH2F point to the original
> > > > > > > one? If the latter is the case, that does not seem to be the point
> > > > > > > of a copy constructor IMHO.
> > > > > > >
> > > > > > > Can you please clarify this, thanks.
> > > > > > >
> > > > > > > Vuko
> > > > > > >
> > > > > > > > On Tue, 26 Mar 2002, Vuko Brigljevic wrote:
> > > > > > > >
> > > > > > > > > Hi all,
> > > > > > > > >
> > > > > > > > > The following is done using root 3.02/07 on SunOS,
> > > > > > > > > I see the same problem on Linux.
> > > > > > > > >
> > > > > > > > > I am trying to use the same 2D histo several times in a row
> > > > > > > > > as output of a Tree->Draw() and I am crashing. The code:
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >   TH2F *srhistos[10];
> > > > > > > > >
> > > > > > > > >   int ilay;
> > > > > > > > >   for (ilay=0; ilay<4; ilay++) {
> > > > > > > > >     t1->Draw(scalerName(ilay+1)+":time >> srhist",cutString);
> > > > > > > > >     srhistos[ilay]= new TH2F(*srhist);
> > > > > > > > >     srhistos[ilay]->GetXaxis()->SetTimeDisplay(1);
> > > > > > > > >     srhistos[ilay]->GetXaxis()->SetTimeFormat("%d/%m");
> > > > > > > > >     srhistos[ilay]->Draw();
> > > > > > > > >     gPad->SaveAs(TString(prefix+"singles-layer"+ilay+".eps"));
> > > > > > > > >     //    srhist.Reset();     tried this as well: does not help
> > > > > > > > >     delete srhist;
> > > > > > > > >   }
> > > > > > > > >
> > > > > > > > > Whether I am deleting the temporary histogram (srhist),
> > > > > > > > > resetting it or doing neither of both, I am always
> > > > > > > > > crashing with the following output:
> > > > > > > > >
> > > > > > > > > Info in <TPad::Print>: PostScript file lastDays/singles-layer1.eps has
> > > > > > > > > been created
> > > > > > > > >
> > > > > > > > >  *** Break *** segmentation violation
> > > > > > > > > Root > Function makePlots() busy flag cleared
> > > > > > > > > Function doPlots() busy flag cleared
> > > > > > > > >
> > > > > > > > > How am I supposed to do something like that?
> > > > > > > > >
> > > > > > > > > Thanks for any help!
> > > > > > > > >
> > > > > > > > > Vuko
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > ========================================================================
> > > > > > > > >  Vuko Brigljevic                        | Mail : SLAC, Mail Stop 41
> > > > > > > > >  Lawrence Livermore National Laboratory |        P.O. Box 4349, Stanford
> > > > > > > > >  Nuclear and Particle Physics Division  |        CA 94309, USA
> > > > > > > > >                                         | SLAC-Office : B280- room 143
> > > > > > > > >  Member of the BaBar Collaboration      | Phone : +1-650-926 8512
> > > > > > > > >  <vuko@slac.stanford.edu>          <http://www.slac.stanford.edu/~vuko>
> > > > > > > > > ========================================================================
> > > > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > >
> > > > > > > ========================================================================
> > > > > > >  Vuko Brigljevic                        | Mail : SLAC, Mail Stop 41
> > > > > > >  Lawrence Livermore National Laboratory |        P.O. Box 4349, Stanford
> > > > > > >  Nuclear and Particle Physics Division  |        CA 94309, USA
> > > > > > >                                         | SLAC-Office : B280- room 143
> > > > > > >  Member of the BaBar Collaboration      | Phone : +1-650-926 8512
> > > > > > >  <vuko@slac.stanford.edu>          <http://www.slac.stanford.edu/~vuko>
> > > > > > > ========================================================================
> > > > >
> > > > > --
> > > > >
> > > > > ========================================================================
> > > > >  Vuko Brigljevic                        | Mail : SLAC, Mail Stop 41
> > > > >  Lawrence Livermore National Laboratory |        P.O. Box 4349, Stanford
> > > > >  Nuclear and Particle Physics Division  |        CA 94309, USA
> > > > >                                         | SLAC-Office : B280- room 143
> > > > >  Member of the BaBar Collaboration      | Phone : +1-650-926 8512
> > > > >  <vuko@slac.stanford.edu>          <http://www.slac.stanford.edu/~vuko>
> > > > > ========================================================================
> > >
> > > --
> > >
> > > ========================================================================
> > >  Vuko Brigljevic                        | Mail : SLAC, Mail Stop 41
> > >  Lawrence Livermore National Laboratory |        P.O. Box 4349, Stanford
> > >  Nuclear and Particle Physics Division  |        CA 94309, USA
> > >                                         | SLAC-Office : B280- room 143
> > >  Member of the BaBar Collaboration      | Phone : +1-650-926 8512
> > >  <vuko@slac.stanford.edu>          <http://www.slac.stanford.edu/~vuko>
> > > ========================================================================
> > >
> 
> --
> 
> ========================================================================
>  Vuko Brigljevic                        | Mail : SLAC, Mail Stop 41
>  Lawrence Livermore National Laboratory |        P.O. Box 4349, Stanford
>  Nuclear and Particle Physics Division  |        CA 94309, USA
>                                         | SLAC-Office : B280- room 143
>  Member of the BaBar Collaboration      | Phone : +1-650-926 8512
>  <vuko@slac.stanford.edu>          <http://www.slac.stanford.edu/~vuko>
> ========================================================================



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:47 MET