void writeDataBackgroundHistosForModel(const std::map<TString,TGraph*>& m_bkgds, const std::vector<TH1D *>& vchans, TFile *allHistFile) { for (std::map<TString,TGraph*>::const_iterator it = m_bkgds.begin(); it != m_bkgds.end(); it++) { const TString& name = it->first; // determine binning from the signal histogram for this channel // - (sigh) have to find it first... // TString channame = name.Copy().Remove(0,strlen("Bckgrndtot_")); TH1D *sigh=(TH1D*)0; for (int ichan=0; ichan<NUMCHAN; ichan++) { sigh = vchans.at(ichan); if (strstr(sigh->GetName(),channame.Data())) break; } assert (sigh); // for variable binning - all histos must have the same binning per channel TAxis *xax = sigh->GetXaxis(); TVectorD xbins = TVectorD(sigh->GetNbinsX(),xax->GetXbins()->GetArray()); int lobin = xax->FindFixBin(sumwinmin); int hibin = xax->FindFixBin(sumwinmax)-1; int nbins = hibin-lobin+1; TVectorD xwindow = xbins.GetSub(lobin-1,hibin); printf("Booking TH1D(%s,%s,%d,xwindowarray)\n", name.Data(),name.Data(),nbins); TH1D *h = new TH1D(name.Data(),name.Data(),nbins,xwindow.GetMatrixArray()); for (int ibin=1; ibin <= nbins; ibin++) h->SetBinContent(ibin, it->second->Eval(h->GetBinCenter(ibin)) * h->GetBinWidth(ibin) ); allHistFile->WriteTObject(h); } } // writeDataBackgroundHistosForModel
bool TProtoSerial::operator() (TVectorD& v, int protoField) { CHECK_FIELD(); switch (Mode) { case ESerialMode::IN: { const NGroundProto::TVectorD* vecProto = GetEmbedMessage<NGroundProto::TVectorD>(protoField); v = TVectorD(vecProto->x_size()); for (size_t vecIdx=0; vecIdx < v.size(); ++vecIdx) { v(vecIdx) = vecProto->x(vecIdx); } } break; case ESerialMode::OUT: { NGroundProto::TVectorD* vecProto = GetEmbedMutMessage<NGroundProto::TVectorD>(protoField); for (const auto& val: v) { vecProto->add_x(val); } } break; } return true; }
//--------------------------------------------------------------------------- TVectorD OptimalInvest(Double_t riskFactor,TVectorD r,TMatrixDSym Covar) { // what the quadratic programming package will do: // // minimize c^T x + ( 1/2 ) x^T Q x // subject to A x = b // clo <= C x <= cup // xlo <= x <= xup // what we want : // // maximize c^T x - k ( 1/2 ) x^T Q x // subject to sum_x x_i = 1 // 0 <= x_i // We have nrStocks weights to determine, // 1 equality- and 0 inequality- equations (the simple square boundary // condition (xlo <= x <= xup) does not count) const Int_t nrVar = nrStocks; const Int_t nrEqual = 1; const Int_t nrInEqual = 0; // flip the sign of the objective function because we want to maximize TVectorD c = -1.*r; TMatrixDSym Q = riskFactor*Covar; // equality equation TMatrixD A(nrEqual,nrVar); A = 1; TVectorD b(nrEqual); b = 1; // inequality equation // // - although not applicable in the current situation since nrInEqual = 0, one // has to specify not only clo and cup but also an index vector iclo and icup, // whose values are either 0 or 1 . If iclo[j] = 1, the lower boundary condition // is active on x[j], etc. ... TMatrixD C (nrInEqual,nrVar); TVectorD clo (nrInEqual); TVectorD cup (nrInEqual); TVectorD iclo(nrInEqual); TVectorD icup(nrInEqual); // simple square boundary condition : 0 <= x_i, so only xlo is relevant . // Like for clo and cup above, we have to define an index vector ixlo and ixup . // Since each variable has the lower boundary, we can set the whole vector // ixlo = 1 TVectorD xlo (nrVar); xlo = 0; TVectorD xup (nrVar); xup = 0; TVectorD ixlo(nrVar); ixlo = 1; TVectorD ixup(nrVar); ixup = 0; // setup the quadratic programming problem . Since a small number of variables are // involved and "Q" has everywhere entries, we chose the dense version "TQpProbDens" . // In case of a sparse formulation, simply replace all "Dens" by "Sparse" below and // use TMatrixDSparse instead of TMatrixDSym and TMatrixD TQpProbDens *qp = new TQpProbDens(nrVar,nrEqual,nrInEqual); // stuff all the matrices/vectors defined above in the proper places TQpDataDens *prob = (TQpDataDens *)qp->MakeData(c,Q,xlo,ixlo,xup,ixup,A,b,C,clo,iclo,cup,icup); // setup the nrStock variables, vars->fX will contain the final solution TQpVar *vars = qp->MakeVariables(prob); TQpResidual *resid = qp->MakeResiduals(prob); // Now we have to choose the method of solving, either TGondzioSolver or TMehrotraSolver // The Gondzio method is more sophisticated and therefore numerically more involved // If one want the Mehrotra method, simply replace "Gondzio" by "Mehrotra" . TGondzioSolver *s = new TGondzioSolver(qp,prob); const Int_t status = s->Solve(prob,vars,resid); const TVectorD weight = vars->fX; delete qp; delete prob; delete vars; delete resid; delete s; if (status != 0) { cout << "Could not solve this problem." <<endl; return TVectorD(nrStocks); } return weight; }
void writeSignalHistosForModel(std::vector<TH1D *>& vsd, const TString& sigmodel, TFile *allHistFile) { for (int ichan=0; ichan<NUMCHAN; ichan++) { TH1D * sdh = vsd[ichan]; // Find limit window from gaussian fit to signal peak. // double wid = sdh->GetRMS(); double mean = sdh->GetMean(); //TCanvas *c1 = new TCanvas(s,s,300,300); TFitResultPtr r = sdh->Fit("gaus","QNS","",mean-2.5*wid,mean+2.5*wid); cout<<" mean= "<<mean<<", RMS= "<<wid<<", Fit sigma= "<<r->Parameter(2)<<endl; //cout<<r->Parameter(0)<<" "<<r->Parameter(1)<<" "<<r->Parameter(2)<<endl; TAxis *xax = sdh->GetXaxis(); #if 0 int lobin = xax->FindFixBin(r->Parameter(1)-2*r->Parameter(2)); int hibin = xax->FindFixBin(r->Parameter(1)+2*r->Parameter(2)); sd.sumwinmin = xax->GetBinLowEdge(lobin); sd.sumwinmax = xax->GetBinUpEdge(hibin); #elif 0 int lobin = xax->FindFixBin(140); sd.sumwinmin=140; // 1 bin left, int hibin = xax->FindFixBin(170)-1; sd.sumwinmax=170; // 2 bins right #else int lobin = xax->FindFixBin(sumwinmin); int hibin = xax->FindFixBin(sumwinmax)-1; #endif int nbins = hibin-lobin+1; // for variable binning - all histos must have the same binning per channel TVectorD xbins = TVectorD(sdh->GetNbinsX(),sdh->GetXaxis()->GetXbins()->GetArray()); TVectorD xwindow = xbins.GetSub(lobin-1,hibin); xax->SetRange(lobin,hibin); // Copy contents to window-restricted signal histogram // and write to output file. // TString name = Form("Signal%s_%s",sigmodel.Data(),channames[ichan]); printf("Booking TH1D(%s,%s,%d,xwindowarray)\n", name.Data(),sdh->GetTitle(),nbins); TH1D *signm = new TH1D(name.Data(), sdh->GetTitle(), nbins, xwindow.GetMatrixArray()); // make copies of the histograms that are restricted to the // bin range lobin-hibin // for (int ibin=lobin; ibin<=hibin; ibin++) signm->SetBinContent((ibin-lobin+1), sdh->GetBinContent(ibin) *sdh->GetBinWidth(ibin) ); if (!sigmodel.CompareTo("wh")) signm->Scale(whsigscaleto); else signm->Scale(sigscaleto); allHistFile->WriteTObject(signm); } // channel loop } // writeSignalHistosForModel