#include "TFile.h" #include "TTree.h" #include "TRandom.h" #include "TCanvas.h" #include "TH1F.h" struct Ray { double Hag[16]; double NaI[10]; double Ge[4]; double beta; int cycle; }; void Write(const char* filename) { //Create a file for testing. TFile *f = new TFile(filename,"RECREATE"); TTree *t = new TTree("t","Tree"); //Make an instance of the struct Ray calgam; printf("\nCreating a branch with a leaf list.\n"); //Create a new branch with a leaf list. t->Branch("brLeafList",&calgam,"Hag[16]/D:NaI[10]:Ge[4]:beta:cycle/i"); #ifndef __CINT__ printf("Creating a branch with no leaf list.\n"); //Create a new branch using the struct. t->Branch("brNoLeafList",&calgam); #endif printf("\n"); //Fill some of the struct and the tree. for (int i=0;i<100;i++) { calgam.cycle = i; calgam.Ge[0] = gRandom->Gaus(100,0.1); t->Fill(); } t->ResetBranchAddresses(); //Write the file. f->Write(); f->Close(); } void Read( const char* filename) { TCanvas *c = new TCanvas("c","Canvas"); c->Divide(2,1); TFile *f = new TFile(filename); TTree *t = (TTree*)f->Get("t");; #ifndef __CINT__ Ray *calgam = nullptr; #else Ray *calgam = NULL; #endif //Try working on branch with no leaf list printf("\nTrying to SetBranchAddress for branch with no leaf list, 'brNoLeafList'.\n"); printf("Trying to SetBranchAddress without void* casting.\n"); //This is where the error is. int retVal = t->SetBranchAddress("brNoLeafList",&calgam); if (retVal == 0) printf("Success\n"); else if (retVal > 0) printf("Successful, but not a perfect match. retVal = %d\n",retVal); else { printf("Failed! retVal = %d\n",retVal); printf("Trying to SetBranchAddress with void* casting.\n"); //This resolves the error, by negating the error checking. retVal = t->SetBranchAddress("brNoLeafList",(void*)&calgam); if (retVal == 0) printf("Success\n"); else if (retVal > 0) printf("Successful, but not a perfect match. retVal = %d \n",retVal); } c->cd(1); TH1F *hNoLeafList = new TH1F("hNoLeafList","hNoLeafList",100,0,100); hNoLeafList->SetDirectory(0); if (retVal >= 0) { for (int entry=0;entry < t->GetEntries(); entry++) { t->GetEntry(entry); hNoLeafList->Fill(calgam->cycle); } hNoLeafList->Draw(); } Ray calgamLeafList; //Try working on branch with no leaf list printf("\nTrying to SetBranchAddress for branch with leaf list, 'brLeafList'.\n"); printf("Trying to SetBranchAddress without void* casting.\n"); //This is where the error is. int retValLeafList = t->SetBranchAddress("brLeafList",&calgamLeafList); if (retValLeafList == 0) printf("Success\n"); else if (retValLeafList > 0) printf("Successful, but not a perfect match. retVal = %d\n",retValLeafList); else { printf("Failed! retVal = %d\n",retValLeafList); printf("Trying to SetBranchAddress with void* casting.\n"); //This resolves the error, by negating the error checking. retValLeafList = t->SetBranchAddress("brLeafList",(void*)&calgamLeafList); if (retValLeafList == 0) printf("Success\n"); else if (retValLeafList > 0) printf("Successful, but not a perfect match. retVal = %d \n",retValLeafList); } c->cd(2); TH1F *hLeafList = new TH1F("hleafList","hLeafList",100,0,100); hLeafList->SetDirectory(0); if (retValLeafList >= 0) { for (int entry=0;entry < t->GetEntries(); entry++) { t->GetEntry(entry); hLeafList->Fill(calgamLeafList.cycle); } hLeafList->Draw(); } t->ResetBranchAddresses(); f->Close(); } void SetBranchAddressTest() { Write("test.root"); Read("test.root"); }