Problem using CompareTo function with TString from TTreeReader

Hi Everyone,

I’m trying to read two trees in the same file with TTreeReader. In one of the trees there is a TString branch that I want to use to make a selection, so I wanted to use the CompareTo() function of the TString, but I can’t make it work…

So here is what I’m doing:

#include <iostream>

#include "TFile.h"
#include "TTreeReader.h"

void rd53aCorrelation ( ) { 

  TFile *file = TFile::Open ( "telescopeData.root" ) ;

  TTreeReader dutReader ( "dutData", file ) ;
  TTreeReader telReader ( "cluster3DTree", file ) ;

  // Declare telescope values
  TTreeReaderValue<TString> myM3226( telReader, "modName") ;
  TTreeReaderValue<Int_t> telEvent( telReader, "event" ) ;
  TTreeReaderValue<Double_t> telY( telReader, "y" ) ;

  // Declare dut values
  TTreeReaderValue<Int_t> dutEvent( dutReader, "trigNumber") ;
  TTreeReaderValue<Int_t> rowDUT( dutReader, "row") ;

  TH2D *corr = new TH2D("corr", "Correlation between RD53A and M3226", 480, -3., 3., 200, -0.8, 0.8);

  // Now we loop
  while ( telReader.Next() ) {

    if ( *myM3226.CompareTo("M3226") != 0 ) {

      while ( dutReader.Next()  ) { 

        if ( *telEvent < *dutEvent ) break ;
        if ( *telEvent != *dutEvent ) continue ;
        else if ( *telEvent == *dutEvent ) {

          //corr->Fill( (Double_t)( *rowDUT * 0.005-0.5), *telY );
          std::cout << *rowDUT << std::endl;
        }//end else
      }//end while
    }//end if m3226
  }//end whil 

corr->Draw("colz");

}//end void

The problem is with the line where I compare the TString to see if it matches “M3226”. There I get the error:

/Users/nikkie/cernbox/myWork/telescope/rd53aOctober2018/dataAnalysis/rd53aCorrelation.cc:27:18: error: no member named 'CompareTo' in 'TTreeReaderValue<TString>'; did you mean to use '->' instead of '.'?
    if ( *myM3226.CompareTo("M3226") != 0 ) {
                 ^
                 ->
/Users/nikkie/cernbox/myWork/telescope/rd53aOctober2018/dataAnalysis/rd53aCorrelation.cc:27:10: error: indirection requires pointer operand ('int' invalid)
    if ( *myM3226.CompareTo("M3226") != 0 ) {

I used the arrow as well to get rid of the first error but I can never get rid of the second one.

Does anybody know what I’m doing wrong?

Cheers,
Nikkie


_ROOT Version: 6.14/04
_Platform: MacOS Mojave
_Compiler: XCode


Hi,
I think the right C++ is myM3226->CompareTo("M3226") != 0: a -> to call CompareTo on the TString pointed by myM3226, and no * as you don’t need to dereference an int.

Cheers,
Enrico

When just checking for equality, you should prefer operator== over CompareTo. (better readability, less work)
So simply if (*myM3226 == "M3226") ...

For whatever reason the arrow isn’t working for me:

root [21] (*myM3226).CompareTo("X")
(int) 1
root [22] myM3226->CompareTo("X")
ROOT_prompt_22:1:8: error: member reference type 'TTreeReaderValue<TString>' is not a pointer; did you mean to use '.'?
myM3226->CompareTo("X")
~~~~~~~^~
root [23] (*myM3226 == "hello")
(bool) true

Hi Both,

Thanks for the help!
Actually Eguiraud’s comment solved my issue. I get a different error now but that is because my file is messed up.

Thanks again!
Nikkie

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