Home | News | Documentation | Download

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");

yes i want to draw the quantity: (100)*(doseSans-doseAvec)/(doseAvec). is it the same ??

@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.

@Axel
it’s gives me this plot but i don’t know where is the problem. Of course it’s not the complete program, here is the rest of this macro

TCanvas *c1 = new TCanvas ("c1","c1",200,10,600,400);
     
    TNtupleSim-> SetMarkerStyle(10);
    TNtupleSim -> SetMarkerColor(4);
    TNtupleSim -> SetMarkerSize(0.7);
    
   //------------------------------------
    TNtupleSim ->  Draw("(-100)*(doseAvec-doseSans)/doseSans:iX","","");
    TH2F *htemp = (TH2F*)gPad->GetPrimitive("htemp");
    TAxis *xaxis = htemp->GetXaxis();
    TAxis *yaxis = htemp->GetYaxis();
    xaxis->SetTitle("Depth [mm]");
    yaxis->SetTitle("PercentageOfDecreasingDose [%]");
    htemp -> SetTitle("\t \tDose");
    
    // Legend
    leg = new TLegend(0.50,0.60,0.20,0.70); 
    leg -> SetTextSize(0.04);
    leg -> SetFillColor(0);
    leg -> AddEntry(TNtupleSim, "Geant4, D", "P");
    leg -> Draw();

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()?

@Axel no you can check it if you want the text file below

differenceDose.txt (7.5 KB)

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