247 if(
uVERBOSE >= 1)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile::ERROR: Array does NOT exist")<<std::endl;
251 if(ObjectsArray->GetLast() < 0)
253 if(
uVERBOSE >= 1)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile: WARING: Objects array is empty")<<std::endl;
257 TString resultfilename = filename;
258 if(!resultfilename.EndsWith(
".root")) resultfilename +=
".root";
260 TObject* pobjCurrObject = NULL;
263 TFile *file =
new TFile(resultfilename.Data(),
"UPDATE");
267 if(
uVERBOSE >= 2)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile: Writing arrays in file \"%s\" ... ", resultfilename.Data())<<std::endl;
271 if(
uVERBOSE >= 1)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile::ERROR: File \"%s\" was NOT opened for UPDATE",resultfilename.Data())<<std::endl;
276 for(Int_t n=0; n<ObjectsArray->GetEntries(); n++)
278 pobjCurrObject = ObjectsArray->At(n);
279 if(pobjCurrObject == NULL)
continue;
281 objectname = pobjCurrObject->GetName();
282 if(
uVERBOSE >= 3)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile::Saving \"%s\" ...",objectname.Data())<<std::endl;
283 file->rmdir(objectname.Data());
284 TDirectory *CurrDir = file->mkdir(objectname.Data()); CurrDir->cd();
285 pobjCurrObject->Write();
287 if(
uVERBOSE >= 2)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile::Object \"%s\" was successfully writed",objectname.Data())<<std::endl;
293 gDirectory->cd(
"Rint:/");
295 TObjArray *currArray = NULL;
298 for(Int_t n=0; n<ObjectsArray->GetEntries(); n++)
300 currArray = (TObjArray*)ObjectsArray->At(n);
301 if(currArray == NULL)
continue;
302 objectname = currArray->GetName();
303 if(
uVERBOSE >= 2)
current_out <<Form(
"utilites.h/SaveArrayObjectsArrayInFile::Deleting \"%s\"",objectname.Data())<<std::endl;
306 ObjectsArray->Clear();
333Int_t
FitHistogrammInRMSRange(TH1 *histogramm, Double_t &mean, Double_t &sigma, Double_t RMSrange, Double_t RangeXmin, Double_t RangeXmax)
336 current_out <<Form(
"utilites.h/FitHistogrammInRMSRange::ERROR: histogramm pointer is NULL")<<std::endl;
return 0;}
338 if(RangeXmax <= RangeXmin)
340 RangeXmin = histogramm->GetXaxis()->GetXmin();
341 RangeXmax = histogramm->GetXaxis()->GetXmax();
344 Double_t hist_start_Xmin = histogramm->GetXaxis()->GetXmin();
345 Double_t hist_start_Xmax = histogramm->GetXaxis()->GetXmax();
347 histogramm->SetAxisRange(RangeXmin, RangeXmax);
348 Double_t histMean = histogramm->GetMean();
349 Double_t histRMS = histogramm->GetRMS();
350 histogramm->SetAxisRange(hist_start_Xmin, hist_start_Xmax);
352 Double_t fit_range_min = histMean-RMSrange*histRMS;
353 Double_t fit_range_max = histMean+RMSrange*histRMS;
355 if(fit_range_min < RangeXmin) fit_range_min = RangeXmin;
356 if(fit_range_max > RangeXmax) fit_range_max = RangeXmax;
358 return FitHistogramm(histogramm, mean, sigma, fit_range_min, fit_range_max);
361Int_t
FitHistogrammInMaxPeak(TH1 *histogramm, Double_t &mean, Double_t &sigma, Double_t PeakWidth, Double_t RangeXmin, Double_t RangeXmax)
364 current_out <<Form(
"utilites.h/FitHistogrammInMaxPeak::ERROR: histogramm pointer is NULL")<<std::endl;
return 0;}
367 if(RangeXmax <= RangeXmin)
369 RangeXmin = histogramm->GetXaxis()->GetXmin();
370 RangeXmax = histogramm->GetXaxis()->GetXmax();
373 Double_t hist_start_Xmin = histogramm->GetXaxis()->GetXmin();
374 Double_t hist_start_Xmax = histogramm->GetXaxis()->GetXmax();
376 histogramm->SetAxisRange(RangeXmin, RangeXmax);
377 Double_t PeakMax = histogramm->GetBinCenter( histogramm->GetMaximumBin() );
378 histogramm->SetAxisRange(hist_start_Xmin, hist_start_Xmax);
380 Double_t FitRangeMin = PeakMax-PeakWidth*0.5;
381 Double_t FitRangeMax = PeakMax+PeakWidth*0.5;
383 FitRangeMin = (FitRangeMin < RangeXmin)? RangeXmin : FitRangeMin;
384 FitRangeMax = (FitRangeMax > RangeXmax)? RangeXmax : FitRangeMax;
387 if(
uVERBOSE >= 4)
current_out<<Form(
"utilites.h/FitHistogrammInMaxPeak: Hsit: \"%s\" Maximum: %.2f, Peak Width: %.2f, Range:[%.2f, %.2f]",
388 histogramm->GetName(), PeakMax, PeakWidth, FitRangeMin, FitRangeMax)<<std::endl;
391 return FitHistogramm(histogramm, mean, sigma,FitRangeMin,FitRangeMax);
395Int_t
FitHistogrammSmartInMaxPeak(TH1 *histogramm, Double_t &mean, Double_t &sigma,Double_t nRMSfit = 2., Double_t RangeXmin = 0., Double_t RangeXmax = 0.)
397 static const Int_t nSmooth = 1;
398 static const Double_t nSmoothRMScut = 1.;
402 current_out <<Form(
"utilites.h/FitHistogrammSmartInMaxPeak::ERROR: histogramm pointer is NULL")<<std::endl;
return 0;}
404 if(RangeXmax <= RangeXmin)
406 RangeXmin = histogramm->GetXaxis()->GetXmin();
407 RangeXmax = histogramm->GetXaxis()->GetXmax();
410 histogramm->SetAxisRange(RangeXmin, RangeXmax);
413 TH1 *tempSmoothProjection = (TH1*)histogramm->Clone(
"tempSmoothFitHist");
414 tempSmoothProjection->Smooth(nSmooth);
415 tempSmoothProjection->SetAxisRange(RangeXmin, RangeXmax);
416 Double_t SmoothPeakMax = tempSmoothProjection->GetBinCenter( tempSmoothProjection->GetMaximumBin() );
417 Double_t SmoothRMS = tempSmoothProjection->GetRMS();
420 if( TMath::Abs(SmoothPeakMax - RangeXmax) < SmoothRMS*0.5 )
422 RangeXmax += SmoothRMS*0.5;
423 tempSmoothProjection->SetAxisRange(RangeXmin, RangeXmax);
424 SmoothPeakMax = tempSmoothProjection->GetBinCenter( tempSmoothProjection->GetMaximumBin() );
425 SmoothRMS = tempSmoothProjection->GetRMS();
426 }
else if( TMath::Abs(SmoothPeakMax - RangeXmin) < SmoothRMS*0.5 )
428 RangeXmin -= SmoothRMS*0.5;
429 tempSmoothProjection->SetAxisRange(RangeXmin, RangeXmax);
430 SmoothPeakMax = tempSmoothProjection->GetBinCenter( tempSmoothProjection->GetMaximumBin() );
431 SmoothRMS = tempSmoothProjection->GetRMS();
434 tempSmoothProjection->SetAxisRange(SmoothPeakMax-SmoothRMS*nSmoothRMScut, SmoothPeakMax+SmoothRMS*nSmoothRMScut);
435 Double_t SmoothRMSreal = tempSmoothProjection->GetRMS();
436 delete tempSmoothProjection;
438 Double_t FitRangeMin = SmoothPeakMax-SmoothRMSreal*nRMSfit;
439 Double_t FitRangeMax = SmoothPeakMax+SmoothRMSreal*nRMSfit;
441 FitRangeMin = (FitRangeMin < RangeXmin)? RangeXmin : FitRangeMin;
442 FitRangeMax = (FitRangeMax > RangeXmax)? RangeXmax : FitRangeMax;
444 if(
uVERBOSE >= 3)
current_out<<Form(
"utilites.h/FitHistogrammSmartInMaxPeak: Hsit: \"%s\" Maximum: %.2f, SmoothRMS: %.2f, SmoothRMSreal: %.2f, Range:[%.2f, %.2f]",
445 histogramm->GetName(), SmoothPeakMax, SmoothRMS, SmoothRMSreal, FitRangeMin, FitRangeMax)<<std::endl;
447 return FitHistogramm(histogramm, mean, sigma, FitRangeMin, FitRangeMax);
451Int_t
FitHistogramm(TH1 *histogramm, Double_t &mean, Double_t &sigma, Double_t RangeXmin, Double_t RangeXmax, TString fit_formula)
453 static const Int_t minFitEvents = 1;
454 static const Int_t EventsNoFit = 15;
456 TString fitoptions[] = {
"QR",
"QRL",
"QRI",
"END"};
459 current_out <<Form(
"utilites.h/FitHistogramm::ERROR: histogramm pointer is NULL")<<std::endl;
return 0;}
462 if(RangeXmax <= RangeXmin)
464 RangeXmin = histogramm->GetXaxis()->GetXmin();
465 RangeXmax = histogramm->GetXaxis()->GetXmax();
470 Int_t intBinMin = histogramm->FindBin(RangeXmin);
471 Int_t intBinMax = histogramm->FindBin(RangeXmax);
472 Int_t integral = histogramm->Integral(intBinMin, intBinMax);
474 if( integral < minFitEvents){
if(
uVERBOSE >= 1)
475 current_out <<Form(
"utilites.h/FitHistogramm::ERROR: Too low events in \"%s\" histogram: %i",histogramm->GetName(), integral)<<std::endl;
return 0;}
478 current_out <<Form(
"utilites.h/FitHistogramm::Fiting histogramm \"%s\"",histogramm->GetName())<<std::endl;
481 Double_t hist_start_Xmin = histogramm->GetXaxis()->GetXmin();
482 Double_t hist_start_Xmax = histogramm->GetXaxis()->GetXmax();
484 histogramm->SetAxisRange(RangeXmin, RangeXmax);
485 Double_t histMean = histogramm->GetMean();
486 Double_t histRMS = histogramm->GetRMS();
487 histogramm->SetAxisRange(hist_start_Xmin, hist_start_Xmax);
491 if( integral <= EventsNoFit )
498 TF1 *currfitfunction = NULL;
499 TF1 *bestfitfunction = NULL;
500 Int_t bestOption = -111;
502 Double_t bestChisquare = -111., currChisquare = 0;
504 for(Int_t opt = 0; !fitoptions[opt].Contains(
"END"); opt++){
506 currfitfunction =
new TF1(
fit_funk_name, fit_formula.Data(), RangeXmin, RangeXmax);
508 histogramm->Fit(currfitfunction, fitoptions[opt],
"", RangeXmin, RangeXmax);
509 currChisquare = currfitfunction->GetChisquare();
511 if(
uVERBOSE >= 3)
current_out<<Form(
"utilites.h/FitHistogramm: current fiting option: \"%s\", chi-square: %.1f",fitoptions[opt].Data(), currChisquare)<<std::endl;
512 if( ( (currChisquare < bestChisquare)&&(currChisquare > 0.0001) )||( (bestChisquare < 0.)&&(currChisquare > 0.0001)) )
514 bestChisquare = currChisquare;
515 if(bestfitfunction)
delete bestfitfunction;
516 bestfitfunction = currfitfunction;
521 if(currfitfunction)
delete currfitfunction;
526 if( 0. < bestChisquare)
528 histogramm->Fit(bestfitfunction, fitoptions[bestOption],
"", RangeXmin, RangeXmax);
529 if(
uVERBOSE >= 2)
current_out<<Form(
"utilites.h/FitHistogramm: best fit [%.2f, %.2f]: %.2f, option %i",
530 RangeXmin, RangeXmax, bestChisquare, bestOption)<<std::endl;
531 histogramm->GetFunction(
fit_funk_name)->SetRange(RangeXmin, RangeXmax);
534 mean = bestfitfunction->GetParameter(1);
535 sigma = bestfitfunction->GetParameter(2);
544 if(bestfitfunction)
delete bestfitfunction;
613 const Float_t minbinevedge = 0.001,
const Int_t rebinX = 1,
const Int_t rebinY = 1, Int_t ProjectionRebin = 5, Float_t projWidth = 1.5, TObjArray *monitor_slices_array = NULL)
619 if(
uVERBOSE >= 1)cout <<Form(
"utilites.h/CreateSmartPojection::ERROR: hist pointer is NULL !!!\n")<<endl;
623 static const Int_t minBinEvOnEdge = 5;
626 const Bool_t IsDrawSIGMA = option.Contains(
"SIGMA");
627 const Bool_t IsFitProjection = option.Contains(
"FIT");
629 if(
uVERBOSE >= 2)cout <<Form(
"utilites.h/CreateSmartPojection: Creating %s Projection%s for \"%s\" ... ",
630 (IsDrawSIGMA)?
"sigma":
"mean", (IsFitProjection)?
" by fit":
"", sourcehist->GetName())<<endl;
632 Int_t NumTotalEv = sourcehist->GetEntries();
633 if(NumTotalEv < 1000)
635 cout <<Form(
"utilites.h/CreateSmartPojection::ERROR: Too low events in \"%s\" hist: %i",sourcehist->GetName(), NumTotalEv)<<endl;
639 TH2 *rebinedhist = sourcehist->Rebin2D(rebinX, rebinY, Form(
"%s_rebin",sourcehist->GetName()) );
641 if(option.Contains(
"s"))rebinedhist->Smooth(3);
643 Int_t NumBinsY = rebinedhist->GetNbinsY();
644 Int_t NumBinsX = rebinedhist->GetNbinsX();
647 Int_t AverageEvInBinX = 0, bincalculated = 0, integral;
648 for(Int_t xbin = 1; xbin <= NumBinsX; xbin++)
650 integral = rebinedhist->Integral(xbin,xbin,1,NumBinsY);
651 if(0 < integral){ AverageEvInBinX += integral; bincalculated++;}
653 AverageEvInBinX = ceil((Float_t)AverageEvInBinX / (Float_t)bincalculated);
656 if(
uVERBOSE >= 4)cout <<Form(
"utilites.h/CreateSmartPojection: Average events in bin: %i", AverageEvInBinX)<<endl;
659 Int_t firstBinX = -1, lastBinX = -1;
660 for(Int_t xbin = 1; xbin <= NumBinsX; xbin++)
663 if( (ceil(minbinevedge*AverageEvInBinX) <= rebinedhist->Integral(xbin,xbin,1,NumBinsY)) &&
664 ( minBinEvOnEdge <= rebinedhist->Integral(xbin,xbin,1,NumBinsY)) ) firstBinX = xbin;
667 if( (ceil(minbinevedge*AverageEvInBinX) <= rebinedhist->Integral(NumBinsX-xbin+1,NumBinsX-xbin+1,1,NumBinsY)) &&
668 ( minBinEvOnEdge <= rebinedhist->Integral(NumBinsX-xbin+1,NumBinsX-xbin+1,1,NumBinsY))) lastBinX = NumBinsX-xbin+1;
670 if( (lastBinX > 0) && (firstBinX > 0) )
break;
673 if(
uVERBOSE >= 4)cout <<Form(
"utilites.h/CreateSmartPojection: FIRST BIN: %i, %.1f (%.1f ev); LAST BIN: %i, %.1f (%.1f ev)",
674 firstBinX, rebinedhist->GetXaxis()->GetBinCenter(firstBinX), rebinedhist->Integral(firstBinX,firstBinX,1,NumBinsY),
675 lastBinX, rebinedhist->GetXaxis()->GetBinCenter( lastBinX), rebinedhist->Integral(lastBinX, lastBinX, 1,NumBinsY))<<endl;
676 if(option.Contains(
"r"))
if(
uVERBOSE >= 4)cout <<Form(
"Bin before rebinning: %i", lastBinX-firstBinX)<<endl;
679 Float_t *NewBinning =
new Float_t[lastBinX-firstBinX+2];
680 Int_t eventsINbin = 0, numberOfBins = 0;
682 if(
uVERBOSE >= 5)cout <<Form(
"BINNING: ");
683 NewBinning[0] = rebinedhist->GetXaxis()->GetBinLowEdge(firstBinX);
684 for(Int_t xbin = firstBinX+1; xbin <= lastBinX; xbin++)
686 eventsINbin += rebinedhist->Integral(xbin,xbin,1,NumBinsY);
688 if( (eventsINbin >= AverageEvInBinX*minbinevX) || (xbin == lastBinX) || (!option.Contains(
"r")))
691 NewBinning[numberOfBins] = rebinedhist->GetXaxis()->GetBinUpEdge(xbin);
692 if(
uVERBOSE >= 5)cout <<Form(
"%.1f ", NewBinning[numberOfBins]);
697 if(option.Contains(
"r"))
if(
uVERBOSE >= 4)cout <<Form(
"Bin after rebining: %i", numberOfBins)<<endl;
699 if(numberOfBins <= 0)
701 if(
uVERBOSE >= 1)cout <<Form(
"utilites.h/CreateSmartPojection: Binning failure, number of bins: %i", numberOfBins)<<endl;
706 TH1 *ptrProjection =
new TH1F(Form(
"%s_smProj_%s", sourcehist->GetName(),(IsDrawSIGMA)?
"sigma":
"mean"),
"title",numberOfBins,0,1);
707 ptrProjection->GetXaxis()->Set(numberOfBins, NewBinning);
708 ptrProjection->SetOption(
"E1");
709 ptrProjection->SetTitle(Form(
"[%s projection%s] %s",
710 (IsDrawSIGMA)?
"SIGMA":
"MEAN", (IsFitProjection)?
" by fit":
"", sourcehist->GetTitle()));
711 if(IsFitProjection && IsDrawSIGMA)
713 ptrProjection->SetMarkerStyle(22);
717 Int_t firstbin, lastbin;
718 Double_t mean = 0., sigma = 0., sigmaERROR = 0., meanERROR = 0.;
720 static Int_t monitor_slices_number = 0;
722 for(Int_t bin = 0; bin < numberOfBins; bin++)
724 ptrProjectionY = NULL;
725 mean = sigma = sigmaERROR = 0.0001;
726 firstbin = rebinedhist->GetXaxis()->FindBin( NewBinning[bin] );
727 lastbin = rebinedhist->GetXaxis()->FindBin( NewBinning[bin+1] );
729 ptrProjectionY = rebinedhist->ProjectionY(Form(
"%i_%s_slice_bin%i_binmin%i_binmax%i",
730 monitor_slices_number++,(IsDrawSIGMA)?
"SIGMA":
"MEAN",bin, firstbin,lastbin),firstbin,lastbin);
731 ptrProjectionY->SetTitle( Form(
"[proj slice: minX %.2f, maxX %.2f] %s", NewBinning[bin], NewBinning[bin+1], ptrProjection->GetTitle()));
735 ptrProjectionY->Rebin(ProjectionRebin);
744 meanERROR = ptrProjectionY->GetFunction(
fit_funk_name)->GetParError(1);
745 sigmaERROR = ptrProjectionY->GetFunction(
fit_funk_name)->GetParError(2);
750 mean = ptrProjectionY->GetMean();
751 sigma = ptrProjectionY->GetRMS();
752 meanERROR = ptrProjectionY->GetMeanError();
753 sigmaERROR = ptrProjectionY->GetRMSError();
756 if(monitor_slices_array) monitor_slices_array->Add(ptrProjectionY);
757 else delete ptrProjectionY;
759 }
else { cout <<Form(
"utilites.h/CreateSmartPojection: WARING: ProjectionY \"%s\" was NOT created", Form(
"QTCCFDslice_bin_%i_%i",firstbin,lastbin) )<<endl; }
761 ptrProjection->SetBinContent(bin+1,(IsDrawSIGMA) ? sigma : mean);
762 ptrProjection->SetBinError(bin+1, (IsDrawSIGMA) ? sigmaERROR*0.5 : meanERROR*0.5);
766 if(monitor_slices_array) monitor_slices_array->Add(rebinedhist);
767 else delete rebinedhist;
770 return ptrProjection;
773TObjArray*
HDraw(
const TObjArray *Histogramms_arr, TString name =
"",
774 int Hr = 4,
int Vt = 4,
const int Pair = 1,
775 int min = 0,
int max = 100,
int Log = 0,
int Leg = 0,
const TString param =
"",
int Norm = 0)
777 gStyle->SetPalette(1);
780 int H_Last = Histogramms_arr->GetLast();
781 if(H_Last <= 0)
return NULL;
784 if(
min > H_Last)
min = H_Last;
786 if(
max > H_Last)
max = H_Last;
789 if((Leg == 2)&&(Hr*Vt == 1))Leg = 0;
791 if(name ==
"") name = Histogramms_arr->GetName();
793 TObjArray *Canvas_arr =
new TObjArray();
794 Canvas_arr->SetOwner();
798 for(
int t=
min; t <=
max; t++){
806 for(
int t=
min; t <=
max; t++){
809 char n_Canv[30]; sprintf(n_Canv,
"%s#%d",name.Data(),Canvas_arr->GetLast()+1);
810 Canvas_arr->Add(
new TCanvas(n_Canv, n_Canv, 2000, 2000) );
811 ((TCanvas*)Canvas_arr->Last())->Divide(Hr,Vt,0.0001,0.0001);
815 ((TCanvas*)Canvas_arr->Last())->cd(flagD);
817 if(!Norm){ ((TH1F*)Histogramms_arr->At(t))->
Draw(param.Data()); }
820 ((TH1F*)Histogramms_arr->At(t))->DrawNormalized(param.Data(), 1);
821 for(
int c_Pair = 1; c_Pair < Pair; c_Pair++){
824 if(!Norm){ ((TH1F*)Histogramms_arr->At(t))->
Draw(
"sames");}
827 ((TH1F*)Histogramms_arr->At(t))->DrawNormalized(
"sames", 1);
831 if((Leg == 2)&&(flagD == 1)){
832 gPad -> BuildLegend(0,0,1,1);
835 ((TCanvas*)Canvas_arr->Last())->cd(flagD);
837 if(!Norm) ((TH1F*)Histogramms_arr->At(t))->
Draw(param.Data());
840 ((TH1F*)Histogramms_arr->At(t))->DrawNormalized(param.Data(),1);
841 for(
int c_Pair = 1; c_Pair < Pair; c_Pair++){
844 if(!Norm) ((TH1F*)Histogramms_arr->At(t))->
Draw(
"sames");
847 ((TH1F*)Histogramms_arr->At(t))->DrawNormalized(
"sames", 1);
852 if(Log == 1) gPad -> SetLogy(1);
853 if(Log == 2) gPad -> SetLogz(1);