GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Spin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef ROOT_TString
24 #include <TString.h>
25 #endif
26 
27 #include <iostream>
28 #include <sstream>
29 
30 #ifndef GW_Spin
31 #include "Spin.h"
32 #endif
33 
34 using namespace std;
35 using namespace Gw;
36 
37 ClassImp(Spin);
38 
39 Spin::Spin()
40  : QNumber(), InfoData()
41 {
42  fN = 0; fD = 1;
43 }
44 
45 Spin::Spin(Int_t N, Int_t D)
46  : QNumber(), InfoData()
47 {
48  Set(N,D);
49 }
50 
52 {
53 }
54 
55 const Char_t* Spin::GetString() const
56 {
57  if (fD != 1)
58  return Form("%d/%d", fN, fD);
59  else
60  return Form("%d", fN);
61 }
62 
63 void Spin::Set(const char *s)
64 {
65  std::istringstream input; input.clear(); TString st = s;
66  switch( WhatIs(s) ){
67  case kKnown:
68  if ( st.Contains('/') ) { // not an integer
69  st.ReplaceAll('/',' '); input.str(st.Data()); input >> fN >> fD;
70  }
71  else { input.str(st.Data()); input >> fN; fD = 1; }
72  if ( input.fail() )
74  else
75  SetInfo(kKnown);
76  break;
77  case kUnknown:
79  break;
80  case kAbout:
81  st.ReplaceAll('~',' ');
82  if ( st.Contains('/') ) { // not an integer
83  st.ReplaceAll('/',' '); input.str(st.Data()); input >> fN >> fD;
84  }
85  else { input.str(st.Data()); input >> fN; fD = 1; }
86  if ( input.fail() )
88  else
89  SetInfo(kAbout);
90  break;
91  case kTentative:
92  st.ReplaceAll('(',' ');
93  st.ReplaceAll(')',' ');
94  if ( st.Contains('/') ) { // not an integer
95  st.ReplaceAll('/',' '); input.str(st.Data()); input >> fN >> fD;
96  }
97  else { input.str(st.Data()); input >> fN; fD = 1; }
98  if ( input.fail() )
100  else
102  break;
103  case kTheo:
104  st.ReplaceAll('[',' ');
105  st.ReplaceAll(']',' ');
106  if ( st.Contains('/') ) { // not an integer
107  st.ReplaceAll('/',' '); input.str(st.Data()); input >> fN >> fD;
108  }
109  else { input.str(st.Data()); input >> fN; fD = 1; }
110  if ( input.fail() )
111  SetInfo(kUnknown);
112  else
113  SetInfo(kTheo);
114  break;
115  default:
116  SetInfo(kUnknown);
117  break;
118  }
119 }
120 
124 void Spin::Set(Int_t n, Int_t d)
125 {
126  fN = n; fD = d;
127  if ( fD < 1 || fD > 2 ) {
128  printf("- ! - A spin should be an integer or half an integer ! \n ");
129  printf("\t [%d/%d] --> [%d/1] \n ",fN,fD,fN);
130  fD = 1;
131  }
132 }
133 
135 {
136  fN += fD*value;
137  return *this;
138 }
139 
141 {
142  if (fD == sp.fD) {
143  fN += sp.fN;
144  } else {
145  fN = fD*sp.fN + fN*sp.fD;
146  fD = TMath::Max(fD, sp.fD);
147  }
148  return *this;
149 }
150 
151 std::ostream & operator << (std::ostream &out, const Spin &spin)
152 {
153  string sep1, sep2;
154 
155  if ( spin.IsData(InfoData::kUnknown) ) {
156  out << "?" ;
157  return out;
158  }
159  sep1 = sep2 = "";
160  if ( spin.IsData(InfoData::kTentative) )
161  { sep1 = '('; sep2 = ')'; }
162  if ( spin.IsData(InfoData::kTheo) )
163  { sep1 = '['; sep2 = ']'; }
164  if ( spin.IsData(InfoData::kAbout) )
165  { sep1 = '~'; sep2 = ""; }
166 
167  if ( spin.GetD() == 1 ) {
168  out << sep1 << spin.GetN() << sep2 ;
169  return out;
170  }
171  else {
172  out << sep1 << spin.GetN() << "/" << spin.GetD() << sep2;
173  return out;
174  }
175 }
176 
177 
printf("******************************************************************** \n")
static EData WhatIs(const char *)
It deduces from a string the kind of data.
Definition: InfoData.cpp:45
virtual bool IsData(UShort_t) const
to get some information about this data
Definition: InfoData.h:82
void Set(Int_t n, Int_t d=1)
To set the spin.
Definition: Spin.cpp:124
Int_t GetD() const
To get the spin (denominator)
Definition: Spin.h:77
Base class for a quantum number.
Definition: QNumber.h:43
virtual ~Spin()
Definition: Spin.cpp:51
UInt_t value[MaxValue]
Definition: ReadDaqAlone.C:29
A spin is defined by two integers: a numerator and a denominator.
Definition: Spin.h:46
Int_t fD
Definition: Spin.h:57
Int_t fN
Definition: Spin.h:56
Spin & operator+=(Int_t value)
operator +=
Definition: Spin.cpp:134
const Char_t * GetString() const
To get the spin as a string.
Definition: Spin.cpp:55
Spin()
Definition: Spin.cpp:39
Base class to get/set some flag informations concerning any data.
Definition: InfoData.h:46
virtual void SetInfo(InfoData::EData)
to set some information about this data
Definition: InfoData.h:83
Int_t GetN() const
To get the spin (numerator)
Definition: Spin.h:75
header file for a spin quantum number
ClassImp(Spin)
std::ostream & operator<<(std::ostream &out, const Spin &spin)
to write a Spin in a stream
Definition: Spin.cpp:151