[ROOT] Re: TString & Co.

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Tue Apr 09 2002 - 11:41:03 MEST


Hi Jacek,

  the way you describe it it indeed looks ugly. Luckily things can be
done simpler and less ugly.

Concerning the first example you can just do:

 	void z1aAnalyze(Int_t NuAlpha)
 	{
 	TString s;
 	// ... something is done here and then ...
 	s = ""; // just a precaution ...
 	s += NuAlpha; // encode the "number"
 	nVac250SM->Process(selector, s + " Vac250SM");
 	nVac250NS->Process(selector, s + " Vac250NS");

s + " Vac250SM" creates a new temp TString and since this TString is
passed as a "const char*" argument the "TString::operator const char*()"
is automatically applied to cast the TString to a "const char*". This
"operator const char*" does the same as calling the method
TString::Data().

To read from a TString without sscanf() in a type safe way, do:

TString input("222 MKX");
istringstream source(input.Data());  // there is no "const char*" ctor
int number;
TString mkx;
source >> number >> mkx;

Parsing a string can be done using TString and istreams like this:

istringstream source("item1, item2, item3");
TString s;
s.ReadToDelim(source, ',');
etc., see also TString::ReadToken().

Concerning TString::Strip(), it returns a TSubString, which is only a
range limitation of the original TString, it does not physically modify
the TString. To get a permanently stripped string do yo have to reassign
is to a TString, like:

TString s("   aap   ");
s = s.Strip(TString::kBoth);

and this brings us to the last remark, the TString enums are known to
CINT but you need to specify the full scope: TString::kBoth, idem for
TString::kExact, etc.


Cheers, Fons.




On Sat, 2002-04-06 at 00:29, Jacek M. Holeczek wrote:
> Hi,
> As Rene asked Fons to have a look at it I decided to give more
> explanations. Maybe I am doing something completely screwy ???
> I have a "selector" which is expected to analyze an ntuple.
> Up to now the "selector" was expected to get one "option" which was used
> as a "file name" inside of "Selector::Begin" :
> 	TString MyNameIs = GetOption();
> and everyone was happy ...
> But today, I've been asked to make some additional pictures ...
> This created the problem that I need to pass an additional option to my
> "Selector::Begin" which defines a "starting value" of one parameter.
> Unfortunately, the "tree->Process" allows only one option (a string) ...
> So, I need to encode both, a "number" and a "string", into this option.
> The main (interpreted) script that does this analysis looks like :
> 	void z1aAnalyze(Int_t NuAlpha)
> 	{
> 	TString s;
> 	// ... something is done here and then ...
> 	s = ""; // just a precaution ...
> 	s += NuAlpha; // encode the "number"
> 	nVac250SM->Process(selector, s.Copy().Append(" Vac250SM").Data());
> 	nVac250NS->Process(selector, s.Copy().Append(" Vac250NS").Data());
> 	nMat250SM->Process(selector, s.Copy().Append(" Mat250SM").Data());
> 	nMat250NS->Process(selector, s.Copy().Append(" Mat250NS").Data());
> 	nVac732SM->Process(selector, s.Copy().Append(" Vac732SM").Data());
> 	nVac732NS->Process(selector, s.Copy().Append(" Vac732NS").Data());
> 	nMat732SM->Process(selector, s.Copy().Append(" Mat732SM").Data());
> 	nMat732NS->Process(selector, s.Copy().Append(" Mat732NS").Data());
> 	// ... again something is done here ...
> 	}
> You can see how screwy it is ...
> I especially like very much this "s.Copy().Append(" xxxx").Data()" !!!
> Then the problem in the "Selector::Begin" is that I need to decode it ...
> 	void z1aSelector::Begin(TTree *tree)
> 	{
> 	// ... something is done here and then ...
> 	char ctmp[16];
> 	ctmp[0] = '\0'; // just a precaution ...
> 	TString MyNameIs = GetOption();
> 	Int_t MyNuAlphaIs;
> 	sscanf(MyNameIs.Data(), "%i %15s", &MyNuAlphaIs, ctmp);
> 	MyNameIs = ctmp;
> 	// ... again something is done here ...
> 	}
> Again, you can see how screwy it is ... but I haven't found any better
> solution for now.
> I tried to use methods of the TString class to "split" the option when it
> comes to "Selector::Begin", but I didn't succeed - I tried, for example,
> MyNameIs.Strip(1, ' ').Data(), but it always returns the full original
> string ... apparently I don't really know how to use "Strip" ...
> (That's how I discovered that some enums are not visible in root/cint.)
> Best regards,
> Jacek.
-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7679480



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:48 MET