TTree Draw() GetV

Dears,

I’m using the data from TTree to produce a plot . I’m using the .GetV method to extract the data and assingne to variables. I draw in a first moment the first 3 variables and I get the values using GetV. (BOT, TN, TW) Then I draw again the tree using 3 different variables and I get also these 3 (BOT2, TN2, TN2).

The problem is that, also if I give different name to the variables, at the end they have the same value, equal to the second took: BOT=BOT2, TN=TN2, TW=TW2. Below a piece of code.

tree.Draw("resistivity_BOT"+chamber+":resistivity_TN"+chamber+":resistivity_TW"+chamber,"","goff")	
resistivityBOT= tree.GetV1()	
resistivityTN= tree.GetV2()	
resistivityTW= tree.GetV3()

tree.Draw("resistivity_BOT"+chamber2+":resistivity_TN"+chamber2+":resistivity_TW"+chamber2,"","goff")	
resistivityBOT2= tree.GetV1()	
resistivityTN2= tree.GetV2()	
resistivityTW2= tree.GetV3()

Thanks a lot in advance for your help :slight_smile:

TTree::GetV1 and others simply return a pointer to an array of values. When you call the Draw command the second time it reassigns the values in the array at the same memory address. And thus calling GetV1 again will give you the same address as previously, but the values at that address have now changed. If you print the addresses as below you will see they are the same:

std::cout << resistivityBOT << " is the same as " << resistivityBOT2 << "\n";

You could create vectors from the arrays and then use those later. Here is an example:

{
   //Create a tree
   TTree * t = new TTree("t", "T");
   double x, y;
   t->Branch("x", &x);
   t->Branch("y", &y);

   // Fill it with some random data
   for (int i=0;i<10000;i++) {
      x = gRandom->Uniform();
      y = gRandom->Gaus(0, x);
      t->Fill();
   }

   t->ResetBranchAddresses();

   // Make a vector for the x-values.
   t->Draw("x","","GOFF");
   std::vector<double> xVals(t->GetV1(), t->GetV1() + t->GetSelectedRows() % t->GetEstimate());

   // Make a vector for the y-values.
   t->Draw("y","","GOFF");
   std::vector<double> yVals(t->GetV1(), t->GetV1() + t->GetSelectedRows() % t->GetEstimate());

   // Do something with the values.
   TH2D* h = new TH2D("h", "H", 100, 0, 1, 100, -3, 3);
   for (int i=0;i<xVals.size() && i < yVals.size(); i++) {
      h->Fill(xVals.at(i), yVals.at(i));
   }
   h->Draw("COLZ");
}

And the output:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.