Draw from text file the difference of two value

greetings;
I hope you’re doing well, I encountered a problem with plotting from a text file as shown below (code). the text file contains 3 columns: x, dose 1, dose 2, and I want to draw the difference between (dose1 and dose2) can someone help:
@couet @Axel

TString doseFileSim = "differenceDose.txt";
    TNtuple *TNtupleSim = new TNtuple("ntuple1","dose from simulation file", "iX:doseSans:doseAvec");

    cout << "Reading from file \" " << doseFileSim << "\" ... ";
    Long64_t nlines = TNtupleSim -> ReadFile(doseFileSim, "iX:doseSans:doseAvec"); 
    if (nlines <=0){cout << "Error: Check  \"" << doseFileSim << "\"\n"; return;}
    printf("%lld Experimental points found\n", nlines);
        
    Float_t iX, doseSans,doseAvec;
    TNtupleSim -> SetBranchAddress("iX", &iX);
    TNtupleSim -> SetBranchAddress("doseSans", &doseSans);
    TNtupleSim -> SetBranchAddress("doseSans", &doseSans);  
    
    Int_t nentries = (Int_t)TNtupleSim -> GetEntries();   
    TNtupleSim -> GetEntry(0);
    Float_t maxDose = 0, depthSim = 0, slength = lengthBox/nentries;
    vector <Float_t> vec_doseSans,vec_doseAvec, vec_iX;

//------------------------------------------------------------------------deleted----------------------------------
for (Int_t l = 0; l<nentries; l++)
    {
      TNtupleSim -> GetEntry(l);
vec_iX.push_back(depthSim);

vec_doseSans.push_back(doseSans);
                               //vec_iX.push_back(depthSim); 
vec_doseAvec.push_back(doseAvec);
                                

depthSim += slength;
}
//----------------------------------------------------------------------deleted------------------------------------   


    TNtupleSim -> Reset(); 

//---------------------------------------------------------------------deleted-----------------------------------------
for (Int_t l = 0; l<nentries; l++){

iX = vec_iX[l];
doseSans = ((vec_doseAvec[l]-vec_doseSans[l])/(vec_doseSans[l]))*(-100);

TNtupleSim -> Fill(iX,0,0,doseSans);

}   

//---------------------------------------------------

  
    TCanvas *c1 = new TCanvas ("c1","c1",200,10,600,400);
     
    TNtupleSim-> SetMarkerStyle(20);
    TNtupleSim -> SetMarkerColor(4);
    TNtupleSim -> SetMarkerSize(0.7);
    
   //------------------------------------
    TNtupleSim ->  Draw("doseSans:iX","","");

If you just want to draw, consider using

cout << "Reading from file \" " << doseFileSim << "\" ... ";
Long64_t nlines = TNtupleSim -> ReadFile(doseFileSim, "iX:doseSans:doseAvec"); 
if (nlines <=0){cout << "Error: Check  \"" << doseFileSim << "\"\n"; return;}
printf("%lld Experimental points found\n", nlines);
TNtupleSim->Draw("doseSans-doseAvec");

@Axel it gives me like this

BraggPeakComparison

TNtupleSim->Draw("(100.*(doseSans-doseAvec))/doseAvec"); shouldn’t give you that plot - e.g. where do the axis titles come from? And that plot is likely of a different type - TH2 or TGraph; it doesn’t look like a histogram. Please make sure you really run that Draw invocation and not something “similar” where I need to guess what you actually did :slight_smile: Or share what you actually called - either way works for understanding what’s happening.

OK there we go - you draw vs iX and I wasn’t aware of that…

So, looks like doseSans == doseAvec for all iX? Can you post the output of TNtupleSim->Scan()?

See my other question:

@Axel you mean like this? yes i can

Perfect! So in the TTree, the doseSans and doseAvec are different. Good!

I see that in your macro you have

    Float_t iX, doseSans,doseAvec;
    TNtupleSim -> SetBranchAddress("iX", &iX);
    TNtupleSim -> SetBranchAddress("doseSans", &doseSans);
    TNtupleSim -> SetBranchAddress("doseSans", &doseSans);

Please make sure you’re setting doseAvec to its branch, and both contain the data you expect, e.g. by modifying your code as follows:

for (Int_t l = 0; l<nentries; l++)
    {
      TNtupleSim -> GetEntry(l);
      if (l < 10) std::cout << "iX: " << iX << " doseSans:" << doseSans << " doseAvec: " << doseAvec << '\n';
...

thank you very much now it’s clear i made a mistake in these lines

TNtupleSim -> SetBranchAddress(“doseSans”, &doseSans);
TNtupleSim -> SetBranchAddress(“doseSans”, &doseSans);

it works now thanks @Axel

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.