Storing C Struct in Tree

ROOT Version : 6.26
Platform : Debian 11
Compiler : GCC 10.2

#include <iostream>
#include "TFile.h"
#include "TTree.h"

struct Event{
  int NParticle;
  double b, PsiEP;
};

int main(int argc, char **argv){
	TFile File("test.root", "recreate");
	TTree Tree("Events", "Event Info");

	Event E;
	Tree.Branch("Event", &E, "NParticle/I:b/D:PsiEP/D");

	E = {10, 0.5, -0.5};
	Tree.Fill();

	File.Write();
	File.Close();
	return 0;
}

Not getting stored in test.root. Am I missing something here ? What is the correct
procedure to store C-like structs in a Tree ? (Apart from creating branches for each
member variable separately :smiley: )

When using the leaflist way of creating branch you must order your data member in decrease order of size (and list them in the comment the same order):

struct Event{
  double b, PsiEP;
  int NParticle;
};

You are usually better off generating and loading a “dictionary” for the struct. See I/O of custom classes - ROOT or use gInterpreter->GenerateDictionary("Event", "Event.h");