Using Regex with a TLegend

Hey,

I am using Regex to search through a list of files for a select number of files, open the files, find a particular histogram and then layer the histograms on top and repeat this for different file combinations. I want to use TLegend to label each line in a plot. I am not sure how to add the correct file name for each histogram to the TLegend. This is the line I am having trouble with: Legend->AddEntry(h[i], Form("%s", BaseLabelPattern));

   std::regex Pattern("Mu");

    //Loop through the possible numbers for Mu%iMu%i
   for( int j = 0; j < 6; j++ ){

     //Define a dynamic TString with the base pattern
     std::string BasePattern = "Mu" + std::to_string(j) + "Mu" + std::to_string(j) + "Y" +     "[0-5]";
 //Now define the regeular expression for pattern matching
 std::regex PatternRegex(BasePattern.c_str());  //Looking for file names which match this.

 //Define a dynamic TString with the base pattern for the labelling
 std::string BaseLabelPattern = "Mu" + std::to_string(j) + "Mu" + std::to_string(j) + "Y" + "[0-5]";
 //Now define the regular expression for patter matching
 std::regex LabelPatternRegex(BaseLabelPattern);
 
 std::cout << "<Overlaying_Plots()>::   >>>>>>>>>>>>>>>>> Matching 'Y" << j << " <<<<<<<<<<<<<<<<<<<<<" << std::endl;
 
 TCanvas *Dummy = new TCanvas (BasePattern.c_str(), BasePattern.c_str(), 700, 500);
 Dummy->SetLogy();
 
 //Define a TLegend for the Plots
 auto Legend = new TLegend (0.6, 0.6, 0.9, 0.9);    //x1,y1,x2,y2
 Legend->SetHeader("The Legend Title", "C");
 //Loop through files
 for (int i = 0; i < numFiles; i++){

   //Define a search match condition identifier
   std::smatch match;

   if( std::regex_search(File_Names.at(i), match, PatternRegex) ){
 std::cout << "<Overlaying_Plots()>::   Matched regular expression : " << BasePattern << " with " << File_Names.at(i) << std::endl;
 f[i] = TFile::Open(File_Names[i].c_str());
 TH1D *h[i];
 f[i]-> GetObject(Histogram_Names[0].c_str(),h[i]);
 h[i]->SetDirectory(0);

 if(i!=0){
   h[i]->SetLineColor(i);
   h[i]->Draw("same");
  
 }else{
   h[i]->SetLineColor(i+2);	  
   h[i]->Draw();
 }
 
 //add histogram to legend
 Legend->AddEntry(h[i], Form("%s", LabelPatternRegex));

 
   }else{
 //Do nothing
   }
   
 }//Loop end (NumFiles)
 
 Legend->Draw();

Please provide the following information:


ROOT Version (e.g. 6.12/02):
Platform, compiler (e.g. CentOS 7.3, gcc6.2):


Hi,

Well, a std::regex is not a string… Please take a look at the example on this page

Cheers, Bertrand.

Hi,

I’ve had a read over, I believe I should have something like

AddEntry->(h[i], Form("%s", match.str() )); ?

However, it doesn’t like that as it’s a string and not a char

http://www.cplusplus.com/reference/string/string/c_str/

I don’t know… Maybe something like: AddEntry->(h[i], Form("%s", match.str().c_str()));?

Hey,

Yep that seems to do the trick. Thanks!

1 Like

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