Hello,
I have a problem with RootFile. I work on a simulation of a detector with Geant4
and this code return some of data and I put there in a TH2D and in a TGraph. When simulation is done,
I write my result in a RootFile. When I open this later, the TH2D plot anything and the TGraph is empty.
I used the following code .
#include “G4ParticleTable.hh”
#include “G4ParticleDefinition.hh”
#include <TFile.h>
#include <TTree.h>
#include <TParticle.h>
#include <TGraph.h>
#include <TCanvas.h>
PlanckRootFile::PlanckRootFile() {
rootFile = new TFile(RootFileName,"RECREATE");
hXYGlobal = new TH2D("hXYGlobal","",(int)(2.2*trackerSizeX/mm),-(int)(1.1*trackerSizeX),(int)(1.1*trackerSizeX),(int)(2.2*trackerSizeY/mm),-(int)(1.1*trackerSizeY),(int)(1.1*trackerSizeY));
hXZGlobal = new TH2D("hXZGlobal","",(int)(2.2*trackerSizeX/mm),-(int)(1.1*trackerSizeX),(int)(1.1*trackerSizeX),(int)(2.2*trackerSizeZ/mm),-(int)(1.1*trackerSizeZ),(int)(1.1*trackerSizeZ));
hTraceEnergyElec = new
TH2D(“hTraceEnergieElec”,"",(int)(1.1eMax),(int)(-4eMax),(int)(4eMax),(int)(2.2trackerSizeZ/mm),-(int)(1.1trackerSizeZ/mm),(int)(11trackerSizeZ/mm));
hTraceEnergyIon = new
TH2D(“hTraceEnergieIon”,"",(int)(1.1eMax),(int)(-4eMax),(int)(4eMax),(int)(2.2trackerSizeZ/mm),-(int)(1.1trackerSizeZ/mm),(int)(50trackerSizeZ/mm));
gTraceEnergyIon = new TGraph();
gTraceEnergyElec = new TGraph();
C1 = new TCanvas("C1","Graph",200,10,700,500);
}
PlanckRootFile::~PlanckRootFile()
{
gTraceEnergyIon->Draw(“AP”);
gTraceEnergyElec->Draw(“AP”);
hNbHitIncidentPart -> Write();
hNHitTot -> Write();
hEDepIncidentPart -> Write();
hEDepPrimaryElec -> Write();
hEDepPrimaryIon -> Write();
hXZGlobal -> Write();
hXYGlobal -> Write();
hTraceEnergyElec -> Write();
hTraceEnergyIon -> Write();
gTraceEnergyIon -> Write();
gTraceEnergyElec -> Write();
C1 -> Write();
rootTree -> Write();
rootFile -> Close();
outputTxtFileroot.close();
G4cout<<"Data Write in the root file"<<G4endl;
}
void PlanckRootFile::FillHistosEndOfEvent(PlanckTrackerHitsCollection * trackerCollection){
for (G4int i=0;i<NbHits;i++)
{
processId = (*trackerCollection)[i]->GetProcessType();
eHit = (*trackerCollection)[i]->GetEdep()/keV;
eOut = (*trackerCollection)[i]->GetOutcomingE()/keV;
if(eHit > 0) nHits++;
G4ThreeVector vec = (*trackerCollection)[i]->GetPos();
posX = vec.x()/cm;
posY = vec.y()/cm;
posZ = vec.z()/cm;
//There are a macro for give EDepIncidentPart, EDepPrimaryElec and length
if( EDepIncidentPart > 0 ) hEDepIncidentPart->Fill((double)EDepIncidentPart);
if( EDepPrimaryElec > 0 ) hEDepPrimaryElec->Fill((double)EDepPrimaryElec);
if( EDepPrimaryIon > 0 )
{
EDepPrimaryIonDouble =(Double_t) EDepPrimaryIon;
lengthDouble = (Double_t)length;
hEDepPrimaryIon->Fill(EDepPrimaryIonDouble);
gTraceEnergyIon->SetPoint(NbPointIon,EDepPrimaryIonDouble,lengthDouble);
hTraceEnergyIon -> Fill(EDepPrimaryIonDouble,lengthDouble);
NbPointIon++;
outputTxtFileroot<<tree_NEvent<<" "<<EDepPrimaryIon<<" "<<length<<endl;
if(processIdEvent < 56 && processIdEvent >50 ) outputTxtFilerootPROTONS<<tree_NEvent<<" "<<EDepPrimaryIon<<" "<<length<<endl;
else outputTxtFilerootIONS<<tree_NEvent<<" "<<EDepPrimaryIon<<" "<<length<<endl;
}
if( EDepPrimaryElec > 0 )
{
EDepPrimaryElecDouble = (Double_t)EDepPrimaryElec;
lengthDouble = (Double_t)length;
NbPointElec++;
hTraceEnergyElec -> Fill(EDepPrimaryElecDouble,lengthDouble);
}
}