8 #include <TClonesArray.h>
11 #include <TGraphErrors.h>
13 #include <TMultiGraph.h>
15 #include <TObjectTable.h>
16 #include <TDatabasePDG.h>
18 #include <AliMultiDimVector.h>
19 #include "AliHFMassFitter.h"
20 #include <AliSignificanceCalculator.h>
33 Double_t sigmaCut=0.035;//0.03;
34 Double_t errSgnCut=0.4;//0.35;
35 Double_t nSigmaMeanCut=4.;//3.;
37 ofstream outcheck("output.dat");
38 ofstream outdetail("discarddetails.dat");
40 Bool_t Data(TH1F* h,Double_t* rangefit,Bool_t writefit,Double_t& sgn, Double_t& errsgn, Double_t& bkg, Double_t& errbkg, Double_t& sgnf, Double_t& errsgnf, Double_t& sigmafit, Int_t& status);
41 Bool_t MC(TH1F* hs,TH1F* hb, Double_t& sgn, Double_t& errsgn, Double_t& bkg, Double_t& errbkg, Double_t& sgnf, Double_t& errsgnf, Double_t& sigmaused, Int_t& status);
49 //- 5 = kLambdactopKpi
51 //Note: writefit=kTRUE writes the root files with the fit performed but it also draw all the canvas, so if your computer is not powerfull enough I suggest to run it in batch mode (root -b)
53 Bool_t charmCutsOptimization(Bool_t isData=kTRUE,TString part="both"/*"A" anti-particle, "P" particle*/,TString centr="no",Bool_t writefit=kTRUE,Int_t minentries=50,Double_t *rangefit=0x0){
55 gStyle->SetFrameBorderMode(0);
56 gStyle->SetCanvasColor(0);
57 gStyle->SetFrameFillColor(0);
58 gStyle->SetTitleFillColor(0);
59 gStyle->SetStatColor(0);
62 TString filename="AnalysisResults.root",dirname="PWG3_D2H_Significance",listname="coutputSig",mdvlistname="coutputmv";
64 TString hnamemass="hMass_",hnamesgn="hSig_",hnamebkg="hBkg_";
99 cout<<decCh<<" is not allowed as decay channel "<<endl;
102 mass=TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
105 listname.Append(part);
106 mdvlistname.Append(part);
109 listname.Append(centr);
110 mdvlistname.Append(centr);
112 cout<<"Mass = "<<mass<<endl;
114 Int_t countFitFail=0,countSgnfFail=0,countNoHist=0,countBkgOnly=0;
116 outcheck<<"ptbin\tmdvGlobAddr\thistIndex\tSignif\tS\tB"<<endl;
117 outdetail<<"ptbin\tmdvGlobAddr\thistIndex\trelErrS\t\tmean_F-mass (mass = "<<mass<<")"<<endl;
118 TFile *fin=new TFile(filename.Data());
120 cout<<"File "<<filename.Data()<<" not found"<<endl;
124 TDirectoryFile *dir=(TDirectoryFile*)fin->GetDirectory(dirname);
126 cout<<"Directory "<<dirname<<" not found"<<endl;
130 TList* histlist= (TList*)dir->Get(listname);
132 cout<<listname<<" doesn't exist"<<endl;
136 TList* listamdv= (TList*)dir->Get(mdvlistname);
138 cout<<mdvlistname<<" doesn't exist"<<endl;
142 TH1F* hstat=(TH1F*)histlist->FindObject("fHistNEvents");
143 TCanvas *cst=new TCanvas("hstat","Summary of statistics");
147 hstat->Draw("htext0");
148 cst->SaveAs("hstat.png");
150 cout<<"Warning! fHistNEvents not found in "<<listname.Data()<<endl;
154 TH1F* htestIsMC=(TH1F*)histlist->FindObject("hSig_0");
155 if(htestIsMC) isMC=kTRUE;
157 Int_t nptbins=listamdv->GetEntries();
158 Int_t nhist=(histlist->GetEntries()-1);//-1 because of fHistNevents
159 if(isMC) nhist/=4; ///4 because hMass_, hSgn_,hBkg_,hRfl_
161 Int_t *indexes= new Int_t[nhist];
162 //initialize indexes[i] to -1
163 for(Int_t i=0;i<nhist;i++){
167 TFile* fout=new TFile(Form("outputSignifMaxim.root"),"recreate");
169 TH1F** hSigma=new TH1F*[nptbins];
170 TH1F* hstatus=new TH1F("hstatus","Flag status",6,-0.5,5.5);
171 hstatus->GetXaxis()->SetBinLabel(1,"fit fail");
172 hstatus->GetXaxis()->SetBinLabel(2,"fit ok and good results");
173 hstatus->GetXaxis()->SetBinLabel(3,"quality requirements not satisfied");
174 hstatus->GetXaxis()->SetBinLabel(4,"only bkg fit ok");
175 hstatus->GetXaxis()->SetBinLabel(5,"negative signif");
176 hstatus->GetXaxis()->SetBinLabel(6,Form("< %d entries",minentries));
178 //Check wheter histograms are filled
180 for(Int_t i=0;i<nhist;i++){
181 TString name=Form("%s%d",hnamemass.Data(),i);
182 TH1F* h=(TH1F*)histlist->FindObject(name.Data());
185 cout<<name<<" not found"<<endl;
189 if(h->GetEntries()>minentries){
190 //cout<<"Entries = "<<h->GetEntries()<<endl;
191 if (h->Integral() > minentries){
192 cout<<i<<") Integral = "<<h->Integral()<<endl;
200 cout<<"There are "<<count<<" histogram with more than "<<minentries<<" entries"<<endl;
202 cout<<"No histogram to draw..."<<endl;
206 //create multidimvectors
208 //for(Int_t i=0;i<1;i++){
209 for(Int_t i=0;i<nptbins;i++){
211 //multidimvectors for signal
212 AliMultiDimVector *mdvS=(AliMultiDimVector*)listamdv->FindObject(Form("multiDimVectorPtBin%d",i));
213 TString name=mdvS->GetName(),nameErr="err",setname="";
215 setname=Form("S%s",name.Data());
216 mdvS->SetName(setname.Data());
217 outcheck<<"\n"<<mdvS->GetPtLimit(0)<<" < Pt <"<<mdvS->GetPtLimit(1)<<endl;
219 AliMultiDimVector *mdvSerr=(AliMultiDimVector*)mdvS->Clone(setname.Data());
220 setname=Form("%sS%s",nameErr.Data(),name.Data());
221 mdvSerr->SetName(setname.Data());
223 //multidimvectors for background
224 setname=Form("B%s",name.Data());
225 AliMultiDimVector *mdvB=(AliMultiDimVector*)mdvS->Clone(setname.Data());
227 AliMultiDimVector *mdvBerr=(AliMultiDimVector*)mdvS->Clone(setname.Data());
228 setname=Form("%sB%s",nameErr.Data(),name.Data());
229 mdvBerr->SetName(setname.Data());
231 //multidimvectors for significance
232 setname=Form("Sgf%s",name.Data());
233 AliMultiDimVector *mdvSgnf=(AliMultiDimVector*)mdvS->Clone(setname.Data());
235 AliMultiDimVector *mdvSgnferr=(AliMultiDimVector*)mdvS->Clone(setname.Data());
236 setname=Form("%sSgf%s",nameErr.Data(),name.Data());
237 mdvSgnferr->SetName(setname.Data());
239 hSigma[i]=new TH1F(Form("hSigmapt%d",i),Form("Sigma distribution pt bin %d (%.1f < pt < %.1f)",i,mdvSgnf->GetPtLimit(0),mdvSgnf->GetPtLimit(1)), 200,0.,0.05);
241 Int_t nhistforptbin=mdvS->GetNTotCells();
242 //Int_t nvarsopt=mdvS->GetNVariables();
244 cout<<"nhistforptbin = "<<nhistforptbin<<endl;
246 //loop on all histograms and do AliHFMassFitter
247 //for(Int_t ih=0;ih<1;ih++){
248 for(Int_t ih=0;ih<nhistforptbin;ih++){
249 printf("Analyzing indexes[%d] = %d \n",ih+i*nhistforptbin,indexes[ih+i*nhistforptbin]);
251 if(isData && indexes[ih+i*nhistforptbin] == -1) {
253 mdvSgnferr->SetElement(ih,0);
254 mdvS->SetElement(ih,0);
255 mdvSerr->SetElement(ih,0);
256 mdvB->SetElement(ih,0);
257 mdvBerr->SetElement(ih,0);
261 outcheck<<i<<"\t\t "<<ih<<"\t\t"<<indexes[ih+i*nhistforptbin];
265 Double_t signif=0, signal=0, background=0, errSignif=0, errSignal=0, errBackground=0,sigmafit=0;
268 name=Form("%s%d",hnamemass.Data(),indexes[ih+i*nhistforptbin]);
269 h=(TH1F*)histlist->FindObject(name.Data());
271 Data(h,rangefit,writefit,signal,errSignal,background,errBackground,signif,errSignif,sigmafit,status);
273 name=Form("%s%d",hnamesgn.Data(),ih+i*nhistforptbin);
274 h=(TH1F*)histlist->FindObject(name.Data());
276 cout<<name.Data()<<" not found"<<endl;
279 name=Form("%s%d",hnamebkg.Data(),ih+i*nhistforptbin);
280 g=(TH1F*)histlist->FindObject(name.Data());
282 cout<<name.Data()<<" not found"<<endl;
285 MC(h,g,signal,errSignal,background,errBackground,signif,errSignif,sigmafit,status);
287 hstatus->Fill(status);
290 mdvSgnf->SetElement(ih,signif);
291 mdvSgnferr->SetElement(ih,errSignif);
292 mdvS->SetElement(ih,signal);
293 mdvSerr->SetElement(ih,errSignal);
294 mdvB->SetElement(ih,background);
295 mdvBerr->SetElement(ih,errBackground);
296 hSigma[i]->Fill(sigmafit);
299 mdvSgnf->SetElement(ih,0);
300 mdvSgnferr->SetElement(ih,0);
301 mdvS->SetElement(ih,0);
302 mdvSerr->SetElement(ih,0);
303 mdvB->SetElement(ih,0);
304 mdvBerr->SetElement(ih,0);
306 mdvB->SetElement(ih,background);
307 mdvBerr->SetElement(ih,errBackground);
329 TCanvas *cinfo=new TCanvas("cinfo","Status");
332 hstatus->Draw("htext0");
339 outcheck<<"\nSummary:\n - Total number of histograms: "<<nhist<<"\n - "<<count<<" histograms with more than "<<minentries<<" entries; \n - Too few entries in histo "<<countNoHist<<" times;\n - Fit failed "<<countFitFail<<" times \n - no sense Signal/Background/Significance "<<countSgnfFail<<" times\n - only background "<<countBkgOnly<<" times"<<endl;
344 //this function fit the hMass histograms
345 //status = 0 -> fit fail
346 // 1 -> fit ok and good results
347 // 2 -> quality requirements not satisfied, try to fit with bkg only
348 // 3 -> only bkg fit ok
349 // 4 -> negative signif
350 // 5 -> not enough entries in the hisotgram
351 Bool_t Data(TH1F* h,Double_t* rangefit,Bool_t writefit, Double_t& sgn, Double_t& errsgn, Double_t& bkg, Double_t& errbkg, Double_t& sgnf, Double_t& errsgnf, Double_t& sigmafit, Int_t& status){
352 Int_t nbin=h->GetNbinsX();
353 Double_t min=h->GetBinLowEdge(7);
354 Double_t max=h->GetBinLowEdge(nbin-5)+h->GetBinWidth(nbin-5);
356 min=h->GetBinLowEdge(1);
357 max=h->GetBinLowEdge(nbin+1);
364 AliHFMassFitter fitter(h,min, max,rebin,fitbtype);
365 fitter.SetInitialGaussianMean(mass);
366 fitter.SetInitialGaussianSigma(sigma);
368 //if(ih==0) fitter.InitNtuParam(Form("ntuPtbin%d",i));
369 // fitter.SetHisto(h);
370 // fitter.SetRangeFit(min,max);
371 //fitter.SetRangeFit(1.68,2.05);
373 //fitter.SetType(fitbtype,0);
375 Bool_t ok=fitter.MassFitter(kFALSE);
377 cout<<"FIT NOT OK!"<<endl;
379 //outcheck<<i<<"\t\t "<<ih<<"\t\t"<<indexes[ih+i*nhistforptbin]<<"\t 0\t xxx"<<"\t bkgonly"<<endl;
380 outcheck<<"\t 0\t xxx"<<"\t failed"<<endl;
385 if(writefit) fitter.WriteCanvas(h->GetName(),"./",nsigma);
386 fitter.Signal(nsigma,sgn,errsgn);
387 fitter.Background(nsigma,bkg,errbkg);
388 Double_t meanfit=fitter.GetMean();
389 sigmafit=fitter.GetSigma();
392 //if(ok==kTRUE && ( (sigmafit < 0.03) || (sigmafit < 0.04 && mdvS->GetPtLimit(0)>8.)) && sgn > 0 && bkg > 0){
393 if(ok==kTRUE && ( (sigmafit < sigmaCut) ) && sgn > 0 && bkg > 0){
394 Double_t errmeanfit=fitter.GetMassFunc()->GetParError(fitter.GetNFinalPars()-2);
395 fitter.Significance(nsigma,sgnf,errsgnf);
398 if(errsgn/sgn < errSgnCut && /*TMath::Abs(meanfit-mass)<0.015*/TMath::Abs(meanfit-mass)/errmeanfit < nSigmaMeanCut){
399 //outcheck<<i<<"\t\t "<<ih<<"\t\t"<<indexes[ih+i*nhistforptbin]<<"\t"<<signif<<" +- "<<errSignif<<"\t"<<sgn<<" +- "<<errsgn<<"\t"<<bkg<<" +- "<<errbkg<<endl;
400 outcheck<<"\t\t "<<sgnf<<" +- "<<errsgnf<<"\t"<<sgn<<" +- "<<errsgn<<"\t"<<bkg<<" +- "<<errbkg<<endl;
405 //outdetail<<i<<"\t\t "<<ih<<"\t\t"<<indexes[ih+i*nhistforptbin]<<"\t"<<errsgn/sgn<<"\t\t "<<(meanfit-mass)/errmeanfit<<endl;
406 outdetail<<"\t\t "<<errsgn/sgn<<"\t\t "<<(meanfit-mass)/errmeanfit<<endl;
407 ok=fitter.RefitWithBkgOnly(kFALSE);
411 Double_t bkg=0,errbkg=0.;
412 Double_t nsigmarange[2]={mass-nsigma*sigma,mass+nsigma*sigma};
413 fitter.Background(nsigmarange[0],nsigmarange[1],bkg,errbkg);
414 //outcheck<<i<<"\t\t "<<ih<<"\t\t"<<indexes[ih+i*nhistforptbin]<<"\t 0\t "<<bkg <<"\t bkgonly"<<endl;
415 outcheck<<"\t\t 0\t "<<bkg <<"\t bkgonly"<<endl;
417 //outdetail<<i<<"\t\t "<<ih<<"\t\tnot able to refit with bkg obly"<<endl;
418 outdetail<<"\t\t \t\tnot able to refit with bkg obly"<<endl;
427 cout<<"Setting to 0 (fitter results meaningless)"<<endl;
428 outcheck<<"\t S || B || sgnf negative";
440 //this function counts the entries in hSgn and hBgk
441 Bool_t MC(TH1F* hs,TH1F* hb, Double_t& sgn, Double_t& errsgn, Double_t& bkg, Double_t& errbkg, Double_t& sgnf, Double_t& errsgnf, Double_t& sigmaused, Int_t& status){
443 //do we want to use a fixed sigma or take the standard deviation of the signal histogram?
444 sigmaused=hs->GetRMS();
447 Double_t nsigmarange[2]={mass-nsigma*sigmaused,mass+nsigma*sigmaused};
448 cout<<"from "<<nsigmarange[0]<<" to "<<nsigmarange[1]<<endl;
450 Int_t binnsigmarange[2]={hs->FindBin(nsigmarange[0]),hs->FindBin(nsigmarange[1])};//for bkg histo it's the same
451 cout<<"bins "<<binnsigmarange[0]<<" e "<<binnsigmarange[1]<<endl;
453 sgn=hs->Integral(binnsigmarange[0],binnsigmarange[1]);
454 errsgn=TMath::Sqrt(sgn);
455 bkg=hb->Integral(binnsigmarange[0],binnsigmarange[1]);
456 errbkg=TMath::Sqrt(bkg);
457 if(sgn+bkg>0.) sgnf=sgn/TMath::Sqrt(sgn+bkg);
462 errsgnf=TMath::Sqrt(sgnf*sgnf/(sgn+bkg)/(sgn+bkg)*(1/4.*errsgn*errsgn+errbkg*errbkg)+sgnf*sgnf/sgn/sgn*errsgn*errsgn);
468 //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
470 // which=0 plot significance
472 // =2 plot background
474 // maximize = kTRUE (default) if you want to fix the step of the variables not shown to the value that maximize the significance. Note that these values are saved in fixedvars.dat
475 // readfromfile = kTRUE (default is kFALSE) if you want to read the value fixed in a previous run of this function (e.g. significance or signal maximization)
478 void showMultiDimVector(Int_t n=2,Int_t which=0, Bool_t plotErrors=kFALSE, Bool_t maximize=kTRUE,Bool_t readfromfile=kFALSE, Bool_t fixedrange=kFALSE){
480 gStyle->SetCanvasColor(0);
481 gStyle->SetFrameFillColor(0);
482 gStyle->SetTitleFillColor(0);
483 gStyle->SetOptStat(0);
484 //gStyle->SetOptTitle(0);
485 gStyle->SetFrameBorderMode(0);
487 if((maximize && readfromfile) || (!maximize && !readfromfile)){
488 cout<<"Error! maximize & readfromfile cannot be both kTRUE or kFALSE"<<endl;
492 TFile* fin=new TFile("outputSignifMaxim.root");
494 cout<<"outputSignifMaxim.root not found"<<endl;
499 cout<<"Error! cannot show "<<n+1<<" dimentions"<<endl;
503 TString name,title,namebis,shorttitle;
506 name="SgfmultiDimVectorPtBin";
507 title="Significance";
511 name="SmultiDimVectorPtBin";
516 name="BmultiDimVectorPtBin";
521 name="SmultiDimVectorPtBin";
522 namebis="BmultiDimVectorPtBin";
523 title="Signal over Background ";
527 // name="errBmultiDimVectorPtBin";
528 // title="Background (error)";
532 if(plotErrors && which!=3 && n==2){
534 title.Append(" Error") ;
535 shorttitle.Append("Err");
540 for(Int_t ip=0;ip<=nptbins;ip++){
541 TString mdvname=Form("%s%d",name.Data(),ip);
542 AliMultiDimVector* mdv=(AliMultiDimVector*)fin->Get(mdvname);
545 cout<<"Number of pt bins "<<ip<<endl;
550 cout<<"Projecting "<<title.Data()<<" with respect to the maximization variable(s) [chose]"<<endl;
552 Int_t variable[2]; //no more than 2D
553 TString mdvname=Form("%s0",name.Data()), mdverrname="";//, mdvnamebis="", mdverrnamebis="";
554 AliMultiDimVector* mdv=(AliMultiDimVector*)fin->Get(mdvname);
555 AliMultiDimVector* mdverr=0x0;
557 cout<<mdvname.Data()<<" not found"<<endl;
562 Int_t nvarsopt=mdv->GetNVariables();
563 //Int_t nfixed=nvarsopt-n;
564 Int_t fixedvars[nvarsopt];
565 Int_t allfixedvars[nvarsopt*nptbins];
567 fstream writefixedvars;
569 //open file in read mode
570 writefixedvars.open("fixedvars.dat",ios::in);
572 while(writefixedvars){
573 writefixedvars>>allfixedvars[longi];
578 //open file in write mode
579 writefixedvars.open("fixedvars.dat",ios::out);
582 //ask variables for projection
583 for(Int_t k=0;k<nvarsopt;k++){
584 cout<<k<<" "<<mdv->GetAxisTitle(k)<<endl;
586 cout<<"Choose "<<n<<" variable(s)"<<endl;
587 for(Int_t j=0;j<n;j++){
588 cout<<"var"<<j<<": ";
591 if(n==1) variable[1]=999;
593 TCanvas* cvpj=new TCanvas(Form("proj%d",variable[0]),Form("%s wrt %s",title.Data(),(mdv->GetAxisTitle(variable[0])).Data()));
595 TMultiGraph* mg=new TMultiGraph(Form("proj%d",variable[0]),Form("%s wrt %s;%s;%s",title.Data(),(mdv->GetAxisTitle(variable[0])).Data(),(mdv->GetAxisTitle(variable[0])).Data(),title.Data()));
596 TLegend *leg=new TLegend(0.7,0.2,0.9,0.6,"Pt Bin");
597 leg->SetBorderSize(0);
598 leg->SetFillStyle(0);
602 Float_t axisrange[2*nptbins];
604 //open file in read mode
605 writerange.open("rangehistos.dat",ios::in);
608 writerange>>axisrange[longi];
613 //open file in write mode
614 writerange.open("rangehistos.dat",ios::out);
617 for (Int_t i=0;i<nptbins;i++){ //loop on ptbins
618 cout<<"\nPtBin = "<<i<<endl;
620 //using AliSignificanceCalculator
622 TString nameS,nameB,nameerrS,nameerrB;
623 nameS.Form("SmultiDimVectorPtBin%d",i);
624 nameerrS.Form("errSmultiDimVectorPtBin%d",i);
625 nameB.Form("BmultiDimVectorPtBin%d",i);
626 nameerrB.Form("errBmultiDimVectorPtBin%d",i);
628 AliMultiDimVector* mdvS=(AliMultiDimVector*)fin->Get(nameS.Data());
629 AliMultiDimVector* mdvB=(AliMultiDimVector*)fin->Get(nameB.Data());
630 AliMultiDimVector* mdvBerr=(AliMultiDimVector*)fin->Get(nameerrS.Data());
631 AliMultiDimVector* mdvSerr=(AliMultiDimVector*)fin->Get(nameerrB.Data());
632 if(!(mdvS && mdvB && mdvSerr && mdvBerr)){
633 cout<<"one of the multidimvector is not present"<<endl;
637 AliSignificanceCalculator *cal=new AliSignificanceCalculator(mdvS,mdvB,mdvSerr,mdvBerr,1.,1.);
639 AliMultiDimVector* mvess=cal->GetSignificanceError();
640 AliMultiDimVector* mvpur=cal->CalculatePurity();
641 AliMultiDimVector* mvepur=cal->CalculatePurityError();
643 Int_t ncuts=mdvS->GetNVariables();
644 Int_t *maxInd=new Int_t[ncuts];
645 Float_t *cutvalues=new Float_t[ncuts];
647 for(Int_t ind=0;ind<ncuts;ind++)maxInd[ind]=0;
649 Float_t sigMax0=cal->GetMaxSignificance(maxInd,0); //look better into this!!
650 for(Int_t ic=0;ic<ncuts;ic++){
651 cutvalues[ic]=((AliMultiDimVector*)fin->Get(nameS.Data()))->GetCutValue(ic,maxInd[ic]);
653 //setting step of fixed variables
654 if(readfromfile){ //from file
655 fixedvars[ic]=allfixedvars[i+ic];
658 if(maximize) { //using the values which maximize the significance
659 fixedvars[ic]=maxInd[ic];
660 //write to output fixedvars.dat
661 writefixedvars<<fixedvars[ic]<<"\t";
664 //output file: return after each pt bin
665 if(maximize) writefixedvars<<endl;
667 printf("Maximum of significance for Ptbin %d found in bin:\n",i);
668 for(Int_t ic=0;ic<ncuts;ic++){
669 printf(" %d\n",maxInd[ic]);
670 printf("corresponding to cut:\n");
671 printf(" %f\n",cutvalues[ic]);
674 printf("Significance = %f +- %f\n",sigMax0,mvess->GetElement(maxInd,0));
675 printf("Purity = %f +- %f\n",mvpur->GetElement(maxInd,0),mvepur->GetElement(maxInd,i));
679 mdv=cal->CalculateSOverB();
680 if(!mdv)cout<<mdv->GetName()<<" null"<<endl;
682 mdverr=cal->CalculateSOverBError();
683 if(!mdverr)cout<<mdverr->GetName()<<" null"<<endl;
687 mdvname=Form("%s%d",name.Data(),i);
688 mdv=(AliMultiDimVector*)fin->Get(mdvname);
689 if(!mdv)cout<<mdvname.Data()<<" not found"<<endl;
691 //multidimvector of errors
692 mdverrname=Form("err%s%d",name.Data(),i);
693 mdverr=(AliMultiDimVector*)fin->Get(mdverrname);
694 if(!mdverr)cout<<mdverrname.Data()<<" not found"<<endl;
696 TString ptbinrange=Form("%.1f < p_{t} < %.1f GeV/c",mdv->GetPtLimit(0),mdv->GetPtLimit(1));
699 gStyle->SetPalette(1);
700 TH2F* hproj=mdv->Project(variable[0],variable[1],fixedvars,0);
701 hproj->SetTitle(Form("%s wrt %s vs %s (Ptbin%d %.1f<pt<%.1f);%s;%s",title.Data(),(mdv->GetAxisTitle(variable[0])).Data(),mdv->GetAxisTitle(variable[1]).Data(),i,mdv->GetPtLimit(0),mdv->GetPtLimit(1),(mdv->GetAxisTitle(variable[0])).Data(),mdv->GetAxisTitle(variable[1]).Data()));
703 hproj->SetMinimum(axisrange[2*i]);
704 hproj->SetMaximum(axisrange[2*i+1]);
706 writerange<<hproj->GetMinimum()<<"\t"<<hproj->GetMinimum()<<endl;
709 TCanvas* cvpj=new TCanvas(Form("proj%d%dpt%d",variable[0],variable[1],i),Form("%s wrt %s vs %s (Ptbin%d)",title.Data(),(mdv->GetAxisTitle(variable[0])).Data(),mdv->GetAxisTitle(variable[1]).Data(),i));
711 hproj->DrawClone("COLZtext");
712 cvpj->SaveAs(Form("%s%s.png",shorttitle.Data(),cvpj->GetName()));
718 Int_t nbins=mdv->GetNCutSteps(variable[0]);
720 Double_t *x=new Double_t[nbins];
721 Double_t *y=new Double_t[nbins];
722 Double_t *errx=new Double_t[nbins];
723 Double_t *erry=new Double_t[nbins];
725 for(Int_t k=0;k<nbins;k++){ //loop on the steps (that is the bins of the graph)
730 fixedvars[variable[0]]=k; //variable[0] is the index of the variable on which we project. This variable must increase, the others have been set before
732 x[k]=mdv->GetCutValue(variable[0],k);
733 errx[k]=mdv->GetCutStep(variable[0])/2.;
735 y[k]=mdv->GetElement(fixedvars,0);
736 erry[k]=mdverr->GetElement(fixedvars,0);
740 cout<<mdv->GetAxisTitle(variable[0])<<" step "<<k<<" = "<<x[k]<<":"<<" y = "<<y[k]<<endl;
743 cout<<"----------------------------------------------------------"<<endl;
744 TGraphErrors* gr=new TGraphErrors(nbins,x,y,errx,erry);
745 gr->SetMarkerStyle(20+i);
746 gr->SetMarkerColor(i+1);
747 gr->SetLineColor(i+1);
749 gr->SetMarkerColor(i+2);
750 gr->SetLineColor(i+2);
754 gr->SetName(Form("g1%d",i));
756 leg->AddEntry(gr,ptbinrange.Data(),"p");
764 cvpj->SaveAs(Form("%s%s.png",shorttitle.Data(),cvpj->GetName()));
768 //draw sigma as a function of cuts
770 void DrawSigmas(TH2F* h2cuts){
772 TString fittype="ExpFit";
774 if(fittype=="Pol2Fit") ntot=6;
775 Int_t ihfirst=0,ihlast=1; //change this (must think on it and remember what I wanted to do!)
776 for(Int_t ih=ihfirst;ih<ihlast;ih++){
777 fin=new TFile(Form("h%d%s.root",ih,fittype.Data()));
779 TCanvas *cv=(TCanvas*)fin->Get(Form("cv1%s%d",fittype.Data(),ih));
780 TF1* func=(TF1*)cv->FindObject("funcmass");
781 Int_t sigma=func->GetParameter(ntot-1);
782 //h2cuts->SetBinContent();
787 //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
788 // Some methods to get the index of the histogram corresponding to a set of cuts
790 // vct = the AliMultiDimVector correponding to ptbin: it can be from AnalysisResults.root or outputSignifMaxim.root
792 // indices = array of the index of the cut variables (the dimension of the array must be equal to the number of variables maximized)
794 Int_t GetNHistFromIndices(AliMultiDimVector* vct,Int_t ptbin,Int_t* indices){
795 cout<<"Calculating the index of the histogram corresponding to the following cut steps:"<<endl;
796 for(Int_t i=0;i<vct->GetNVariables();i++){
797 cout<<vct->GetAxisTitle(i)<<" --> "<<indices[i]<<endl;
799 cout<<"Pt bin "<<ptbin<<" from "<<vct->GetPtLimit(0)<<" to "<<vct->GetPtLimit(1)<<endl;
800 cout<<"Info: total number of cells per multidim: "<<vct->GetNTotCells()<<endl;
801 ULong64_t glindex=vct->GetGlobalAddressFromIndices(indices,0);
802 cout<<"The histogram you want is:\t";
803 return glindex+vct->GetNTotCells()*ptbin;
806 // vct = the AliMultiDimVector correponding to ptbin: it can be from AnalysisResults.root or outputSignifMaxim.root
808 // values = array of the cut values (the dimension of the array must be equal to the number of variables maximized)
810 Int_t GetNHistFromValues(AliMultiDimVector* vct,Int_t ptbin,Float_t* values){
811 cout<<"Calculating the index of the histogram corresponding to the following cut values:"<<endl;
812 for(Int_t i=0;i<vct->GetNVariables();i++){
813 cout<<vct->GetAxisTitle(i)<<" --> "<<values[i]<<endl;
815 cout<<"Pt bin "<<ptbin<<" from "<<vct->GetPtLimit(0)<<" to "<<vct->GetPtLimit(1)<<endl;
816 cout<<"Info: total number of cells per multidim: "<<vct->GetNTotCells()<<endl;
817 ULong64_t glindex=vct->GetGlobalAddressFromValues(values,0);
819 cout<<"The histogram you want is:\t"<<glindex+vct->GetNTotCells()*ptbin<<endl;
820 return glindex+vct->GetNTotCells()*ptbin;
823 // vct = the AliMultiDimVector correponding to ptbin: it can be from AnalysisResults.root or outputSignifMaxim.root
825 // values = array of the cut values: the dimention can be <= number of variables maximized
826 // valsgiven = array of dimention = to the number of variables optimized. For each variable put kTRUE if the value is given (in values), kFALSE otherwise
827 // nhistinrange = pass an integer which will contains the number of histogram returned (that is the dimention of the Int_t* returned)
829 //NB: Remember that the cut applied is the lower edge of the step where lower=looser
831 Int_t* GetRangeHistFromValues(AliMultiDimVector* vct,Int_t ptbin,Bool_t* valsgiven,Float_t* values,Int_t& nhistinrange){
835 Int_t nvar4opt=vct->GetNVariables();
836 Float_t allvals[nvar4opt];
838 for (Int_t i=0;i<nvar4opt;i++) {
839 if(valsgiven[i]==kTRUE) {
840 allvals[i]=values[nvargiven];
844 nhistinrange+=vct->GetNCutSteps(i);
845 allvals[i]=vct->GetCutValue(i,0);
846 //allvals[i]=vct->GetCutValue(0,i); //ivar,icell
849 cout<<nhistinrange<<" index will be returned"<<endl;
850 Int_t *rangeofhistos=new Int_t[nhistinrange];
853 rangeofhistos[0]=GetNHistFromValues(vct,ptbin,allvals);
854 cout<<"output"<<rangeofhistos[0]<<endl;
856 Int_t index[nvar4opt-nvargiven];
858 for (Int_t i=0;i<nvar4opt;i++){
859 if(valsgiven[i]==kFALSE) {
860 //cout<<"kTRUE==>"<<i<<endl;
866 for(Int_t i=0;i<nvar4opt-nvargiven;i++){ //loop on number of free variables
867 cout<<"Info: incrementing "<<vct->GetAxisTitle(index[i])<<endl;
868 for(Int_t j=0;j<vct->GetNCutSteps(i);j++){ //loop on steps of each free variable
869 allvals[index[i]]=vct->GetCutValue(index[i],j);
870 rangeofhistos[i*vct->GetNCutSteps(i)+j]=GetNHistFromValues(vct,ptbin,allvals);
874 return rangeofhistos;
877 // vct = the AliMultiDimVector correponding to ptbin: it can be from AnalysisResults.root or outputSignifMaxim.root
879 // nhist = number of the histogram from which you want to have the cut values (returned)
881 Float_t* GetCutValuesFromNHist(AliMultiDimVector* vct,Int_t ptbin,Int_t nhist){
882 ULong64_t totCells=vct->GetNTotCells();
883 ULong64_t globadd=nhist-ptbin*totCells;
884 const Int_t nvars=vct->GetNVariables();
885 Float_t* cuts=new Float_t[nvars];
887 vct->GetCutValuesFromGlobalAddress(globadd,cuts,ptinside);
892 // values = array of the cut values: the dimention can be <= number of variables maximized
893 // valsgiven = array of dimention = to the number of variables optimized. For each variable put kTRUE if the value is given (in values), kFALSE otherwise
896 void DrawPossibilities(Int_t ptbin,Bool_t* valsgiven,Float_t* values,TString path="./",Int_t decCh=1){
897 gStyle->SetFrameBorderMode(0);
898 gStyle->SetCanvasColor(0);
899 gStyle->SetFrameFillColor(0);
900 gStyle->SetOptStat(0);
903 TString filename="AnalysisResults.root";
904 TString dirname="PWG3_D2H_Significance",listname="coutputSig",mdvlistname="coutputmv";
907 TFile *fin=new TFile(Form("%s%s",path.Data(),filename.Data()));
909 cout<<path.Data()<<filename.Data()<<" not found"<<endl;
912 TDirectoryFile *dir=(TDirectoryFile*)fin->GetDirectory(dirname);
914 cout<<"Directory "<<dirname<<" not found"<<endl;
920 mdvlistname+="Dplus";
930 mdvlistname+="Dstar";
949 cout<<decCh<<" is not allowed as decay channel "<<endl;
955 TList* listamdv= (TList*)dir->Get(mdvlistname);
957 cout<<mdvlistname<<" doesn't exist"<<endl;
961 AliMultiDimVector* vct=(AliMultiDimVector*)listamdv->FindObject(Form("multiDimVectorPtBin%d",ptbin));
964 TString filehistname="";
965 Int_t* indexes=GetRangeHistFromValues(vct,ptbin,valsgiven,values,nhists);
966 for(Int_t i=0;i<nhists;i++){
968 fin2=new TFile(Form("%sh%dExpMassFit.root",path.Data(),indexes[i]));
970 cout<<"File "<<indexes[i]<<" not found!"<<endl;
973 TCanvas *cv=(TCanvas*)fin2->Get(Form("c1h%dExp",indexes[i]));
975 cout<<"Canvas c1h"<<indexes[i]<<"Exp not found among";
980 cv->SaveAs(Form("h%dExpMassFit.png",indexes[i]));
987 void Merge2Bins(Int_t b1, Int_t b2,TString pathin="./",Int_t decCh=1,TString part="both"/*"A" anti-particle, "P" particle*/){
990 printf("The bins to be merget must be consecutive. Check! [b1 = %d, b2= %d]\n",b1,b2);
994 TFile *fin=new TFile(Form("%sAnalysisResults.root",pathin.Data()));
996 cout<<"Input file not found"<<endl;
1000 TString dirname="PWG3_D2H_Significance",listname="coutputSig",mdvlistname="coutputmv";
1005 mdvlistname+="Dplus";
1013 mdvlistname+="Dstar";
1028 cout<<decCh<<" is not allowed as decay channel "<<endl;
1033 listname.Append(part);
1034 mdvlistname.Append(part);
1037 TDirectoryFile *dir=(TDirectoryFile*)fin->GetDirectory(dirname);
1039 cout<<"Directory "<<dirname<<" not found"<<endl;
1043 TList* histlist= (TList*)dir->Get(listname);
1045 cout<<listname<<" doesn't exist"<<endl;
1049 TList* listamdv= (TList*)dir->Get(mdvlistname);
1051 cout<<mdvlistname<<" doesn't exist"<<endl;
1054 if (!gSystem->AccessPathName(Form("merged%d%d",b1,b2))) gSystem->Exec(Form("mkdir merged%d%d",b1,b2));
1055 gSystem->Exec(Form("cd merged%d%d",b1,b2));
1057 TFile* fout=new TFile("mergeAnalysisResults.root","recreate");
1059 fout->mkdir(dirname);
1060 TList* listmdvout=new TList();
1061 listmdvout->SetName(listamdv->GetName());
1062 listmdvout->SetOwner();
1063 //listmdvout->SetTitle(listamdv->GetTitle());
1064 TList* histlistout=new TList();
1065 histlistout->SetName(histlist->GetName());
1066 histlistout->SetOwner();
1067 //histlistout->SetTitle(histlist->GetTitle());
1069 AliMultiDimVector* mdvin1=(AliMultiDimVector*)listamdv->FindObject(Form("multiDimVectorPtBin%d",b1));
1070 AliMultiDimVector* mdvin2=(AliMultiDimVector*)listamdv->FindObject(Form("multiDimVectorPtBin%d",b2));
1072 Int_t ntotHperbin = mdvin1->GetNTotCells();
1073 if(mdvin2->GetNTotCells() != ntotHperbin) {
1074 cout<<"Error! Number of histos in pt bin "<<b1<<" = "<<ntotHperbin<<" != Number of histos in pt bin "<<b2<<" = "<<mdvin2->GetNTotCells()<<endl;
1077 Int_t nvar1=mdvin1->GetNVariables();
1078 if(nvar1 != mdvin2->GetNVariables()){
1079 cout<<"Error! Mismatch in number of variables"<<endl;
1083 Float_t newptbins[2]={mdvin1->GetPtLimit(0),mdvin2->GetPtLimit(1)};
1084 Float_t loosercuts[nvar1], tightercuts[nvar1];
1085 TString axistitles[nvar1];
1086 Int_t ncells[nvar1];
1088 for (Int_t ivar=0;ivar<nvar1;ivar++){
1089 loosercuts[ivar]=mdvin1->GetCutValue(ivar,0);
1090 if(loosercuts[ivar] - mdvin2->GetCutValue(ivar,0) < 1e-8) printf("Warning! The loose cut %s is different between the 2: %f and %f\n",mdvin1->GetAxisTitle(ivar).Data(),loosercuts[ivar],mdvin2->GetCutValue(ivar,0));
1091 tightercuts[ivar]=mdvin1->GetCutValue(ivar,mdvin1->GetNCutSteps(ivar)-1);
1092 if(tightercuts[ivar] - mdvin2->GetCutValue(ivar,mdvin1->GetNCutSteps(ivar)-1) < 1e-8) printf("Warning! The tight cut %s is different between the 2: %f and %f\n",mdvin1->GetAxisTitle(ivar).Data(),tightercuts[ivar],mdvin2->GetCutValue(ivar,mdvin2->GetNCutSteps(ivar)));
1093 axistitles[ivar]=mdvin1->GetAxisTitle(ivar);
1094 cout<<axistitles[ivar]<<"\t";
1095 ncells[ivar]=mdvin1->GetNCutSteps(ivar);
1098 AliMultiDimVector* mdvout= new AliMultiDimVector(Form("multiDimVectorPtBin%d",b1),"MultiDimVector",1,newptbins,mdvin1->GetNVariables(),ncells,loosercuts,tightercuts,axistitles);
1099 cout<<"Info: writing mdv"<<endl;
1100 listmdvout->Add(mdvout);
1103 TH1F* htestIsMC=(TH1F*)histlist->FindObject("hSgn_0");
1104 if(htestIsMC) isMC=kTRUE;
1105 Int_t nptbins=listamdv->GetEntries();
1106 Int_t nhist=(histlist->GetEntries()-1);//-1 because of fHistNevents
1107 if(isMC) nhist/=4; ///4 because hMass_, hSgn_,hBkg_,hRfl_
1109 cout<<"Merging bin from "<<mdvin1->GetPtLimit(0)<<" to "<<mdvin1->GetPtLimit(1)<<" and from "<<mdvin2->GetPtLimit(0)<<" to "<<mdvin2->GetPtLimit(1)<<endl;
1110 Int_t firsth1=b1*ntotHperbin,firsth2=b2*ntotHperbin; //firsth2 = (b1+1)*ntotHperbin
1111 Int_t lasth1=firsth1+ntotHperbin-1,lasth2=firsth2+ntotHperbin-1;
1112 cout<<"Histograms from "<<firsth1<<" to "<<lasth1<<" and "<<firsth2<<" to "<<lasth2<<endl;
1114 //add the others mdv to the list
1116 for(Int_t i=0;i<nptbins;i++){
1118 AliMultiDimVector* vcttmp=(AliMultiDimVector*)listamdv->FindObject(Form("multiDimVectorPtBin%d",i));
1120 vcttmp->SetName(Form("multiDimVectorPtBin%d",b2+cnt));
1123 listmdvout->Add(vcttmp);
1127 histlistout->Add((TH1F*)histlist->FindObject("fHistNEvents"));
1131 for(Int_t ih1=firsth1;ih1<lasth1;ih1++){
1132 TH1F* h1=(TH1F*)histlist->FindObject(Form("hMass_%d",ih1));
1134 cout<<"hMass_"<<ih1<<" not found!"<<endl;
1137 TH1F* h2=(TH1F*)histlist->FindObject(Form("hMass_%d",ih2));
1139 cout<<"hMass_"<<ih2<<" not found!"<<endl;
1142 //h1->SetName(Form("hMass_%d",cnt));
1144 histlistout->Add(h1);
1151 for(Int_t j=0;j<ntotHperbin*nptbins;j++){
1152 if(!(j>=firsth1 && j<lasth2)){
1153 TH1F* htmp=(TH1F*)histlist->FindObject(Form("hMass_%d",j));
1155 //cout<<lasth1<<" + "<<cnt<<endl;
1156 htmp->SetName(Form("hMass_%d",lasth1+cnt));
1159 histlistout->Add(htmp);
1164 ((TDirectoryFile*)fout->Get(dirname))->cd();
1165 listmdvout->Write(mdvlistname.Data(),TObject::kSingleKey);
1166 histlistout->Write(listname.Data(),TObject::kSingleKey);
1170 void SubtractBkg(Int_t nhisto){
1172 gStyle->SetFrameBorderMode(0);
1173 gStyle->SetCanvasColor(0);
1174 gStyle->SetFrameFillColor(0);
1175 gStyle->SetOptStat(0);
1177 TString fitType="Exp";
1178 TString filename=Form("h%d%sMassFit.root",nhisto,fitType.Data());
1180 TFile* fin=new TFile(filename.Data());
1182 cout<<filename.Data()<<" not found, exit"<<endl;
1186 TKey* key=((TKey*)((TList*)fin->GetListOfKeys())->At(fin->GetNkeys()-1));
1187 TCanvas* canvas=((TCanvas*)fin->Get(key->GetName()));
1189 cout<<"Canvas not found"<<endl;
1194 TH1F* hfit=(TH1F*)canvas->FindObject("fhistoInvMass");
1197 cout<<"Histogram not found"<<endl;
1201 TF1* funcBkgRecalc=(TF1*)hfit->FindObject("funcbkgRecalc");
1203 cout<<"Background fit function (final) not found"<<endl;
1207 TF1* funcBkgFullRange=(TF1*)hfit->FindObject("funcbkgFullRange");
1208 if(!funcBkgFullRange){
1209 cout<<"Background fit function (side bands) not found"<<endl;
1213 Int_t nbins=hfit->GetNbinsX();
1214 Double_t min=hfit->GetBinLowEdge(1), width=hfit->GetBinWidth(1);
1215 TH1F* hsubRecalc=(TH1F*)hfit->Clone("hsub");
1216 hsubRecalc->SetMarkerColor(kRed);
1217 hsubRecalc->SetLineColor(kRed);
1218 hsubRecalc->GetListOfFunctions()->Delete();
1219 TH1F* hsubFullRange=(TH1F*)hfit->Clone("hsub");
1220 hsubFullRange->SetMarkerColor(kGray+2);
1221 hsubFullRange->SetLineColor(kGray+2);
1222 hsubFullRange->GetListOfFunctions()->Delete();
1223 for(Int_t i=0;i<nbins;i++){
1224 //Double_t x=min+i*0.5*width;
1225 Double_t x1=min+i*width, x2=min+(i+1)*width;
1226 Double_t ycont=hfit->GetBinContent(i+1);
1227 Double_t y1=funcBkgRecalc->Integral(x1,x2) / width;//funcBkgRecalc->Eval(x);
1228 hsubRecalc->SetBinContent(i+1,ycont-y1);
1229 Double_t y2=funcBkgFullRange->Integral(x1,x2) / width;//funcBkgFullRange->Eval(x);
1230 hsubFullRange->SetBinContent(i+1,ycont-y2);
1233 TCanvas* c=new TCanvas("c","subtraction");
1235 hsubRecalc->DrawClone();
1236 hsubFullRange->DrawClone("sames");
1238 for(Int_t i=0;i<nbins;i++){
1239 if(hsubRecalc->GetBinContent(i+1)<0) hsubRecalc->SetBinContent(i+1,0);
1240 if(hsubFullRange->GetBinContent(i+1)<0) hsubFullRange->SetBinContent(i+1,0);
1243 TCanvas *cvnewfits=new TCanvas("cvnewfits", "new Fits",1200,600);
1244 cvnewfits->Divide(2,1);
1246 AliHFMassFitter fitter1(hsubRecalc,min,min+nbins*width,1,1);
1247 fitter1.MassFitter(kFALSE);
1248 fitter1.DrawHere(cvnewfits->cd(1));
1250 AliHFMassFitter fitter2(hsubFullRange,min,min+nbins*width,1,1);
1251 fitter2.MassFitter(kFALSE);
1252 fitter2.DrawHere(cvnewfits->cd(2));
1254 canvas->SaveAs(Form("h%d%sMassFit.png",nhisto,fitType.Data()));
1255 c->SaveAs(Form("h%d%sSubtr.png",nhisto,fitType.Data()));
1256 cvnewfits->SaveAs(Form("h%d%sFitNew.png",nhisto,fitType.Data()));