Changeset 8680 for trunk/MagicSoft/Mars/mhflux
- Timestamp:
- 08/20/07 11:04:58 (17 years ago)
- Location:
- trunk/MagicSoft/Mars/mhflux
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mhflux/MMcSpectrumWeight.cc
r8674 r8680 19 19 ! Author(s): Marcos Lopez 10/2003 <mailto:marcos@gae.ucm.es> 20 20 ! 21 ! Copyright: MAGIC Software Development, 2000-200 621 ! Copyright: MAGIC Software Development, 2000-2007 22 22 ! 23 23 ! … … 40 40 // Method: 41 41 // ------- 42 // 42 // 43 43 // - Corsika spectrun: dN/dE = A * E^(a) 44 44 // with a = fOldSlope, and A = N/integral{E*de} from ELowLim to EUppLim … … 72 72 // MMcEvt 73 73 // MMcCorsikaRunHeader 74 // [MPointingPos] 75 // [MHillas] 74 76 // 75 77 // Output Container: … … 81 83 #include <TF1.h> 82 84 #include <TH1.h> 85 #include <TH2.h> 86 #include <TH3.h> 83 87 #include <TSpline.h> 84 88 … … 109 113 fNameMcEvt = "MMcEvt"; 110 114 111 fNewSlope = -9 ;112 fOldSlope = -9 ;115 fNewSlope = -99; 116 fOldSlope = -99; 113 117 114 118 fEnergyMin = -1; … … 210 214 // The slope is returned as %.3f 211 215 // 212 TString MMcSpectrumWeight::GetFormulaSpecOld( ) const213 { 214 return Form("pow(%s.fEnergy, %.3f)", fNameMcEvt.Data(), fOldSlope);216 TString MMcSpectrumWeight::GetFormulaSpecOld(const char *name) const 217 { 218 return Form("pow(%s.fEnergy, %.3f)", name, fOldSlope); 215 219 } 216 220 … … 225 229 // unchanged. 226 230 // 227 TString MMcSpectrumWeight::GetFormulaSpecNew( ) const228 { 229 TString str = fFormula.IsNull() ? Form("pow(%s.fEnergy, %.3f)", fNameMcEvt.Data(), fNewSlope) : fFormula.Data();231 TString MMcSpectrumWeight::GetFormulaSpecNew(const char *name) const 232 { 233 TString str = fFormula.IsNull() ? Form("pow(%s.fEnergy, %.3f)", name, fNewSlope) : fFormula.Data(); 230 234 if (!fFormula.IsNull()) 231 str.ReplaceAll("X", Form("(%s.fEnergy)", fNameMcEvt.Data()));235 str.ReplaceAll("X", Form("(%s.fEnergy)", name)); 232 236 233 237 return str; … … 258 262 // Example: 0.3712780019*(pow(MMcEvt.fEnergy,-2.270))/(pow(MMcEvt.fEnergy,-2.600)) 259 263 // 260 TString MMcSpectrumWeight::GetFormulaWeights( ) const264 TString MMcSpectrumWeight::GetFormulaWeights(const char *name) const 261 265 { 262 266 if (GetFormulaSpecOld()==GetFormulaSpecNew()) … … 268 272 const Double_t norm = fNorm*iold/inew; 269 273 270 return Form("%.16e*(%s)/(%s)", norm, GetFormulaSpecNew( ).Data(), GetFormulaSpecOld().Data());274 return Form("%.16e*(%s)/(%s)", norm, GetFormulaSpecNew(name).Data(), GetFormulaSpecOld(name).Data()); 271 275 } 272 276 … … 276 280 // GetFormulaSpecNewX() describing the destination spectrum 277 281 // 278 Double_t MMcSpectrumWeight::GetSpecNewIntegral( ) const282 Double_t MMcSpectrumWeight::GetSpecNewIntegral(Double_t emin, Double_t emax) const 279 283 { 280 284 TF1 funcnew("Dummy", GetFormulaSpecNewX()); 281 return funcnew.Integral( fEnergyMin, fEnergyMax);285 return funcnew.Integral(emin, emax); 282 286 } 283 287 … … 287 291 // GetFormulaSpecOldX() describing the simulated spectrum 288 292 // 289 Double_t MMcSpectrumWeight::GetSpecOldIntegral( ) const293 Double_t MMcSpectrumWeight::GetSpecOldIntegral(Double_t emin, Double_t emax) const 290 294 { 291 295 TF1 funcold("Dummy", GetFormulaSpecOldX()); 292 return funcold.Integral( fEnergyMin, fEnergyMax);296 return funcold.Integral(emin, emax); 293 297 } 294 298 … … 357 361 if (fEnergyMax>fEnergyMin && !fAllowChange) 358 362 { 363 if (TMath::Abs(fEnergyMax-rh.GetEUppLim())>1e-10) 364 { 365 *fLog << err; 366 *fLog << "ERROR - The maximum simulated Monte Carlo energy is not allowed to change ("; 367 *fLog << "(" << fEnergyMax << " --> " << rh.GetEUppLim() << ")... abort." << endl; 368 return kFALSE; 369 } 370 359 371 if (TMath::Abs(fOldSlope-rh.GetSlopeSpec())>1e-10) 360 372 { … … 364 376 return kFALSE; 365 377 } 366 if (TMath::Abs(fEnergyMin-rh.GetELowLim())>1e-10) 367 { 368 *fLog << err; 369 *fLog << "ERROR - The minimum simulated Monte Carlo energy is not allowed to change "; 370 *fLog << "(" << fEnergyMin << " --> " << rh.GetELowLim() << ")... abort." << endl; 371 return kFALSE; 372 } 373 if (TMath::Abs(fEnergyMax-rh.GetEUppLim())>1e-10) 374 { 375 *fLog << err; 376 *fLog << "ERROR - The maximum simulated Monte Carlo energy is not allowed to change ("; 377 *fLog << "(" << fEnergyMax << " --> " << rh.GetEUppLim() << ")... abort." << endl; 378 return kFALSE; 379 } 380 return kTRUE; 378 379 // No change happened 380 if (TMath::Abs(fEnergyMin-rh.GetELowLim())<=1e-10) 381 return kTRUE; 382 383 // The lower energy limit has changed 384 *fLog << warn; 385 *fLog << "The minimum simulated Monte Carlo energy has changed from "; 386 *fLog << fEnergyMin << "GeV to " << rh.GetELowLim() << "GeV." << endl; 387 fEnergyMin = rh.GetELowLim(); 381 388 } 382 389 … … 385 392 fEnergyMax = rh.GetEUppLim(); 386 393 387 if (fNewSlope==-9 )388 { 389 *fLog << inf << " The new slope of the power law is undefined (-9)... no weighting applied." << endl;394 if (fNewSlope==-99) 395 { 396 *fLog << inf << "A new slope for the power law has not been defined... no weighting applied." << endl; 390 397 fNewSlope = fOldSlope; 391 398 } … … 403 410 // --------------------------------------------------------------------------- 404 411 // 412 // completes a simulated spectrum starting at an energy fEnergyMin down to 413 // an energy emin. 414 // 415 // It is assumed that the contents of MMcSpectrumWeight for the new spectrum 416 // correctly describe the spectrum within the histogram, and fEnergyMin 417 // and fEnergyMax correctly describe the range. 418 // 419 // In the 1D case it is assumed that the x-axis is a zenith angle binning. 420 // In the 2D case the x-axis is assumed to be zenith angle, the y-axis 421 // to be energy. 422 // 423 void MMcSpectrumWeight::CompleteEnergySpectrum(TH1 &h, Double_t emin) const 424 { 425 if (h.InheritsFrom(TH3::Class())) 426 { 427 *fLog << "ERROR - MMcSpctrumWeight::CompleteEnergySpectrum doesn't support TH3." << endl; 428 return; 429 } 430 431 if (fEnergyMin <= emin) 432 return; 433 434 const Double_t norm = GetSpecNewIntegral(); 435 436 if (!h.InheritsFrom(TH2::Class())) 437 { 438 h.Scale(GetSpecNewIntegral(emin, fEnergyMax)/norm); 439 return; 440 } 441 442 const TAxis &axey = *h.GetYaxis(); 443 444 const Int_t first = axey.FindFixBin(emin); 445 const Int_t last = axey.FindFixBin(fEnergyMin); 446 447 for (int x=1; x<=h.GetNbinsX(); x++) 448 { 449 const Double_t f = h.Integral(x, x, -1, 9999)/norm; 450 451 for (int y=first; y<=last; y++) 452 { 453 const Double_t lo = axey.GetBinLowEdge(y) <emin ? emin : axey.GetBinLowEdge(y); 454 const Double_t hi = axey.GetBinLowEdge(y+1)>fEnergyMin ? fEnergyMin : axey.GetBinLowEdge(y+1); 455 456 h.AddBinContent(h.GetBin(x, y), f*GetSpecNewIntegral(lo, hi)); 457 } 458 } 459 } 460 461 // --------------------------------------------------------------------------- 462 // 405 463 // The current contants are printed 406 464 // 407 465 void MMcSpectrumWeight::Print(Option_t *o) const 408 466 { 467 const TString opt(o); 468 469 const Bool_t hasnew = opt.Contains("new") || opt.IsNull(); 470 const Bool_t hasold = opt.Contains("old") || opt.IsNull(); 471 409 472 *fLog << all << GetDescriptor() << endl; 410 *fLog << " Simulated energy range: " << fEnergyMin << "GeV - " << fEnergyMax << "GeV" << endl; 411 *fLog << " Simulated spectral slope: " << fOldSlope << endl; 412 *fLog << " New spectral slope: " << fNewSlope << endl; 473 474 if (hasold) 475 { 476 *fLog << " Simulated energy range: " << fEnergyMin << "GeV - " << fEnergyMax << "GeV" << endl; 477 *fLog << " Simulated spectral slope: "; 478 if (fOldSlope==-99) 479 *fLog << "undefined" << endl; 480 else 481 *fLog << fOldSlope << endl; 482 } 483 if (hasnew) 484 { 485 *fLog << " New spectral slope: "; 486 if (fNewSlope==-99) 487 *fLog << "undefined" << endl; 488 else 489 *fLog << fNewSlope << endl; 490 } 413 491 *fLog << " Additional user norm.: " << fNorm << endl; 414 492 *fLog << " Spectra are normalized: " << (fNormEnergy<0?"by integral":Form("at %.1fGeV", fNormEnergy)) << endl; 415 *fLog << " Old Spectrum: " << GetFormulaSpecOldX() << " (I=" << GetSpecOldIntegral() << ")" << endl; 416 *fLog << " New Spectrum: " << GetFormulaSpecNewX() << " (I=" << GetSpecNewIntegral() << ")" << endl; 493 if (hasold) 494 { 495 *fLog << " Old Spectrum: " << GetFormulaSpecOldX(); 496 if (fEnergyMin>=0 && fEnergyMax>0) 497 *fLog << " (I=" << GetSpecOldIntegral() << ")"; 498 *fLog << endl; 499 } 500 if (hasnew) 501 { 502 *fLog << " New Spectrum: " << GetFormulaSpecNewX(); 503 if (fEnergyMin>=0 && fEnergyMax>0) 504 *fLog << " (I=" << GetSpecNewIntegral() << ")"; 505 *fLog << endl; 506 } 417 507 if (fFunc) 418 *fLog << " Weight func tion: " << fFunc->GetTitle() << endl;508 *fLog << " Weight func: " << fFunc->GetTitle() << endl; 419 509 } 420 510 … … 436 526 } 437 527 528 /* 529 * This could be used to improve the Zd-weighting within a bin. 530 * Another option is to use more bins, or both. 531 * Note that it seems unnecessary, because the shape within the 532 * theta-bins should be similar in data and Monte Carlo... hopefully. 533 * 534 void MMcSpectrumWeight::InitZdWeights() 535 { 536 TH2D w(*fWeightsZd); 537 538 for (int i=1; i<=w.GetNbinsX(); i++) 539 { 540 const Double_t tmin = w.GetBinLowEdge(i) *TMath::DegToRad(); 541 const Double_t tmax = w.GetBinLowEdge(i+1)*TMath::DegToRad(); 542 543 const Double_t wdth = tmax-tmin; 544 const Double_t integ = cos(tmin)-cos(tmax); 545 546 w.SetBinContent(i, w.GetBinContent(i)*wdth/integ); 547 } 548 549 // const Int_t i = fWeightsZd->GetXaxis()->FindFixBin(fPointing->GetZd()); 550 // const Double_t theta = fPointing->GetZd()*TMath::DegToRad(); 551 // w = sin(theta)*w.GetBinContent(i); 552 } 553 */ 554 438 555 // ---------------------------------------------------------------------------- 439 556 // … … 447 564 if (fWeightsZd) 448 565 { 449 /*450 const Int_t i = fWeightsZd->GetXaxis()->FindFixBin(fPointing->GetZd());451 452 Double_t tmin = fWeightsZd->GetXaxis()->GetBinLowEdge(i) *TMath::DegToRad();453 Double_t tmax = fWeightsZd->GetXaxis()->GetBinLowEdge(i+1)*TMath::DegToRad();454 Double_t wdth = fWeightsZd->GetXaxis()->GetBinWidth(i) *TMath::DegToRad();455 456 Double_t cont = fWeightsZd->GetBinContent(i);457 458 Double_t integ = cos(tmin)-cos(tmax);459 460 w = sin(tmax)*cont/integ*wdth;461 */462 463 566 const Int_t i = fWeightsZd->GetXaxis()->FindFixBin(fPointing->GetZd()); 464 567 w = fWeightsZd->GetBinContent(i); … … 472 575 473 576 const Double_t e = fMcEvt->GetEnergy(); 577 474 578 fWeight->SetVal(fFunc->Eval(e)*w); 475 579 -
trunk/MagicSoft/Mars/mhflux/MMcSpectrumWeight.h
r7688 r8680 77 77 78 78 // Getter 79 TString GetFormulaSpecOld() const; 80 TString GetFormulaSpecNew() const; 81 TString GetFormulaWeights() const; 79 TString GetFormulaSpecOld(const char *name) const; 80 TString GetFormulaSpecNew(const char *name) const; 81 TString GetFormulaWeights(const char *name) const; 82 TString GetFormulaSpecOld() const { return GetFormulaSpecOld(fNameMcEvt); } 83 TString GetFormulaSpecNew() const { return GetFormulaSpecNew(fNameMcEvt); } 84 TString GetFormulaWeights() const { return GetFormulaWeights(fNameMcEvt); } 82 85 83 86 TString GetFormulaSpecOldX() const { return ReplaceX(GetFormulaSpecOld()); } … … 85 88 TString GetFormulaWeightsX() const { return ReplaceX(GetFormulaWeights()); } 86 89 87 Double_t GetSpecNewIntegral() const; 88 Double_t GetSpecOldIntegral() const; 90 Double_t GetSpecNewIntegral(Double_t emin, Double_t emax) const; 91 Double_t GetSpecOldIntegral(Double_t emin, Double_t emax) const; 92 93 Double_t GetSpecNewIntegral() const { return GetSpecNewIntegral(fEnergyMin, fEnergyMax); } 94 Double_t GetSpecOldIntegral() const { return GetSpecOldIntegral(fEnergyMin, fEnergyMax); } 89 95 90 96 Double_t CalcSpecNew(Double_t e) const; … … 93 99 Double_t GetEnergyMin() const { return fEnergyMin; } 94 100 Double_t GetEnergyMax() const { return fEnergyMax; } 101 102 // Functions 103 void CompleteEnergySpectrum(TH1 &h, Double_t emin) const; 95 104 96 105 // TObject
Note:
See TracChangeset
for help on using the changeset viewer.