Combining 2 root files in a new histogram TH2F - How do you fill Histograms from Chains?


ROOT Version: 5.34.36
_Platform: Not Provided
_Compiler: Not Provided


Hi everybody!

I have an error like this but I can’t see where is the error could anyone help me?

code root version 5.34.36

#include "TROOT.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TBrowser.h"
#include "TH1.h"
#include "TF1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TStopwatch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

void anaGam1(){  /// fecha linha 175
Int_t mm=0;

// read a tree already done

      // defined a chain and added

     TChain fChain("Samp3"); //samp.geom
      
      fChain.Add("Samp3_tree_1000.root"); //*/

       /*TChain fChain("Samp4"); //samp1.geom
      
      fChain.Add("Samp4_tree_1000.root"); */
            
     // if (!t) { delete f_gamos; continue; } // just a precaution

	//chain.Print();
// definition of variables	
	Int_t n_entries=0, EventID=0;
	Double_t EventAEnerDep=0.0, EventAEnerLost=0.0, EventInitKinEn;
	
	std::vector<int> *StepNumber=0;
 	std::vector<int> *StepEventID = 0;
	std::vector<double> *StepFinKinEn=0;
	std::vector<double> *StepInKinEn=0;
	std::vector<double>  *StepAcEnDep=0;
   	std::vector<double>  *StepAcEnLost=0;

   	std::vector<double>  *StepFinPosX=0;
   	std::vector<double>  *StepFinPosY=0;
   	std::vector<double>  *StepFinPosZ=0;
	std::vector<string> *StepFinLogVol;//=0;
	std::vector<string> *StepInitLogVol;//=0;   
	std::vector<string> *StepParticle;

// carry out the same variables defined	

  fChain.SetBranchAddress("Event_EventID",&EventID);
  fChain.SetBranchAddress("Event_AccumulatedEnergyDeposited",&EventAEnerDep);
  fChain.SetBranchAddress("Event_AccumulatedEnergyLost",&EventAEnerLost);
  fChain.SetBranchAddress("Event_InitialKineticEnergy",&EventInitKinEn);
  
  fChain.SetBranchAddress("Step_StepNumber", &StepNumber);
  fChain.SetBranchAddress("Step_EventID",&StepEventID);
  fChain.SetBranchAddress("Step_AccumulatedEnergyDeposited",&StepAcEnDep);
  fChain.SetBranchAddress("Step_AccumulatedEnergyLost",&StepAcEnLost);
  fChain.SetBranchAddress("Step_Particle",&StepParticle);

  fChain.SetBranchAddress("Step_FinalKineticEnergy",&StepFinKinEn);
  fChain.SetBranchAddress("Step_InitialKineticEnergy",&StepInKinEn);
  fChain.SetBranchAddress("Step_FinalLogicalVolume", &StepFinLogVol);
  fChain.SetBranchAddress("Step_InitialLogicalVolume", &StepInitLogVol);
  fChain.SetBranchAddress("Step_FinalPosX", &StepFinPosX);
  fChain.SetBranchAddress("Step_FinalPosY", &StepFinPosY);
  fChain.SetBranchAddress("Step_FinalPosZ", &StepFinPosZ);

 
///////////////// fill histogram and load tree

  int nbins = 200;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  TH2F *histo3 = new TH2F("histo3", "histo3", nbins, -1.5, 1.5, nbins, 0., 0.0455); // hist->Samp2 (samp1.in) 0.05  0., -1.5, 1.5,
  //TH2F *histo4 = new TH2F("histo4", "histo4", nbins, -1.5, 1.5, nbins, 0., 0.0455); // hist1-> Samp1 (samp.in) 0.025

  

	Long64_t nb = 0, nbytes = 0;
	n_entries=fChain.GetEntries();
	for (Int_t j=0; j<n_entries;j++){ //run in all tree //////fecha linha 160
		
    		nb = fChain.GetEntry(j); nbytes += nb;

                //cout<<"file= "<<i<<endl;
		cout<<"entries vector = "<<n_entries<<endl;
		//cout<<"entries vector 1 = "<<fChain.GetEntriesFast()<<endl;
		cout<<"nbytes = "<<nbytes<<endl;
		//cout<<""<<
		cout<<"Entry = "<<j<<endl;
		cout<<"size = "<<StepFinLogVol->size()-1<<endl;

		//cout<<"EventID = "<<EventID<<endl;
		
//		cout<<"EventAEnerDep = "<<EventAEnerDep<<endl;
//	        cout<<"EventAEnerLost = "<<EventAEnerLost<<endl;
//		cout<<"StepEventID = "<<StepEventID->size()<<endl;
//		cout<<"StepFinKinEn = "<<StepFinKinEn->size()<<endl;

		//cout<<"Value Vol(before) = "<<StepFinLogVol->at(i)<<endl;
          for(Int_t i=0; i<n_entries;i++){
		if(StepFinLogVol->size()!=0){ //load the branches of the tree ///fecha linha 158
			Int_t contador=0;
			cout<<endl;

			while (StepFinLogVol->at(contador) !="control" && contador < StepFinLogVol->size()-1){ /// fecha linha 137
				contador++;

			      	//hist ->Fill(StepFinKinEn->at(contador)); 
	    			/*cout<<"entry vector = "<<contador<<endl;
				cout<<"volume = "<<StepFinLogVol->at(contador)<<endl;
		      		//hist ->Fill(StepFinKinEn->at(contador)); 
				cout<<"StepInKinEn = "<<StepInKinEn->at(contador)<<endl;

				cout<<"StepFinKinEn = "<<StepFinKinEn->at(contador)<<endl;
				cout<<endl;
				cout<<"#####"<<endl;

				cout<<"EnLost by Step = "<<StepInKinEn->at(contador)-StepFinKinEn->at(contador)<<endl;
				
				cout<<"StepAcEnLost = "<<StepAcEnLost->at(contador)<<endl;*/

			}

			Double_t sum=0;
			if (StepFinLogVol->at(contador) !="control") { // fecha linha 148
				
				for(Int_t l=0;l<StepNumber->size();l++){ /// fecha linha 144
					sum = sum + StepAcEnLost->at(l);
				} 
				//hist1 -> Fill(sum); //enter in control
				//hist -> Fill(StepAcEnLost->at(contador));
				//hist -> Fill(EventAEnerLost); //deu certo!! 
			}

			if (StepFinLogVol->at(contador) =="control" && StepParticle->at(contador) != "e-") {//linha 154
				mm++; 
				cout<<"cont = "<<contador<<endl;
				histo3 -> Fill(StepFinKinEn->at(contador),StepFinPosX->>at(contador));
			}
			
			cout<<endl;
                             
		}
            }
	}
cout<< "numero de partículas no controlo =  "<< mm<<endl;

histo3 -> SetNameTitle("energy", "FinKinEn;EKin;Counts"); //Kinetic Energy Energy Lost
histo3 -> SetFillColor(0); 
histo3 -> Draw("");

//TFile *file5 = new TFile("histo4.root", "RECREATE"); // Samp2
TFile *file4 = new TFile("histo3.root", "RECREATE");

histo3->Write();

}

Thanks

Hi David,
what is the error you get?

Error: G__getvariable: expression

This is the error that gave me, but I don’t know how or where is this error, the same question of years before (How do you fill Histograms from Chains?), for I may fix.

Thanks

I could fix the error, but my histogram was empty. I can’t understand what I missed. Could anyone help?

#include "TROOT.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TBrowser.h"
#include "TH1.h"
#include "TF1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TStopwatch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

void posX(){  /// fecha linha 175
Int_t mm=0;

// read a tree already done

      // defined a chain and added

     TChain fChain("Samp3"); //samp.geom
      
     fChain.Add("Samp3_tree_1000.root"); //*/

       /*TChain fChain("Samp4"); //samp1.geom
      
      fChain.Add("Samp4_tree_1000.root"); */
            
     // if (!t) { delete f_gamos; continue; } // just a precaution

	//chain.Print();
// definition of variables	
	Int_t n_entries=0, EventID=0;
	Double_t EventAEnerDep=0.0, EventAEnerLost=0.0, EventInitKinEn;
	
	std::vector<int> *StepNumber=0;
 	std::vector<int> *StepEventID=0;
	std::vector<int> *StepTrackID=0;
	std::vector<double> *StepFinKinEn=0;
	std::vector<double> *StepInKinEn=0;
	std::vector<double>  *StepAcEnDep=0;
   	std::vector<double>  *StepAcEnLost=0;

   	std::vector<double>  *StepFinPosX=0;
   	std::vector<double>  *StepFinPosY=0;
   	std::vector<double>  *StepFinPosZ=0;
	std::vector<string> *StepFinLogVol;//=0;
	std::vector<string> *StepInitLogVol;//=0;   
	std::vector<string> *StepParticle;

// carry out the same variables defined	

  fChain.SetBranchAddress("Event_EventID",&EventID);
  fChain.SetBranchAddress("Event_AccumulatedEnergyDeposited",&EventAEnerDep);
  fChain.SetBranchAddress("Event_AccumulatedEnergyLost",&EventAEnerLost);
  fChain.SetBranchAddress("Event_InitialKineticEnergy",&EventInitKinEn);
  
  fChain.SetBranchAddress("Step_StepNumber", &StepNumber);
  fChain.SetBranchAddress("Step_TrackID", &StepTrackID);
  fChain.SetBranchAddress("Step_EventID",&StepEventID);
  fChain.SetBranchAddress("Step_AccumulatedEnergyDeposited",&StepAcEnDep);
  fChain.SetBranchAddress("Step_AccumulatedEnergyLost",&StepAcEnLost);
  fChain.SetBranchAddress("Step_Particle",&StepParticle);

  fChain.SetBranchAddress("Step_FinalKineticEnergy",&StepFinKinEn);
  fChain.SetBranchAddress("Step_InitialKineticEnergy",&StepInKinEn);
  fChain.SetBranchAddress("Step_FinalLogicalVolume", &StepFinLogVol);
  fChain.SetBranchAddress("Step_InitialLogicalVolume", &StepInitLogVol);
  fChain.SetBranchAddress("Step_FinalPosX", &StepFinPosX);
  fChain.SetBranchAddress("Step_FinalPosY", &StepFinPosY);
  fChain.SetBranchAddress("Step_FinalPosZ", &StepFinPosZ);

 
///////////////// fill histogram and load tree

  int nbins = 100;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  TH2F *pos = new TH2F("pos", "pos", nbins, 0., 0.0455, nbins, -2, 2); // event->Samp2 (samp1.in) 0.05  0., -1.5, 1.5,
  //TH2F *pos1 = new TH1F("pos1", "pos1", nbins, -1.5, 1.5, nbins, 0., 0.0455); // event1-> Samp1 (samp.in) 0.025

  
	Long64_t nb = 0, nbytes = 0;
	n_entries=fChain.GetEntries();
	for (Int_t j=0; j<n_entries;j++){ //run in all tree //////fecha linha 157
		
    		nb = fChain.GetEntry(j); nbytes += nb;

                //cout<<"file= "<<i<<endl;
		cout<<"entries vector = "<<n_entries<<endl;
		//cout<<"entries vector 1 = "<<fChain.GetEntriesFast()<<endl;
		cout<<"nbytes = "<<nbytes<<endl;
		//cout<<""<<
		cout<<"Entry = "<<j<<endl;
		cout<<"size = "<<StepFinLogVol->size()-1<<endl;
	        cout<<"trackID = " <<StepTrackID->size()<<endl;	

		//cout<<"EventID = "<<EventID<<endl;
		
//		cout<<"EventAEnerDep = "<<EventAEnerDep<<endl;
//	        cout<<"EventAEnerLost = "<<EventAEnerLost<<endl;
//		cout<<"StepEventID = "<<StepEventID->size()<<endl;
//		cout<<"StepFinKinEn = "<<StepFinKinEn->size()<<endl;

		//cout<<"Value Vol(before) = "<<StepFinLogVol->at(i)<<endl;

		if(StepFinLogVol->size()!=0){ //load the branches of the tree ///fecha linha 155
			Int_t contador=0;
			cout<<endl;

			while (StepFinLogVol->at(contador) !="control" && contador < StepFinLogVol->size()-1){ /// fecha linha 132
				contador++;

			      	//hist ->Fill(StepFinKinEn->at(contador)); 
	    			/*cout<<"entry vector = "<<contador<<endl;
				cout<<"volume = "<<StepFinLogVol->at(contador)<<endl;
		      		//hist ->Fill(StepFinKinEn->at(contador)); 
				cout<<"StepInKinEn = "<<StepInKinEn->at(contador)<<endl;

				cout<<"StepFinKinEn = "<<StepFinKinEn->at(contador)<<endl;
				cout<<endl;
				cout<<"#####"<<endl;

				cout<<"EnLost by Step = "<<StepInKinEn->at(contador)-StepFinKinEn->at(contador)<<endl;
				
				cout<<"StepAcEnLost = "<<StepAcEnLost->at(contador)<<endl;*/

			}

			Double_t sum=0;
			if (StepFinLogVol->at(contador) !="control") { // fecha linha 144
				
				for(Int_t l=0;l<StepNumber->size();l++){ /// fecha linha 139
					sum = sum + StepAcEnLost->at(l);
				} 
				//hist1 -> Fill(sum); //enter in control
				//hist -> Fill(StepAcEnLost->at(contador));
				//hist -> Fill(EventAEnerLost); //deu certo!! 
			}

			if (StepFinLogVol->at(contador) =="control") {//linha 150 StepTrackID->at(contador)
				mm++; 
				//if(StepTrackID->at(contador)-1==0){
					pos -> Fill(StepFinPosX->at(contador), StepFinKinEn->at(contador));
				//}
			}
			cout<<"track value = "<<StepTrackID->at(contador)<<endl;
			cout<<"cont = "<<contador<<endl;
			cout<<endl;
                             
		}

}
cout<< "numero de partículas no controlo =  "<< mm<<endl;

pos -> SetNameTitle("energy", "PositionX versus EKin;EKin;Counts"); //Kinetic Energy Energy Lost
pos -> SetFillColor(0); 
pos -> Draw("hist");

TFile *p = new TFile("PX_x_Ekin.root", "RECREATE"); // Samp2
//TFile *p1 = new TFile("PX_x_Ekin1.root", "RECREATE");

pos->Write();

}

PX_x_Ekin.root (872 Bytes)
This link above is the histogram TH2F generated.
The link bellow contains the root file to this code.
https://drive.google.com/drive/folders/18IHzSLsKibcrXS0U0jQiv9rEV0m0LHYP?usp=sharing

Thanks

The problem was resolved, so I put here the fixed code above… with many root files.

#include "TROOT.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TBrowser.h"
#include "TH1.h"
#include "TF1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TStopwatch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

void posX(){  /// fecha linha 175

/*gROOT->cd();
  THStack *hs =
    new THStack("pos",
                "PositionX versus EKin;EKin;Counts");*/

Int_t mm=0;

// read a tree already done

      // defined a chain and added

     /*TChain fChain("Samp3"); //samp.geom
      
     fChain.Add("Samp3_tree_1000.root"); */

      TChain fChain("Samp2"); //geometry samp1.geom 0.0
      
      fChain.Add("Samp2_tree_1000.root"); fChain.Add("Samp2_tree__1001.root"); fChain.Add("Samp2_tree__1002.root"); 
      fChain.Add("Samp2_tree__1003.root"); fChain.Add("Samp2_tree__1004.root"); fChain.Add("Samp2_tree__1005.root");
      fChain.Add("Samp2_tree__1006.root"); fChain.Add("Samp2_tree__1007.root"); fChain.Add("Samp2_tree__1008.root");
      fChain.Add("Samp2_tree__1009.root");

      /*TChain fChain("Samp1"); 
      
      fChain.Add("Samp1_tree_1000.root"); fChain.Add("Samp1_tree_1001.root"); fChain.Add("Samp1_tree_1002.root"); 
      fChain.Add("Samp1_tree_1003.root"); fChain.Add("Samp1_tree_1004.root"); fChain.Add("Samp1_tree_1005.root");
      fChain.Add("Samp1_tree_1006.root"); fChain.Add("Samp1_tree_1007.root"); fChain.Add("Samp1_tree_1008.root");
      fChain.Add("Samp1_tree_1009.root"); */

       /*TChain fChain("Samp4"); //samp1.geom
      
      fChain.Add("Samp4_tree_1000.root"); */
            
     // if (!t) { delete f_gamos; continue; } // just a precaution

	//chain.Print();

// definition of variables	
	Int_t n_entries=0, EventID=0;
	Double_t EventAEnerDep=0.0, EventAEnerLost=0.0, EventInitKinEn;

	std::vector<int> *TrTrackID=0;

	std::vector<int> *StepNumber=0;
	std::vector<int> *StepTrackID=0;
 	std::vector<int> *StepEventID = 0;
	std::vector<double> *StepFinKinEn=0;
	std::vector<double> *StepInKinEn=0;
	std::vector<double>  *StepAcEnDep=0;
   	std::vector<double>  *StepAcEnLost=0;

   	std::vector<double>  *StepFinPosX=0;
   	std::vector<double>  *StepFinPosY=0;
   	std::vector<double>  *StepFinPosZ=0;

	std::vector<string> *StepCreatProcess;
	std::vector<string> *StepFinLogVol;//=0;
	std::vector<string> *StepInitLogVol;//=0;   
	std::vector<string> *StepParticle;
   	std::vector<string> *StepInitProcess;
   	std::vector<string> *StepFinProcess;
	std::vector<string> *TrCreatProcess;

// carry out the same variables defined	

//*** numbers
  fChain.SetBranchAddress("Event_EventID",&EventID);
  fChain.SetBranchAddress("Event_AccumulatedEnergyDeposited",&EventAEnerDep);
  fChain.SetBranchAddress("Event_AccumulatedEnergyLost",&EventAEnerLost);
  fChain.SetBranchAddress("Event_InitialKineticEnergy",&EventInitKinEn);

//*** vector int
  fChain.SetBranchAddress("Track_TrackID", &TrTrackID);  
  fChain.SetBranchAddress("Step_StepNumber", &StepNumber);
  fChain.SetBranchAddress("Step_TrackID", &StepTrackID);
  fChain.SetBranchAddress("Step_EventID",&StepEventID);

//*** vector double
  fChain.SetBranchAddress("Step_AccumulatedEnergyDeposited",&StepAcEnDep);
  fChain.SetBranchAddress("Step_AccumulatedEnergyLost",&StepAcEnLost);
  fChain.SetBranchAddress("Step_FinalKineticEnergy",&StepFinKinEn);
  fChain.SetBranchAddress("Step_InitialKineticEnergy",&StepInKinEn);
  fChain.SetBranchAddress("Step_FinalPosX", &StepFinPosX);
  fChain.SetBranchAddress("Step_FinalPosY", &StepFinPosY);
  fChain.SetBranchAddress("Step_FinalPosZ", &StepFinPosZ);

//*** vector string
  fChain.SetBranchAddress("Step_Particle",&StepParticle);
  fChain.SetBranchAddress("Step_FinalLogicalVolume", &StepFinLogVol);
  fChain.SetBranchAddress("Step_InitialLogicalVolume", &StepInitLogVol);
  fChain.SetBranchAddress("Step_InitialProcess", &StepInitProcess);
  fChain.SetBranchAddress("Step_FinalProcess", &StepFinProcess);
  fChain.SetBranchAddress("Step_CreatorProcess",&StepCreatProcess);
  fChain.SetBranchAddress("Track_CreatorProcess",&TrCreatProcess);

 
///////////////// fill histogram and load tree

  int nbins = 100;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  TH2F *pos = new TH2F("pos", "pos", nbins, 0., 0.0455, nbins, -1.0, 1.0); // event->Samp2 (samp1.in) 0.05  0., -1.5, 1.5,
  //TH2F *pos1 = new TH1F("pos1", "pos1", nbins, -1.5, 1.5, nbins, 0., 0.0455); // event1-> Samp1 (samp.in) 0.025

  c1->Divide(1,3);

  c1->cd(1);
  
	Long64_t nb = 0, nbytes = 0;
	n_entries=fChain.GetEntries();
	for (Int_t j=0; j<n_entries;j++){ //run in all tree //////fecha linha 157
		
    		nb = fChain.GetEntry(j); nbytes += nb;

                //cout<<"file= "<<i<<endl;
		cout<<"entries vector = "<<n_entries<<endl;
		//cout<<"entries vector 1 = "<<fChain.GetEntriesFast()<<endl;
		cout<<"nbytes = "<<nbytes<<endl;
		//cout<<""<<
		cout<<"Entry = "<<j<<endl;
		cout<<"size = "<<StepFinLogVol->size()-1<<endl;
	        cout<<"trackID = " <<StepTrackID->size()<<endl;	

		//cout<<"EventID = "<<EventID<<endl;
		
//		cout<<"EventAEnerDep = "<<EventAEnerDep<<endl;
//	        cout<<"EventAEnerLost = "<<EventAEnerLost<<endl;
//		cout<<"StepEventID = "<<StepEventID->size()<<endl;
//		cout<<"StepFinKinEn = "<<StepFinKinEn->size()<<endl;

		//cout<<"Value Vol(before) = "<<StepFinLogVol->at(i)<<endl;

		if(StepFinLogVol->size()!=0){ //load the branches of the tree ///fecha linha 157
			Int_t contador=0;
			cout<<endl;

			while (StepFinLogVol->at(contador) !="control" && contador < StepFinLogVol->size()-1){ /// fecha linha 132
				contador++;
			}

			Double_t sum=0;
			if (StepFinLogVol->at(contador) !="control") { // fecha linha 143
				
				for(Int_t l=0;l<StepNumber->size();l++){ /// fecha linha 139
					sum = sum + StepAcEnLost->at(l);
				} 
				//hist1 -> Fill(sum); //enter in control
				//hist -> Fill(StepAcEnLost->at(contador));
				//hist -> Fill(EventAEnerLost); //deu certo!! 
			}

			if (StepFinLogVol->at(contador) =="control") {//linha 150 StepTrackID->at(contador)
				mm++; 
				//if(StepTrackID->at(contador)==0){
				pos->Fill(StepFinKinEn->at(contador),StepFinPosX->at(contador));
				//}
			}
			cout<<"track value = "<<StepTrackID->at(contador)<<endl;
			cout<<"cont = "<<contador<<endl;
			cout<<endl;
                             
		}

	}

cout<< "numero de partículas no controlo =  "<< mm<<endl;

pos -> SetNameTitle("energy", "PositionX versus EKin 0.025;EKin;Counts"); //Kinetic Energy Energy Lost
pos -> SetFillColor(0); 
pos -> Draw("COLZ");
c1->cd(2);
pos->ProjectionX("PROJECTION IN X")->Draw();
c1->cd(3);
pos->ProjectionY("PROJECTION IN Y")->Draw();

//TFile *p = new TFile("PX_x_Ekin.root", "RECREATE"); // Samp1
TFile *p1 = new TFile("PX_x_Ekin1.root", "RECREATE"); // Samp2

pos->Write();

}

Best regards!

1 Like

ROOT 6 has much improved error messages - which explains why you didn’t get a lot of help here: it’s really time consuming for us to deal with errors from very old ROOT versions… Glad you managed to solve this yourself!

@axel There are uninitialized pointers used.

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