ROOT Version: 6.30/02
Platform: Ubuntu 22.04.3 LTS
Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Hi,
I have a macro where I want to get the charge of a particle from its pid. But my macro is giving the following error
In file included from input_line_8:1:
myCODE/CheckRootFile.C:70:19: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
charge = TDatabasePDG::Instance()->GetParticle(Pid)->Charge();
I checked my data files…It’s happening for Pid=42;
Regards
couet
January 16, 2024, 1:54pm
2
Welcome to the ROOT forum.
The line on wish you get the error looks correct.
Do you have a small code snippet reproducing the problem?
void CheckRootFile::Loop()
{
// In a ROOT session, you can do:
// root> .L CheckRootFile.C
//
if (fChain == 0) return;
Long64_t nentries = fChain->GetEntriesFast();
cout << nentries << endl;
Long64_t nbytes = 0, nb = 0;
//Event Loop
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
cout << jentry << endl;
//Particle Loop
for (int p1=0; p1<fNpa;p1++)
{
Int_t Pid= fParticles_fPdg[p1];
Double32_t Px= fParticles_fPx[p1];
Double32_t Py= fParticles_fPy[p1];
Double32_t Pz= fParticles_fPz[p1];
Double32_t E= fParticles_fE[p1];
// cout << jentry<<">>>>"<<Px << ":::::"<<Py<< ":::::" << Pz << ":::::" << E << endl;
Int_t charge=0;
Double_t mass=0;
charge = TDatabasePDG::Instance()->GetParticle(Pid)->Charge();
mass = TDatabasePDG::Instance()->GetParticle(Pid)->Mass();
cout << p1 <<"::::"<<Pid<<">>>>"<< "charge" << ":::::"<<mass<<endl;
}
//particle loop ends
// if (Cut(ientry) < 0) continue;
}//Event Loop ends
}//Main LOOP()
couet
January 16, 2024, 2:53pm
4
This code cannot be run.
May be you can try this on (it prints Pid):
void CheckRootFile::Loop()
{
// In a ROOT session, you can do:
// root> .L CheckRootFile.C
//
if (fChain == 0) return;
Long64_t nentries = fChain->GetEntriesFast();
cout << nentries << endl;
Long64_t nbytes = 0, nb = 0;
//Event Loop
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
cout << jentry << endl;
//Particle Loop
for (int p1=0; p1<fNpa;p1++) {
Int_t Pid = fParticles_fPdg[p1];
Double32_t Px = fParticles_fPx[p1];
Double32_t Py = fParticles_fPy[p1];
Double32_t Pz = fParticles_fPz[p1];
Double32_t E = fParticles_fE[p1];
Int_t charge = 0;
Double_t mass = 0;
cout << "Pid = " << Pid << endl;
charge = TDatabasePDG::Instance()->GetParticle(Pid)->Charge();
mass = TDatabasePDG::Instance()->GetParticle(Pid)->Mass();
cout << p1 <<"::::"<<Pid<<">>>>"<< "charge" << ":::::"<<mass<<endl;
}
}
}
And copy/paste here the output you get.
470::::3112>>>>charge:::::1.19745
Pid = -211
471::::-211>>>>charge:::::0.13957
Pid = 2112
472::::2112>>>>charge:::::0.939565
Pid = -211
473::::-211>>>>charge:::::0.13957
Pid = 111
474::::111>>>>charge:::::0.134977
Pid = 2212
475::::2212>>>>charge:::::0.938272
Pid = 321
476::::321>>>>charge:::::0.493677
Pid = 42
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
In file included from input_line_8:1:
/home/arshad/WorkSpace/LEARN/Centrality/myCODE/CheckRootFile.C:36:20: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
charge = TDatabasePDG::Instance()->GetParticle(Pid)->Charge();
Well, it looks like TDatabasePDG
has no entry for ID 42:
root [0] TDatabasePDG::Instance()->GetParticle(42)
(TParticlePDG *) nullptr
root [1]
Sor you should do something like:
auto particlePDG = TDatabasePDG::Instance()->GetParticle(Pid);
if (particlePDG)
charge = particlePDG->Charge();
...
Yes, Its Working Now
Thanks a lot!!!
1 Like