Trying to get each evenement independently

Dear @KillianUB ,

The RDataFrame API expects you use columns available in your dataset. Are you sure you have the column named x in your dataset?

Cheers,
Vincenzo

No i don’t have column named x in my datatset. I’m sorry some problem are solved before you answered.
Now i’m trying to get the data for one event, i’m using something like :

// Index de l’événement que vous souhaitez accéder
Long64_t eventIndex = 0; // Remplacez 0 par l’index de l’événement que vous souhaitez accéder

// Accédez à l’événement spécifique dans l’arbre
tv__tree->GetEntry(eventIndex);

// Accédez aux données du détecteur 1 pour cet événement
Float_t psdValue = tv__tree->Psd_All_Shift[30]; // Remplacez 30 par l’indice du détecteur que vous souhaitez accéder

// Faites quelque chose avec les données du détecteur 1 pour cet événement, par exemple, affichez-le
std::cout << "Valeur PSD pour le détecteur 1 pour l’événement " << eventIndex << " : " << psdValue << std::endl;

and it returns me

This is my new problem to solve !

I forgot to mention that this code ;

TH1 histogram = (TH1)gPad->GetPrimitive(“h1”);

// Vérifiez si l’histogramme existe
if (histogram) {
// Accédez aux données de l’histogramme
int nbins = histogram->GetNbinsX();
for (int i = 1; i <= nbins; ++i) {
double binContent = histogram->GetBinContent(i);
// Faites quelque chose avec le contenu du bin, par exemple, l’afficher
std::cout << "Contenu du bin " << i << " : " << binContent << std::endl;
}
} else {
std::cout << “L’histogramme n’a pas été trouvé.” << std::endl;
}

Give me the number of event per pin, wish is a great step, but now i want the same thing but event per event.

Dear @KillianUB ,

Float_t psdValue = tv__tree->Psd_All_Shift[30];

Where have you seen this syntax? The tv__tree variable, which is a TTree* does not have a Psd_All_Shift method (as the very informative error suggests). What makes you think that the code you are writing is valid? Could you maybe take a further look at the TTree documentation and tutorials?

Cheers,
Vincenzo

I’m asking chat gpt, of cours the code given by chat gpt are not good but sometime it gives me the path to follow.

Dear @KillianUB ,

You are not that far from the solution. You can traverse the entries of the tree using GetEntries() as the condition to stop the for loop, then calling tree->GetEntry(i) inside the for loop. There, you have to properly set the memory address of a certain variable, call it maybe psdValue as in your previous example, which needs to be populated by the value of the branch Psd_All_Shift with the correct index. Traverse all the indices of however many detectors your application has within the same event, you can store the values in a vector for each event.

Cheers,
Vincenzo

That would be amazing if i could get it tonigth ! I’ll keep going, thx a lot for all your help, i think this forum topic could become an entire tutorial.

I tryed this and the result was kinda funny :

tv__tree->Draw(“Psd_All_Shift[1]>>h1(7500,500,8000)”,“”,“colz”);
for (int i =1; i<= nbins ; ++i ) {
double binContent = GetEntry(i);
std::cout << "Contenu du bin " << i << " : " << binContent << std::endl;
}

How ever i’d like to proced step by step, now that i can get my number of event bin per bin for one detctor, i’d like to get the same thing but divided by event like the exact same thing but for each event, i know what i want, you gave me the tools, but i don’t know how to do.

Dear @KillianUB ,

I am glad that you are appreciating the learning value of this experience. As I previously mentioned, there are many tutorials representing already what you are doing, as well as similar questions on the forum. You have all the ingredients necessary to reach your solution, as follows:

  • The name of the dataset and the name of the file, so you can get a TTree object in your application.
  • The number of entries in the tree, that is entries = tree->GetEntries()
  • Traversing the entries in the tree via a for-loop, using the number of entries as the condition and calling tree->GetEntry(i) inside the loop
  • Populating the value of what you are trying to get from the tree at every event. You need to properly set the branch address before the loop for this to work.
  • Storing N different values at each iteration of this for loop, one for every detector element you need.

You can see an example in the manual. Keep in mind that using TTree::Draw is orthogonal to what you are trying to do, it’s not necessary in this case.

Cheers,
Vincenzo

Hello,

Here is what I’ve done for now :

TFile f{“RAW148OS_treeFF01.root”};
std::unique_ptr tv__tree{f.Get(“Analysis_All”)};
tv__tree->Draw(“Psd_All_Shift[1]>>h1(7500,500,8000)”,“”,“colz”);
TH1 histogram = (TH1)gPad->GetPrimitive(“h1”);

// Vérifiez si l’histogramme existe
if (histogram) {
// Accédez aux données de l’histogramme
int nbins = histogram->GetNbinsX();
int nevent = tv__tree->GetEntries();
for (int j = 0; j < nevent; ++j) {
tv__tree->GetEntry(j); // Accédez à l’événement actuel
for (int i = 1; i <= nbins; ++i) {
double binContent = histogram->GetBinContent(i);
// Faites quelque chose avec le contenu du bin, par exemple, l’afficher
std::cout << "Contenu du bin " << i << " pour l’événement " << j << " : " << binContent << std::endl;
}
}
} else {
std::cout << “L’histogramme n’a pas été trouvé.” << std::endl;
}

I just realised that the commande GetEntry() always return me the same data (int) 1690 what i don’t really understand. So when I run this code, I loop on every events for every pins but the number of events per pins is always the same and it’s not 0 or 1.

Here you can see what GetEntry returns; it’s not what you seem to think.
Have a look at the TTree tutorials to see how to create and read them. For example, check out tree1.C, in particular the function tree1r(), where you can see how to read and use variables (branches) from the tree event by event. You need to use SetBranchAddress (unless you use dataframes, as Vincenzo suggested at the beginning, but you are doing something else).

TFile f{“RAW148OS_treeFF01.root”};
std::unique_ptr tv__tree{f.Get(“Analysis_All”)};
tv__tree->Draw(“Psd_All_Shift>>h1(7500,500,8000)”,“”,“colz”);
TH1 histogram = (TH1)gPad->GetPrimitive(“h1”);

// Vérifiez si l’histogramme existe
if (histogram) {
// Accédez aux données de l’histogramme
int nevent = 1; //on définit le nombre d’évenement
int n = 0;
for (int j = 0; j < nevent; ++j) {
tv__tree->Show(j);
tv__tree->GetEntry(j); // Accédez à l’événement actuel
for (int i = 1; i <= nbins; ++i) {
tv__tree->SetBranchAdress(“Psd_All_Shift[i]”, &Psd_All_Shift);
double branchContent = Psd_All_Shift;
if (branchContent < 500) {
n = n;
} else {
n = n + 1 ;
}
return n;

        // Faites quelque chose avec le contenu du bin, par exemple, l'afficher
        std::cout << "Contenu du bin " << i << " pour l'événement " << j << " : " << branchContent << std::endl;
    }
}

} else {
std::cout << “L’histogramme n’a pas été trouvé.” << std::endl;
}

I tried to creat a code allowing me to navigate intoo event and get the result for only one branch. Her’s what i writed, it obviously dosnt work, but i hope you’ll understand the idea.