TLatex - Combine a string and char*

Hi all,

I’m using the TLatex class to make a label, but I’d like it to read “Average” [string] with only one TLatex. The label is in the bottom right.

Thanks!

Here’s what I have so far.

Relevant code:

 char average[64];
  int ret = snprintf(average, sizeof average, "%f", avg);

if (ret < 0) {
    return EXIT_FAILURE;
}
if (ret >= sizeof average) {
    //Result was truncated - resize the buffer and retry.
}

TLatex *avg2 = new TLatex(0.94,0.01, average);
  avg2->SetNDC();
  avg2->SetTextSize(.015);
  avg2->SetLineWidth(2);
  avg2->Draw();
  title->SetFillColor(32);
  title->Draw();

  TLatex *avg3 = new TLatex(0.87,0.01,"Average:");
  avg3->SetNDC();
  avg3->SetTextSize(.015);
  avg3->SetLineWidth(2);
  avg3->Draw();
  title->SetFillColor(32);
  title->Draw();

All code:

namespace {

Double_t my_transfer_function(const Double_t *x, const Double_t * /*param*/)
{
   // Bin values in our example range from -2 to 1.
   // Let's make values from -2. to -1.5 more transparent:
  if (*x>-.0001 && *x<.0001)
      return 0.001;
   return 1.;
}
}

void glgraph(){

int data = 0;
   
ifstream file { "5603_bubbles.txt" };
if (!file.is_open()) return -1;

double transArray [200][4]{};
 
for (int i{}; i != 200; ++i) {
    for (int j{}; j != 4; ++j) {
        file >> transArray[i][j];
        
    }
}

  float avg = 0.0; 
  float sum = 0.0;
  int size;

  for (int i{}; i != 200; ++i) {
    for (int j{}; j != 4; ++j) {
    size = sizeof(transArray) / sizeof(transArray[i][j]     
 );
  sum += transArray[i][j];
    }
  }
  avg = ((float)sum)/size;

  double dev[size][4];

  cout << "Average Optical Transmittance  " << avg << "\n";

  char average[64];
  int ret = snprintf(average, sizeof average, "%f", avg);

if (ret < 0) {
    return EXIT_FAILURE;
}
if (ret >= sizeof average) {
    //Result was truncated - resize the buffer and retry.
}
  
 for (int i{}; i != 195; ++i) {
    for (int j{}; j != 4; ++j) {
      dev[i][j]= transArray[i][j] - avg;
    }}

 
 TH3F *hist = new TH3F("glvoxel", "OT", (size/4), 0., (size/4), 20, 0., 20., 20, 0., 20.);

   for (UInt_t i = 0; i < 200; ++i) {
     for (Int_t j = 1; j< 19;j++){
       hist->Fill(i, j, 0., dev[i][0]); //Side 1
       hist->Fill(i, 19.9, j, dev[i][1]); //Side 2
       hist->Fill(i, j, 19.9, dev[i][2]); // Side 3
       hist->Fill(i, 0.1,j , dev[i][3]); // Side 4
       }
    }
     
   
   gStyle->SetCanvasPreferGL(1);
   hist->Draw("glcolz");

   hist->GetXaxis()->SetTitle("Length (mm)");
   hist->GetXaxis()->SetTitleOffset(1.5);
   hist->GetYaxis()->SetTitle("Width (mm)");
   hist->GetYaxis()->SetTitleOffset(1.5);
   hist->GetZaxis()->SetTitle("Height (mm)");
   hist->GetZaxis()->SetTitleOffset(1.5);
   hist->GetZaxis()->SetRangeUser(-10,0);
   
   hist->SetTitle("OT");
   gStyle->SetCanvasPreferGL(1);
   gStyle->SetPalette(kRainBow);
   hist->SetFillColor(9);
   hist->Draw("glcolz");

   TPaveLabel *title = new TPaveLabel(-1., 0.86, 1., 0.98, "Sample 5603 (Bubbles) (360 nm) - Optical Transmittance Deviance");
   
   TLatex *tex = new TLatex(0.75,0.05,"Optical Transmittance Deviance (%)");
  tex->SetNDC();
  tex->SetTextSize(.015);
  tex->SetLineWidth(2);
  tex->Draw();
  title->SetFillColor(32);
  title->Draw();

  TLatex *avg2 = new TLatex(0.94,0.01, average);
  avg2->SetNDC();
  avg2->SetTextSize(.015);
  avg2->SetLineWidth(2);
  avg2->Draw();
  title->SetFillColor(32);
  title->Draw();

  TLatex *avg3 = new TLatex(0.87,0.01,"Average:");
  avg3->SetNDC();
  avg3->SetTextSize(.015);
  avg3->SetLineWidth(2);
  avg3->Draw();
  title->SetFillColor(32);
  title->Draw();

   //Now, specify the transfer function.
   TList * lf = hist->GetListOfFunctions();
   if (lf) {
      TF1 * tf = new TF1("TransferFunction", my_transfer_function);
      lf->Add(tf);
   }
}

  

Try:

TLatex *avg3 = new TLatex(0.87, 0.01, TString::Format("Average: %s", average);

or simply:

TLatex *avg3 = new TLatex(0.87, 0.01, TString::Format("Average: %g", avg);
1 Like