Reading output.root file error

I’m getting these errors while running these code snippets and I’m not able to solve these.
I’ve uploaded the files below

#include "run.hh"
#include "G4Run.hh"
#include "G4SDManager.hh"
#include "G4HCofThisEvent.hh"
#include "G4Event.hh"
//#include "G4ios.hh"
#include "G4SystemOfUnits.hh"
#include "TFile.h"
#include "TNtuple.h"

MyRunAction::MyRunAction()
{
    
}

MyRunAction::~MyRunAction()
{}

void MyRunAction::BeginOfRunAction(const G4Run* run)
{

    //G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();

    //int runID = run->GetRunID();

    //d::stringstream strRunID;
    //rRunID << runID;
    //analysisManager->OpenFile("output.root");
   //analysisManager->OpenFile("output" + strRunID.str() + ".root");

    TFile* rootFile = new TFile("output.root", "RECREATE");
    TNtuple* ntuple = new TNtuple("trajectory", "Proton trajectory", "x:y:z");
}
  

void MyRunAction::EndOfRunAction(const G4Run*)
{
    // Close the ROOT file
    TFile* rootFile = TFile::Open("output.root", "UPDATE");
    rootFile->Write();
    rootFile->Close();

}
/*
 void MyRunAction::AddEventInfo(G4int eventID, G4double edep, G4ThreeVector position)
{
    fTotalEdep += edep;
    fPositions.push_back(position);
    fEventIDs.push_back(eventID);
}
*/
#include "event.hh"
#include "run.hh"

#include "G4Event.hh"
#include "G4EventManager.hh"
#include "CLHEP/Units/SystemOfUnits.h"
#include "G4AnalysisManager.hh"


#include "G4SDManager.hh"
#include "G4HCofThisEvent.hh"
#include "G4ThreeVector.hh"
#include "TFile.h"
#include "TNtuple.h"
#include "G4SDManager.hh"
#include "G4ThreeVector.hh"
#include "G4PrimaryVertex.hh"
#include "G4PrimaryParticle.hh"
#include "G4Track.hh"
#include "G4RunManager.hh"
#include "G4Track.hh"
#include "G4Step.hh"
#include "G4SystemOfUnits.hh"
#include "G4VProcess.hh"
#include "G4VTrajectoryPoint.hh"
/*
* MyEventAction::MyEventAction(MyRunAction* runAction) : G4UserEventAction(),
fRunAction(runAction),
fEdep(0.)
{
    //fEdep = 0.;
}
*/
MyEventAction::MyEventAction()
{}


MyEventAction::~MyEventAction()
{}

void MyEventAction::BeginOfEventAction(const G4Event*)
{
    // fEdep = 0.;
     //G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
     //analysisManager->OpenFile("output.root");
 
}

void MyEventAction::EndOfEventAction(const G4Event* event)
{ 
    // Retrieve the proton trajectory data
    G4TrajectoryContainer* trajectoryContainer = event->GetTrajectoryContainer();
    if (!trajectoryContainer) return;

    G4int n_trajectories = trajectoryContainer->entries();
    if (n_trajectories == 0) return;


    G4VTrajectory* trajectory = (*trajectoryContainer)[n_trajectories - 1];

    G4int n_points = trajectory->GetPointEntries();
    if (n_points == 0) return;
    G4ThreeVector position = trajectory->GetPoint(n_points - 1)->GetPosition();
    //fRunAction->AddEventInfo(eventID, fEdep, position);
   
    
    TFile* rootFile = TFile::Open("output.root", "UPDATE");
    TNtuple* ntuple = (TNtuple*)rootFile->Get("trajectory");
    ntuple->Fill(position.x(), position.y(), position.z());
    rootFile->Write();
    rootFile->Close();
  
}
/*
* 
* void MyEventAction::AddEdep(G4double edep)
{
    fEdep += edep;
}
*/

Error in TFile::ReadBuffer: error reading all requested bytes from file output.root, got 218 of 300
Error in TFile::Init: output.root failed to read the file type data.

ROOT Version: root_v6.28.00
Platform: Windows 10
Compiler: Microsoft Visual Studio


These were two different classes. Since I was not able to upload them as files so I uploaded the code

Hi @Deeksha_Singh ,

In this function you are opening a file but never closing it. This leads to errors, never do this. My intuition tells me that in the following function you attempt at closing the same file you created before

But still, you are at least leaking memory and thus opening the door to all sorts of problems. Refrain from this pattern please. Use std::unique_ptr instead of raw pointers everywhere. If you need to keep that TFile around during your application, store it as a std::unique_ptr data member of your class.

Cheers,
Vincenzo

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