Message: Object (NTuple) dissapear in function Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

Question Object (NTuple) dissapear in function 

Forum: Analysis
Date: 21 Apr, 2015
From: Carlos Ayerbe Gayoso <Carlos Ayerbe Gayoso>

Dear Geanters

Today I come with a very simple (in principle) issue. I took example ExAna02 for my application. The issue comes from the HistoManager class. The object fNtuple1 is created, and apparently successfully, but sadly it disappear in the method where is called.

In the code below you can see that fNtuple1 is created in book() method. I introduced a small check with an if(fNutple1) to see if it exists. I did the same in all methods, including in the destructor of the class and it exists EXCEPT where is needed, in FillNtuple(..), there the object fNtuple doesn't exist. The values passed to the function FillNtuple(..) are there (you can see the comprobation lines to screen), is the fNtuple1 object which fails (perhaps)

Honestly, I don't know where is the problem.

Any suggestion is welcome.

Thank you

Carlos

------>HistoManager.cc<----------

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

#include <TH1D.h>
#include <TFile.h>
#include <TTree.h>
#include <CLHEP/Units/SystemOfUnits.h>
#include "G4SystemOfUnits.hh"
#include "HistoManager.hh"
#include "G4UnitsTable.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 HistoManager::HistoManager():fHisto(0),fNtuple1(0)
{
  fEvent = 0;
  fPDGid  = 0;
  fparentID =  0;
  fEnergyTile = 0;
  fSciTileNo = 0;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 HistoManager::~HistoManager()
{
  if ( fRootFile ) delete fRootFile;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 void HistoManager::book()
{ 

// Creating a tree container to handle histograms and ntuples.
// This tree is associated to an output file.
//
 G4cout<<"HistoManager::book()"<<G4endl;

 G4String fileName = "AnaEx02.root";
 fRootFile = new TFile(fileName,"RECREATE");
  if(!fRootFile) {
   G4cout << " HistoManager::book :" 
          << " problem creating the ROOT TFile "
          << G4endl;
   return;
 }

 fHisto = new TH1D("1", "Edep in Crystal", 150, -0.5, 15.5*CLHEP::MeV);
 if (!fHisto) G4cout << "\n can't create histo 1" << G4endl;

 // create 1st ntuple in subdirectory "tuples"
 //

 fNtuple1 = new TTree("Alpom", "Target");
 // // fNtuple1->Branch("Events", &fEvent, "event/I:PDGid/I:parentID/I:EnergyTile/D:SciTileNo/I");

 fNtuple1->Branch("Events", &fEvent, "event/I");
 fNtuple1->Branch("PDGid", &fPDGid,"PDGid/I");
 fNtuple1->Branch("parentID", &fparentID,"parentID/I");
 fNtuple1->Branch("EnergyTile", &fEnergyTile,"EnergyTile/D");
 fNtuple1->Branch("SciTileNo", &fSciTileNo,"SciTileNo/I");

 if (fNtuple1) 
    {
      G4cout<<"I LIVE!! (1)"<<G4endl;
    }

 G4cout << "\n----> Histogram file is opened in " << fileName << G4endl;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 void HistoManager::FillNtuple(G4int evt, 
			      G4int PDGID, 
			      G4int pID, 
			      G4double edep, 
			      G4int Tile)
 {
 if (fNtuple1) 
   {
     G4cout<<"I LIVE!! (2)"<<G4endl;
   }

  fEvent = evt;
  fPDGid  = PDGID;
  fparentID =  pID;
  fEnergyTile = edep;
  fSciTileNo = Tile;

  G4cout<<"event: "<< fEvent<<G4endl; 
  G4cout<<"parentID: "<<fparentID<<G4endl; 
  G4cout<<"PDGID: "<<fPDGid<<G4endl; 
  G4cout<<"Energy: "<<fEnergyTile/MeV<<" Tile: "<<fSciTileNo<<G4endl;
  G4cout<<"__________ "<<G4endl;

  if (fNtuple1) 
    {
      G4cout<<"FILLING!!"<<G4endl;
      fNtuple1->Fill();
    } 

}

 void HistoManager::save()
{ 
  if (fRootFile) {
    fRootFile->Write();       // Writing the histograms to the file
    fRootFile->Close();        // and closing the tree (and the file)
    G4cout << "\n----> Histogram Tree is saved \n" << G4endl;
  }
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
{
  // if (ih >= MaxHisto) {
  //   G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih
  //          << " does not exist. (xbin=" << xbin << " weight=" << weight << ")"
  //          << G4endl;
  //   return;
  // }
 if  (fHisto) { fHisto->Fill(xbin, weight); }

}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 void HistoManager::PrintStatistic()
{
  if(fHisto) {
    G4cout << "\n ----> print histograms statistic \n" << G4endl;

    G4cout 
    << " EAbs : mean = " << G4BestUnit(fHisto->GetMean(), "Energy") 
            << " rms = " << G4BestUnit(fHisto->GetRMS(),  "Energy") << G4endl;
  }
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

------>HistoManager.hh<----------

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

#ifndef HistoManager_h
#define HistoManager_h 1

#include "globals.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

 class TFile;
 class TTree;
 class TH1D;

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

class HistoManager
{
  public:

    HistoManager();
   ~HistoManager();

    void book();
    void save();

    void FillHisto(G4int id, G4double bin, G4double weight = 1.0);

    void FillNtuple(G4int evt, 
		    G4int PDGID, 
		    G4int pID, 
		    G4double edep, 
		    G4int Tile);

    void PrintStatistic();

  private:

    TFile*   fRootFile;
    TH1D*    fHisto;            
    TTree*   fNtuple1;

  G4int fEvent;
  G4int fPDGid;
  G4int fparentID;
  G4double fEnergyTile;
  G4int fSciTileNo;

};

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

#endif

Inline Depth:
 1 1
 All All
Outline Depth:
 1 1
 2 2
 All All
Add message: (add)

1 None: Re: Object (NTuple) dissapear in function   (Ivana Hrivnacova - 28 Apr, 2015)
(_ None: Re: Object (NTuple) dissapear in function   (Carlos Ayerbe Gayoso - 01 May, 2015)
 Add Message Add Message
to: "Object (NTuple) dissapear in function"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]