00001 #include <cassert> 00002 00003 #include "TF1.h" 00004 #include "TGraph.h" 00005 00006 #include "MessageService/MsgService.h" 00007 #include "NtupleUtils/NuHistInterpolator.h" 00008 00009 ClassImp(NuHistInterpolator) 00010 00011 CVSID("$Id: NuHistInterpolator.cxx,v 1.5 2009/04/08 20:16:46 gmieg Exp $"); 00012 00013 //____________________________________________________________________72 00014 void NuHistInterpolator::push_back(const Double_t shift, 00015 const TH1D& spectrum, 00016 const Double_t pot) 00017 { 00018 fSpectra[shift] = spectrum; 00019 fPoT = pot; 00020 } 00021 00022 //____________________________________________________________________72 00023 void NuHistInterpolator::push_back(const Double_t shift, 00024 const TH1D& spectrum) 00025 { 00026 fSpectra[shift] = spectrum; 00027 } 00028 /* 00029 //____________________________________________________________________72 00030 const TH1D NuHistInterpolator::ProvideSpectrum(const Double_t shift) const 00031 { 00032 std::map<Double_t,TH1D>::const_iterator spectraIt = fSpectra.begin(); 00033 while (spectraIt->first < shift && spectraIt != fSpectra.end()){ 00034 ++spectraIt; 00035 } 00036 if (fSpectra.begin() == spectraIt){ 00037 MSG("NuHistInterpolator",Msg::kWarning) 00038 << "Systematic shift " << shift << " below provided range" << endl; 00039 assert(false); 00040 } 00041 if (fSpectra.end() == spectraIt){ 00042 MSG("NuHistInterpolator",Msg::kWarning) 00043 << "Systematic shift " << shift << " above provided range" << endl; 00044 assert(false); 00045 } 00046 00047 TH1D spectrumAbove(spectraIt->second); 00048 const Double_t shiftAbove = spectraIt->first; 00049 00050 ++spectraIt; 00051 if (fSpectra.end() == spectraIt){ 00052 cout << "Dying" << endl; 00053 assert(false); 00054 } 00055 TH1D spectrumTwoAbove(spectraIt->second); 00056 const Double_t shiftTwoAbove = spectraIt->first; 00057 00058 --spectraIt; 00059 --spectraIt; 00060 TH1D spectrumBelow(spectraIt->second); 00061 const Double_t shiftBelow = spectraIt->first; 00062 00063 if (fSpectra.begin() == spectraIt){ 00064 cout << "Dying" << endl; 00065 assert(false); 00066 } 00067 00068 --spectraIt; 00069 if (fSpectra.begin() == spectraIt){ 00070 cout << "Dying" << endl; 00071 assert(false); 00072 } 00073 00074 TH1D spectrumTwoBelow(spectraIt->second); 00075 const Double_t shiftTwoBelow = spectraIt->first; 00076 00077 TH1D middleGradient(spectrumAbove); 00078 middleGradient.Add(&spectrumBelow,-1.0); 00079 middleGradient.Scale(1.0/(shiftAbove-shiftBelow)); 00080 00081 TH1D lowGradient(spectrumBelow); 00082 lowGradient.Add(&spectrumTwoBelow,-1.0); 00083 lowGradient.Scale(1.0/(shiftBelow-shiftTwoBelow)); 00084 00085 TH1D highGradient(spectrumTwoAbove); 00086 highGradient.Add(&spectrumAbove,-1.0); 00087 highGradient.Scale(1.0/(shiftTwoAbove-shiftAbove)); 00088 00089 if (shift<(shiftAbove+shiftBelow)/2.0){ 00090 TH1D spectrumOut(lowGradient); 00091 spectrumOut.Scale(shift - shiftTwoBelow); 00092 spectrumOut.Add(&spectrumTwoBelow); 00093 for (Int_t bin=1; bin<=spectrumOut.GetNbinsX();++bin){ 00094 spectrumOut.SetBinError(bin,1.0); 00095 if (spectrumOut.GetBinContent(bin)<0){ 00096 spectrumOut.SetBinContent(bin,0); 00097 } 00098 } 00099 return spectrumOut; 00100 } 00101 else{ 00102 TH1D spectrumOut(highGradient); 00103 spectrumOut.Scale(shift - shiftAbove); 00104 spectrumOut.Add(&spectrumAbove); 00105 for (Int_t bin=1; bin<=spectrumOut.GetNbinsX();++bin){ 00106 if (spectrumOut.GetBinContent(bin)<0){ 00107 spectrumOut.SetBinContent(bin,0); 00108 } 00109 spectrumOut.SetBinError(bin,1.0); 00110 } 00111 return spectrumOut; 00112 } 00113 } 00114 */ 00115 00116 //____________________________________________________________________72 00117 const TH1D NuHistInterpolator::ProvideSpectrum(const Double_t shift) const 00118 { 00119 //This is the simple interpolator 00120 std::map<Double_t,TH1D>::const_iterator spectraIt = fSpectra.begin(); 00121 while (spectraIt->first < shift && spectraIt != fSpectra.end()){ 00122 ++spectraIt; 00123 } 00124 if (fSpectra.begin() == spectraIt){ 00125 MSG("NuHistInterpolator",Msg::kWarning) 00126 << "Systematic shift " << shift << " below provided range" << endl; 00127 assert(false); 00128 } 00129 if (fSpectra.end() == spectraIt){ 00130 MSG("NuHistInterpolator",Msg::kWarning) 00131 << "Systematic shift " << shift << " above provided range" << endl; 00132 assert(false); 00133 } 00134 00135 TH1D spectrumAbove(spectraIt->second); 00136 const Double_t shiftAbove = spectraIt->first; 00137 --spectraIt; 00138 TH1D spectrumBelow(spectraIt->second); 00139 const Double_t shiftBelow = spectraIt->first; 00140 // cout << "Shift above = " << shiftAbove 00141 // << "; shift below = " << shiftBelow << endl; 00142 00143 if (shiftAbove == shiftBelow){ 00144 MSG("NuHistInterpolator",Msg::kWarning) 00145 << "Two shifted spectra have the identical systematic shifts" << endl; 00146 return spectrumAbove; 00147 } 00148 00149 spectrumAbove.Scale(shift-shiftBelow); 00150 spectrumAbove.Add(&spectrumBelow,shiftAbove-shift); 00151 spectrumAbove.Scale(1.0/(shiftAbove-shiftBelow)); 00152 // static Int_t count=0; 00153 // if (!(count%6)){ 00154 // cout << "Being asked for spectrum with shift " << shift << endl; 00155 // cout << "Bin 10 " << spectrumAbove.GetBinContent(10) << endl; 00156 // } 00157 // ++count; 00158 return spectrumAbove; 00159 } 00160 00161 /* 00162 //____________________________________________________________________72 00163 const TH1D NuHistInterpolator::ProvideSpectrum(const Double_t shift) const 00164 { 00165 std::map<Double_t,TH1D>::const_iterator spectraIt = fSpectra.begin(); 00166 while (spectraIt->first < shift && spectraIt != fSpectra.end()){ 00167 ++spectraIt; 00168 } 00169 if (fSpectra.begin() == spectraIt){ 00170 MSG("NuHistInterpolator",Msg::kWarning) 00171 << "Systematic shift " << shift << " below provided range" << endl; 00172 assert(false); 00173 } 00174 if (fSpectra.end() == spectraIt){ 00175 MSG("NuHistInterpolator",Msg::kWarning) 00176 << "Systematic shift " << shift << " above provided range" << endl; 00177 assert(false); 00178 } 00179 --spectraIt; 00180 if (fSpectra.begin() == spectraIt){ 00181 MSG("NuHistInterpolator",Msg::kWarning) 00182 << "Systematic shift " << shift << " below provided range" << endl; 00183 assert(false); 00184 } 00185 --spectraIt; 00186 TH1D outSpectrum(spectraIt->second); 00187 // outSpectrum.Clear(); 00188 // outSpectrum.Reset(); 00189 for (Int_t bin = 1; bin<=spectraIt->second.GetNbinsX(); ++bin){ 00190 TGraph gInterpolate(4); 00191 gInterpolate.SetPoint(0,spectraIt->first,spectraIt->second.GetBinContent(bin)); 00192 ++spectraIt; 00193 gInterpolate.SetPoint(1,spectraIt->first,spectraIt->second.GetBinContent(bin)); 00194 ++spectraIt; 00195 gInterpolate.SetPoint(2,spectraIt->first,spectraIt->second.GetBinContent(bin)); 00196 ++spectraIt; 00197 gInterpolate.SetPoint(3,spectraIt->first,spectraIt->second.GetBinContent(bin)); 00198 gInterpolate.Fit("pol2","Q"); 00199 Double_t par0 = gInterpolate.GetFunction("pol2")->GetParameter(0); 00200 Double_t par1 = gInterpolate.GetFunction("pol2")->GetParameter(1); 00201 Double_t par2 = gInterpolate.GetFunction("pol2")->GetParameter(2); 00202 Double_t binContent = par0 + shift*par1 + shift*shift*par2; 00203 if (binContent<0.0){binContent=0.0;} 00204 outSpectrum.SetBinContent(bin,binContent); 00205 --spectraIt; 00206 --spectraIt; 00207 --spectraIt; 00208 } 00209 return outSpectrum; 00210 } 00211 */ 00212 /* 00213 //____________________________________________________________________72 00214 const TH1D NuHistInterpolator::ProvideSpectrum(const Double_t shift) const 00215 { 00216 //This is the good interpolator 00217 std::map<Double_t,TH1D>::const_iterator spectraIt = fSpectra.begin(); 00218 TH1D outSpectrum(spectraIt->second); 00219 for (Int_t bin = 1; bin<=outSpectrum.GetNbinsX(); ++bin){ 00220 TGraph gInterpolate(fSpectra.size()); 00221 Int_t count=0; 00222 for (spectraIt = fSpectra.begin(); 00223 spectraIt != fSpectra.end(); 00224 ++spectraIt){ 00225 gInterpolate.SetPoint(count, 00226 spectraIt->first, 00227 spectraIt->second.GetBinContent(bin)); 00228 ++count; 00229 // cout << "Limit: " << outSpectrum.GetNbinsX() << endl; 00230 // cout << "Bin: " << bin << "Count: " << count << endl; 00231 } 00232 gInterpolate.Fit("pol2","Q"); 00233 Double_t par0 = gInterpolate.GetFunction("pol2")->GetParameter(0); 00234 Double_t par1 = gInterpolate.GetFunction("pol2")->GetParameter(1); 00235 Double_t par2 = gInterpolate.GetFunction("pol2")->GetParameter(2); 00236 Double_t binContent = par0 + shift*par1 + shift*shift*par2; 00237 00238 // gInterpolate.Fit("pol6","Q"); 00239 // Double_t par0 = gInterpolate.GetFunction("pol6")->GetParameter(0); 00240 // Double_t par1 = gInterpolate.GetFunction("pol6")->GetParameter(1); 00241 // Double_t par2 = gInterpolate.GetFunction("pol6")->GetParameter(2); 00242 // Double_t par3 = gInterpolate.GetFunction("pol6")->GetParameter(3); 00243 // Double_t par4 = gInterpolate.GetFunction("pol6")->GetParameter(4); 00244 // Double_t par5 = gInterpolate.GetFunction("pol6")->GetParameter(5); 00245 // Double_t par6 = gInterpolate.GetFunction("pol6")->GetParameter(6); 00246 // Double_t binContent = par0 + shift*par1 + shift*shift*par2 + shift*shift*shift*par3 + shift*shift*shift*shift*par4 00247 // + shift*shift*shift*shift*shift*par5 + shift*shift*shift*shift*shift*shift*par6; 00248 00249 if (binContent<0.0){binContent=0.0;} 00250 outSpectrum.SetBinContent(bin,binContent); 00251 } 00252 return outSpectrum; 00253 } 00254 */ 00255 //____________________________________________________________________72 00256 const TH1D NuHistInterpolator::ApplyShiftTo(const TH1D& spectrumToShift, 00257 const Double_t shift) const 00258 { 00259 TH1D outSpectrum(spectrumToShift); 00260 string name = spectrumToShift.GetName(); 00261 string title = spectrumToShift.GetTitle(); 00262 // outSpectrum.Reset(); 00263 // outSpectrum.Clear(); 00264 00265 std::map<Double_t,TH1D>::const_iterator spectraIt = fSpectra.begin(); 00266 for (Int_t bin = 1; bin<=outSpectrum.GetNbinsX(); ++bin){ 00267 TGraph gInterpolate(fSpectra.size()); 00268 Int_t count=0; 00269 for (spectraIt = fSpectra.begin(); 00270 spectraIt != fSpectra.end(); 00271 ++spectraIt){ 00272 gInterpolate.SetPoint(count, 00273 spectraIt->first, 00274 spectraIt->second.GetBinContent(bin)); 00275 ++count; 00276 // cout << "Limit: " << outSpectrum.GetNbinsX() << endl; 00277 // cout << "Bin: " << bin << "Count: " << count << endl; 00278 } 00279 gInterpolate.Fit("pol2","Q"); 00280 Double_t par0 = gInterpolate.GetFunction("pol2")->GetParameter(0); 00281 Double_t par1 = gInterpolate.GetFunction("pol2")->GetParameter(1); 00282 Double_t par2 = gInterpolate.GetFunction("pol2")->GetParameter(2); 00283 00284 Double_t scaleFactor = par0 + shift*par1 + shift*shift*par2; 00285 scaleFactor /= par0; 00286 Double_t binContent = spectrumToShift.GetBinContent(bin); 00287 // if (10==bin){cout << "Scale factor: " << scaleFactor << endl;} 00288 binContent *= scaleFactor; 00289 if (binContent<0.0){binContent=0.0;} 00290 outSpectrum.SetBinContent(bin,binContent); 00291 } 00292 // outSpectrum.SetName(name.c_str()); 00293 // outSpectrum.SetTitle(title.c_str()); 00294 return outSpectrum; 00295 }
1.3.9.1