TChain Add multiples files in a tree


ROOT Version: 5.34.36
Platform: Ubuntu 16.04 and 14.04
__

#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 analy(){  /// fecha linha 175
Int_t mm=0;

// read a tree already done
  const char *files[] = {"Samp_tree_1000.root", "Samp_tree_1001.root", "Samp_tree_1002.root", "Samp_tree_1003.root", "Samp_tree_1004.root",
                         "Samp_tree_1005.root", "Samp_tree_1006.root", "Samp_tree_1007.root", "Samp_tree_1008.root", "Samp_tree_1009.root", 
			 "Samp_tree_1010.root", "Samp_tree_1011.root", "Samp_tree_1012.root", "Samp_tree_1013.root", "Samp_tree_1014.root",
			 "Samp_tree_1015.root", "Samp_tree_1016.root", "Samp_tree_1017.root", "Samp_tree_1018.root", "Samp_tree_1019.root", 
			 "Samp_tree_1020.root", "Samp_tree_1021.root", "Samp_tree_1022.root", "Samp_tree_1023.root", "Samp_tree_1024.root",
			 "Samp_tree_1025.root", "Samp_tree_1026.root", "Samp_tree_1027.root", "Samp_tree_1028.root", "Samp_tree_1029.root",
			 "Samp_tree_1030.root", "Samp_tree_1031.root", "Samp_tree_1032.root", "Samp_tree_1033.root", "Samp_tree_1034.root",
			 "Samp_tree_1035.root", "Samp_tree_1036.root", "Samp_tree_1037.root", "Samp_tree_1038.root", "Samp_tree_1039.root",
   		  	 "Samp_tree_1040.root", "Samp_tree_1041.root", "Samp_tree_1042.root", "Samp_tree_1043.root", "Samp_tree_1044.root",
			 "Samp_tree_1045.root", "Samp_tree_1046.root", "Samp_tree_1047.root", "Samp_tree_1048.root", "Samp_tree_1049.root",
			 "Samp_tree_1050.root", "Samp_tree_1051.root", "Samp_tree_1052.root", "Samp_tree_1053.root", "Samp_tree_1054.root",
			 "Samp_tree_1055.root", "Samp_tree_1056.root", "Samp_tree_1057.root", "Samp_tree_1058.root", "Samp_tree_1059.root",
			 "Samp_tree_1060.root", "Samp_tree_1061.root", "Samp_tree_1062.root", "Samp_tree_1063.root", "Samp_tree_1064.root",
			 "Samp_tree_1065.root", "Samp_tree_1066.root", "Samp_tree_1067.root", "Samp_tree_1068.root", "Samp_tree_1069.root",
			 "Samp_tree_1070.root", "Samp_tree_1071.root", "Samp_tree_1072.root", "Samp_tree_1073.root", "Samp_tree_1074.root",
			 "Samp_tree_1075.root", "Samp_tree_1076.root", "Samp_tree_1077.root", "Samp_tree_1078.root", "Samp_tree_1079.root",
			 "Samp_tree_1080.root", "Samp_tree_1081.root", "Samp_tree_1082.root", "Samp_tree_1083.root", "Samp_tree_1084.root",
			 "Samp_tree_1085.root", "Samp_tree_1086.root", "Samp_tree_1087.root", "Samp_tree_1088.root", "Samp_tree_1089.root",
 			 "Samp_tree_1090.root", "Samp_tree_1091.root", "Samp_tree_1092.root", "Samp_tree_1093.root", "Samp_tree_1094.root",
			 "Samp_tree_1095.root", "Samp_tree_1096.root", "Samp_tree_1097.root", "Samp_tree_1098.root"};

   const int n = sizeof(files) / sizeof(char*);

   for (int i = 0; i < n; i++) { //fecha linha 50 (176)
      TChain chain("Samp");
      chain.Add(files[i]);
	//cout << "n " << n << endl;
	//cout<<"entry1 = " << chain.GetEntry(i) << endl;
	
    }
   /*   TFile *f_gamos = TFile::Open(files[i]);
     // if ((!f_gamos) || f_gamos->IsZombie()) { delete f_gamos; continue; } // just a precaution
      TTree *t; f_gamos->GetObject("Samp", t);
  
     // if (!t) { delete f_gamos; continue; } // just a precaution*/

	//t->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<string> *StepFinLogVol;//=0;
	std::vector<string> *StepInitLogVol;//=0;

// carry out the same variables defined	


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

  chain.SetBranchAddress("Step_FinalKineticEnergy",&StepFinKinEn);
  chain.SetBranchAddress("Step_InitialKineticEnergy",&StepInKinEn);
  chain.SetBranchAddress("Step_FinalLogicalVolume", &StepFinLogVol);
  chain.SetBranchAddress("Step_InitialLogicalVolume", &StepInitLogVol);

 

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


  int nbins = 100;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  TH1F *h = new TH1F("h", "h", nbins, 0., 0.0455);
	
	n_entries=chain.GetEntries();
	for (Int_t j=0; j<n_entries;j++){ //run in all tree //////fecha linha 174
		
    		chain.GetEntry(j);
		cout<<"entries vector = "<<n_entries<<endl;
		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;

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

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

			      	//h->Fill(StepFinKinEn->at(contador)); 
	    			/*cout<<"entry vector = "<<contador<<endl;
				cout<<"volume = "<<StepFinLogVol->at(contador)<<endl;
		      		//h->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 143
				
				for(Int_t l=0;l<StepNumber->size();l++){ /// fecha linha 140
					sum = sum + StepAcEnLost->at(l);
				} 
				//h->Fill(sum); //enter in control
				//h->Fill(StepAcEnLost->at(contador));
			}
			
			if (StepFinLogVol->at(contador) =="control") {mm++;} //h->Fill(StepInKinEn->at(contador));}

			cout<<endl;
                              //h->Fill(EventAEnerLost); //deu certo!! 

		}

//		
//			for (Int_t k=0;k<StepEventID->size();k++){ //run all vector in the event /// fecha na linha 174
//			
//				if(StepFinLogVol->at(k) != "control"){ //StepFinLogVol->at(j) == "control" ///fecha linha 172
					
								
			//	cout<<"StepEventID = "<<StepEventID->at(j)<<endl;
			//	cout<<"StepEventID = "<<StepEventID->size()<<endl;
			//	cout<<"StepFinKinEn = "<<StepFinKinEn->at(j)<<endl;
			//	cout<<"StepInKinEn(Val) = "<<StepInKinEn->at(j)<<endl;
					//cout<<"k = "<<k<<endl;
			//	cout<<"StepFinKinEn1 = "<<StepFinKinEn->size()<<endl;
			//	cout<<"Vol_Val = "<<StepFinLogVol->size()<<endl;
			//	cout<<"Vol_Val = "<<StepFinLogVol->at(j)<<endl;
						//cout<<"StepNumber = "<<StepNumber->at(j)<<endl;
						//cout<<endl;
			
					//h->Fill(EventAEnerLost); 				
				//	h->Fill(StepFinKinEn->at(k)); 
//					h->Fill(StepAcEnLost->at(k));              
//				}
//			
//			}
				
	} 

//h->Fill(sum); //enter in control

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

//cout<<"size  = "<<contador<<endl;
h->SetNameTitle("energy", "Step Energy Lost;EKin;Counts"); //Kinetic Energy Energy Lost
h->SetFillColor(0); 
h->Draw("");

}

I´m produced in GAMOS a input file that generated the 99 files in the code above. In this code I must read all files and using some branches to produce an histogram like those in attached. I use the same code above (Is the same code used above without the TChain).

Event_ELost1.pdf (14.6 KB)
Step_ELost1.pdf (14.6 KB)

When I put the TChain the result was wrong, so my question is what is wrong, how can I used th TChain in this case? I put here bellow the link with some of the files.
https://drive.google.com/drive/folders/1GjKyC3d94Qvz3H-OA0omL8D-BYr-0fV-?usp=sharing

I think that the entries appears in the histogram with this code is only of some files not the sum of the entries of all files.

Best regards

Try to apply MakeClass to your TChain (and then use the automatically generated class).

Thnaks @Wile_E_Coyote!

I was seen the MakeClass for TChain buit I have one question in the case, could you do some example to understand better and see if is what I imagine?

Thanks again!

I got something more close what I want and I see now that all file is search but the graphic of sum of the StepAcEnLost and Kinetic Energy only appears the ultimate data not the whole data. Am I am clearer?

The code change:

#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 analy1(){  /// fecha linha 175
Int_t mm=0;

// read a tree already done
  const char *files[] = {"Samp_tree_1000.root", "Samp_tree_1001.root", "Samp_tree_1002.root", "Samp_tree_1003.root", "Samp_tree_1004.root",
                         "Samp_tree_1005.root", "Samp_tree_1006.root", "Samp_tree_1007.root", "Samp_tree_1008.root", "Samp_tree_1009.root", 
			 "Samp_tree_1010.root", "Samp_tree_1011.root", "Samp_tree_1012.root", "Samp_tree_1013.root", "Samp_tree_1014.root",
			 "Samp_tree_1015.root", "Samp_tree_1016.root", "Samp_tree_1017.root", "Samp_tree_1018.root", "Samp_tree_1019.root", 
			 "Samp_tree_1020.root", "Samp_tree_1021.root", "Samp_tree_1022.root", "Samp_tree_1023.root", "Samp_tree_1024.root",
			 "Samp_tree_1025.root", "Samp_tree_1026.root", "Samp_tree_1027.root", "Samp_tree_1028.root", "Samp_tree_1029.root",
			 "Samp_tree_1030.root", "Samp_tree_1031.root", "Samp_tree_1032.root", "Samp_tree_1033.root", "Samp_tree_1034.root",
			 "Samp_tree_1035.root", "Samp_tree_1036.root", "Samp_tree_1037.root", "Samp_tree_1038.root", "Samp_tree_1039.root",
   		  	 "Samp_tree_1040.root", "Samp_tree_1041.root", "Samp_tree_1042.root", "Samp_tree_1043.root", "Samp_tree_1044.root",
			 "Samp_tree_1045.root", "Samp_tree_1046.root", "Samp_tree_1047.root", "Samp_tree_1048.root", "Samp_tree_1049.root",
			 "Samp_tree_1050.root", "Samp_tree_1051.root", "Samp_tree_1052.root", "Samp_tree_1053.root", "Samp_tree_1054.root",
			 "Samp_tree_1055.root", "Samp_tree_1056.root", "Samp_tree_1057.root", "Samp_tree_1058.root", "Samp_tree_1059.root",
			 "Samp_tree_1060.root", "Samp_tree_1061.root", "Samp_tree_1062.root", "Samp_tree_1063.root", "Samp_tree_1064.root",
			 "Samp_tree_1065.root", "Samp_tree_1066.root", "Samp_tree_1067.root", "Samp_tree_1068.root", "Samp_tree_1069.root",
			 "Samp_tree_1070.root", "Samp_tree_1071.root", "Samp_tree_1072.root", "Samp_tree_1073.root", "Samp_tree_1074.root",
			 "Samp_tree_1075.root", "Samp_tree_1076.root", "Samp_tree_1077.root", "Samp_tree_1078.root", "Samp_tree_1079.root",
			 "Samp_tree_1080.root", "Samp_tree_1081.root", "Samp_tree_1082.root", "Samp_tree_1083.root", "Samp_tree_1084.root",
			 "Samp_tree_1085.root", "Samp_tree_1086.root", "Samp_tree_1087.root", "Samp_tree_1088.root", "Samp_tree_1089.root",
 			 "Samp_tree_1090.root", "Samp_tree_1091.root", "Samp_tree_1092.root", "Samp_tree_1093.root", "Samp_tree_1094.root",
			 "Samp_tree_1095.root", "Samp_tree_1096.root", "Samp_tree_1097.root", "Samp_tree_1098.root"};

   const int n = sizeof(files) / sizeof(char*);



for(Int_t i=0; i<n; i++){	
      TFile *f_gamos = TFile::Open(files[i]);
     // if ((!f_gamos) || f_gamos->IsZombie()) { delete f_gamos; continue; } // just a precaution
      TTree *fChain; f_gamos->GetObject("Samp", fChain);

    
     // 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<string> *StepFinLogVol;//=0;
	std::vector<string> *StepInitLogVol;//=0;

// 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_FinalKineticEnergy",&StepFinKinEn);
  fChain->SetBranchAddress("Step_InitialKineticEnergy",&StepInKinEn);
  fChain->SetBranchAddress("Step_FinalLogicalVolume", &StepFinLogVol);
  fChain->SetBranchAddress("Step_InitialLogicalVolume", &StepInitLogVol);

 

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


  int nbins = 400;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  TH1F *h = new TH1F("h", "h", nbins, 0., 0.0455);
	
	n_entries=fChain->GetEntriesFast();
	for (Int_t j=0; j<n_entries;j++){ //run in all tree //////fecha linha 174
		
    		fChain->GetEntry(j);
                cout<<"file= "<<i<<endl;
		cout<<"entries vector = "<<n_entries<<endl;
		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;

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

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

			      	//h->Fill(StepFinKinEn->at(contador)); 
	    			/*cout<<"entry vector = "<<contador<<endl;
				cout<<"volume = "<<StepFinLogVol->at(contador)<<endl;
		      		//h->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 143
				
				for(Int_t l=0;l<StepNumber->size();l++){ /// fecha linha 140
					sum = sum + StepAcEnLost->at(l);
				} 
				//h->Fill(sum); //enter in control
				//h->Fill(StepAcEnLost->at(contador));
				//h->Fill(EventAEnerLost); //deu certo!! 
			}
			
			if (StepFinLogVol->at(contador) =="control") {
				mm++; 
				cout<<"cont = "<<contador<<endl; 	
				h->Fill(StepInKinEn->at(contador));
			}

			cout<<endl;
                             
		}

//		
//			for (Int_t k=0;k<StepEventID->size();k++){ //run all vector in the event /// fecha na linha 174
//			
//				if(StepFinLogVol->at(k) != "control"){ //StepFinLogVol->at(j) == "control" ///fecha linha 172
					
								
			//	cout<<"StepEventID = "<<StepEventID->at(j)<<endl;
			//	cout<<"StepEventID = "<<StepEventID->size()<<endl;
			//	cout<<"StepFinKinEn = "<<StepFinKinEn->at(j)<<endl;
			//	cout<<"StepInKinEn(Val) = "<<StepInKinEn->at(j)<<endl;
					//cout<<"k = "<<k<<endl;
			//	cout<<"StepFinKinEn1 = "<<StepFinKinEn->size()<<endl;
			//	cout<<"Vol_Val = "<<StepFinLogVol->size()<<endl;
			//	cout<<"Vol_Val = "<<StepFinLogVol->at(j)<<endl;
						//cout<<"StepNumber = "<<StepNumber->at(j)<<endl;
						//cout<<endl;
			
					//h->Fill(EventAEnerLost); 				
				//	h->Fill(StepFinKinEn->at(k)); 
//					h->Fill(StepAcEnLoschain.at(k));              
//				}
//			
//			}
				
	} 
//h->Fill(StepInKinEn->at(mm));
//h->Fill(sum); //enter in control
} 
cout<< "numero de partículas no controlo =  "<< mm<<endl;

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

}

The value of mm in the end gives the number of total in all files but I can reproduce this entries in the histogram of StepFinKinEn, this is one of the things what I want, the other is the graphic of EventAcEnLost and the sum of all StepAcEnLost doing the same as I expect. How can I fix this?

Thanks

I fixed this using the TChain and works well… I have just 1 question if possible:

My new code is:

#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 analyse(){  /// fecha linha 175
Int_t mm=0;

// read a tree already done
 
      // defined a chain and added

      TChain fChain("Samp"); 
      
      fChain.Add("Samp_tree_1000.root"); fChain.Add("Samp_tree_1001.root"); fChain.Add("Samp_tree_1002.root"); 
      fChain.Add("Samp_tree_1003.root"); fChain.Add("Samp_tree_1004.root"); fChain.Add("Samp_tree_1005.root");
      fChain.Add("Samp_tree_1006.root"); fChain.Add("Samp_tree_1007.root"); fChain.Add("Samp_tree_1008.root");
      fChain.Add("Samp_tree_1009.root"); fChain.Add("Samp_tree_1010.root"); fChain.Add("Samp_tree_1011.root"); 
      fChain.Add("Samp_tree_1012.root"); fChain.Add("Samp_tree_1013.root"); fChain.Add("Samp_tree_1014.root");
      fChain.Add("Samp_tree_1015.root"); fChain.Add("Samp_tree_1016.root"); fChain.Add("Samp_tree_1017.root");
      fChain.Add("Samp_tree_1018.root"); fChain.Add("Samp_tree_1019.root"); fChain.Add("Samp_tree_1020.root"); 
      fChain.Add("Samp_tree_1021.root"); fChain.Add("Samp_tree_1022.root"); fChain.Add("Samp1_tree_1023.root");
      fChain.Add("Samp_tree_1024.root"); fChain.Add("Samp_tree_1025.root"); fChain.Add("Samp1_tree_1026.root");
      fChain.Add("Samp_tree_1027.root"); fChain.Add("Samp_tree_1028.root"); fChain.Add("Samp_tree_1029.root"); 
      fChain.Add("Samp_tree_1030.root"); fChain.Add("Samp_tree_1031.root"); fChain.Add("Samp_tree_1032.root");
      
      fChain.Add("Samp_tree_1033.root"); fChain.Add("Samp_tree_1034.root"); fChain.Add("Samp1_tree_1035.root");
      fChain.Add("Samp1_tree_1036.root"); fChain.Add("Samp1_tree_1037.root"); fChain.Add("Samp_tree_1038.root"); 
      fChain.Add("Samp_tree_1039.root"); fChain.Add("Samp_tree_1040.root"); fChain.Add("Samp_tree_1041.root");
      fChain.Add("Samp_tree_1042.root"); fChain.Add("Samp_tree_1043.root"); fChain.Add("Samp_tree_1044.root");
      fChain.Add("Samp_tree_1045.root"); fChain.Add("Samp_tree_1046.root"); fChain.Add("Samp_tree_1047.root"); 
      fChain.Add("Samp_tree_1048.root"); fChain.Add("Samp_tree_1049.root"); fChain.Add("Samp_tree_1050.root");
      fChain.Add("Samp_tree_1051.root"); fChain.Add("Samp_tree_1052.root"); fChain.Add("Samp_tree_1053.root");
      fChain.Add("Samp_tree_1054.root"); fChain.Add("Samp_tree_1055.root"); fChain.Add("Samp_tree_1056.root"); 
      fChain.Add("Samp_tree_1057.root"); fChain.Add("Samp_tree_1058.root"); fChain.Add("Samp_tree_1059.root");
      fChain.Add("Samp_tree_1060.root"); fChain.Add("Samp_tree_1061.root"); fChain.Add("Samp1_tree_1062.root");
      fChain.Add("Samp_tree_1063.root"); fChain.Add("Samp_tree_1064.root"); fChain.Add("Samp_tree_1065.root"); 
      
      fChain.Add("Samp_tree_1066.root"); fChain.Add("Samp_tree_1067.root"); fChain.Add("Samp_tree_1068.root");
      fChain.Add("Samp_tree_1069.root"); fChain.Add("Samp_tree_1070.root"); fChain.Add("Samp_tree_1071.root");
      fChain.Add("Samp_tree_1072.root"); fChain.Add("Samp_tree_1073.root"); fChain.Add("Samp_tree_1074.root"); 
      fChain.Add("Samp_tree_1075.root"); fChain.Add("Samp_tree_1076.root"); fChain.Add("Samp_tree_1077.root");
      fChain.Add("Samp_tree_1078.root"); fChain.Add("Samp_tree_1079.root"); fChain.Add("Samp_tree_1080.root");
      fChain.Add("Samp_tree_1081.root"); fChain.Add("Samp_tree_1082.root"); fChain.Add("Samp_tree_1083.root"); 
      fChain.Add("Samp_tree_1084.root"); fChain.Add("Samp_tree_1085.root"); fChain.Add("Samp_tree_1086.root");
      fChain.Add("Samp_tree_1087.root"); fChain.Add("Samp_tree_1088.root"); fChain.Add("Samp_tree_1089.root");
      fChain.Add("Samp_tree_1090.root"); fChain.Add("Samp_tree_1091.root"); fChain.Add("Samp_tree_1092.root"); 
      fChain.Add("Samp_tree_1093.root"); fChain.Add("Samp_tree_1094.root"); fChain.Add("Samp_tree_1095.root");
      fChain.Add("Samp_tree_1096.root"); fChain.Add("Samp_tree_1097.root"); fChain.Add("Samp_tree_1098.root");  
            
// 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<string> *StepFinLogVol;//=0;
	std::vector<string> *StepInitLogVol;//=0;

// 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_FinalKineticEnergy",&StepFinKinEn);
  fChain.SetBranchAddress("Step_InitialKineticEnergy",&StepInKinEn);
  fChain.SetBranchAddress("Step_FinalLogicalVolume", &StepFinLogVol);
  fChain.SetBranchAddress("Step_InitialLogicalVolume", &StepInitLogVol);

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

  TFile *file = new TFile("hist.root", "CREATE")
  int nbins = 400;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  TH1F *hist = new TH1F("hist", "hist", nbins, 0., 0.0455);
   TH1F *hist = new TH1F file->Get("hist")
  

	Long64_t nbytes = 0, nb = 0;
	n_entries=fChain.GetEntries();
	for (Int_t j=0; j<n_entries;j++){ //run in all tree //////fecha linha 174
		
    		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;

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

			while (StepFinLogVol->at(contador) !="control" && contador < StepFinLogVol->size()-1){ /// fecha linha 133
				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 143
				
				for(Int_t l=0;l<StepNumber->size();l++){ /// fecha linha 140
					sum = sum + StepAcEnLost->at(l);
				} 
				//hist -> Fill(sum); //enter in control
				//hist -> Fill(StepAcEnLost->at(contador));
				//hist -> Fill(EventAEnerLost); //deu certo!! 
			}

			if (StepFinLogVol->at(contador) =="control") {
				mm++; 
				cout<<"cont = "<<contador<<endl; 	
				hist -> Fill(StepFinKinEn->at(contador));
			}
			
			cout<<endl;
                             
		}

//		
//			for (Int_t k=0;k<StepEventID->size();k++){ //run all vector in the event /// fecha na linha 174
//			
//				if(StepFinLogVol->at(k) != "control"){ //StepFinLogVol->at(j) == "control" ///fecha linha 172
					
								
			//	cout<<"StepEventID = "<<StepEventID->at(j)<<endl;
			//	cout<<"StepEventID = "<<StepEventID->size()<<endl;
			//	cout<<"StepFinKinEn = "<<StepFinKinEn->at(j)<<endl;
			//	cout<<"StepInKinEn(Val) = "<<StepInKinEn->at(j)<<endl;
					//cout<<"k = "<<k<<endl;
			//	cout<<"StepFinKinEn1 = "<<StepFinKinEn->size()<<endl;
			//	cout<<"Vol_Val = "<<StepFinLogVol->size()<<endl;
			//	cout<<"Vol_Val = "<<StepFinLogVol->at(j)<<endl;
						//cout<<"StepNumber = "<<StepNumber->at(j)<<endl;
						//cout<<endl;
			
					//hist ->Fill(EventAEnerLost); 				
				//	hist ->Fill(StepFinKinEn->at(k)); 
//					hist ->Fill(StepAcEnLoschain.at(k));              
//				}
//			
//			}
				
	//} 
	
//hist ->Fill(StepInKinEn->at(mm));
//hist ->Fill(sum); //enter in control
  //}
} 
cout<< "numero de partículas no controlo =  "<< mm<<endl;

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

}

I want to save the histogram (in this case StepFinKinEn) in a file, am I doing well?

Thnaks

you are missing

file->Write();

at the end.

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