Problem reading data from root file

Hi,

the individual trees contain separate branches for each detectors, so if you have a single detector, you expect one branch in the tree. You can read the entries like this:

$ root -l data_1.root
root [1] .L ~/path/to/your/allpix-squared/lib/libAllpixObjects.so
root [2] .L analyzeData.C+
root [3] readTree(_file0, "detector1")

with the following ROOT macro, stored as analyzeData.C - you have to adapt the location of the header int he file:

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

// FIXME: dapt path to the include file of your APSQ installation
#include "/path/to/your/allpix-squared/src/objects/DepositedCharge.hpp"

/**
 * Read ROOT TTree from the data objects
 */
void readTree(TFile* file, std::string detector) {

    // Read tree of deposited charges:
    TTree* deposited_tree = static_cast<TTree*>(file->Get("DepositedCharge"));
    if(!deposited_tree) {
        std::cout << "Could not read tree DepositedCharge, cannot continue." << std::endl;
        return;
    }

    TBranch* deposited_branch = deposited_tree->FindBranch(detector.c_str());
    if(!deposited_branch) {
        std::cout << "Could not find the detector branch on tree DepositedCharge, cannot continue." << std::endl;
        return;
    }

    std::vector<allpix::DepositedCharge*> deposited_charges;
    deposited_branch->SetObject(&deposited_charges);

    // Go through the tree event-by-event:
    for(int i = 0; i < deposited_tree->GetEntries(); ++i) {
        deposited_tree->GetEntry(i);

	// Loop over all deposited charges
        for(auto& deposited_charge : deposited_charges) {
	    std::cout << "Event " << i << ": charge = " << deposited_charge->getCharge()
	              << ", position = " << deposited_charge->getGlobalPosition() << std::endl;
        }
    }

    return;
}

There is a longer example in the repository at tools/root_analysis_macro, youc an find a description of it in the user manual.

Let me know if that works for you!

/Simon

2 Likes