48 #include "TEventList.h"
52 #include "TObjArray.h"
55 #include "TVirtualFitter.h"
57 extern void Yields( Int_t&, Double_t*, Double_t& f, Double_t* x, Int_t iflag );
60 const TString& treeName,
68 Bool_t scfDPSmeared ) :
69 fileName_( fileName ),
70 inputTreeName_( treeName ),
72 sweightTreeName_(
"" ),
78 variableNames_( variableNames ),
79 freeSpecies_( freeSpecies ),
80 fixdSpecies_( fixdSpecies ),
81 origFreeSpecies_( freeSpecies ),
82 origFixdSpecies_( fixdSpecies ),
83 twodimPDFs_( twodimPDFs ),
84 signalSplit_( sigSplit ),
85 scfDPSmear_( scfDPSmeared ),
87 definedCNBranches_( kFALSE ),
88 definedSWeightBranches_( kFALSE ),
89 firstExpt_( firstExpt ),
93 nDiscVars_( variableNames.size() ),
94 nFreeSpecies_( freeSpecies.size() ),
95 nFixdSpecies_( fixdSpecies.size() ),
96 nSpecies_( freeSpecies.size() + fixdSpecies.size() )
128 cerr <<
"ERROR in LauSPlot::createFileAndTree : Bad filename supplied, not creating file or tree."
134 cerr <<
"ERROR in LauSPlot::createFileAndTree : Problem opening file \"" <<
fileName_
135 <<
"\" for updating, can't do anything." << endl;
168 cerr <<
"ERROR in LauSPlot::readInputInfo : Invalid pointer to data tree." << endl;
173 TObjArray* pLeaves =
inputTree_->GetListOfLeaves();
175 cerr <<
"ERROR in LauSPlot::readInputInfo : Problem retrieving leaves from the tree." << endl;
178 TObjArray& leaves = *pLeaves;
180 if ( nBranches > leaves.GetSize() ) {
181 cerr <<
"ERROR in LauSPlot::readInputInfo : List of leaves is smaller than number of branches - this is strange!"
186 for ( Int_t iLeaf = 0; iLeaf < nBranches; ++iLeaf ) {
188 TLeaf* leaf =
dynamic_cast<TLeaf*
>( leaves[iLeaf] );
191 Int_t size = leaf->GetNdata();
193 cerr <<
"ERROR in LauSPlot::readInputInfo : Tree has array branches, can't deal with those."
199 TString
name = leaf->GetName();
218 const TString& specName = twodim_iter->first;
219 const TString& firstVarName = twodim_iter->second.first;
220 const TString& secondVarName = twodim_iter->second.second;
222 TString expectedName( specName );
223 expectedName += firstVarName;
224 expectedName += secondVarName;
225 expectedName +=
"Like";
227 Bool_t found( kFALSE );
228 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
230 const TString& leafName = leaf_iter->first;
231 const TLeaf* leaf = leaf_iter->second;
233 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
240 cerr <<
"ERROR in LauSPlot::checkLeaves : Could not find expected leaf \""
241 << expectedName <<
"\"." << endl;
251 const TString& specName = fixd_iter->first;
262 const TString& varName = ( *vars_iter );
263 TString expectedName( specName );
264 expectedName += varName;
265 expectedName +=
"Like";
266 Bool_t found( kFALSE );
267 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
269 const TString& leafName = leaf_iter->first;
270 const TLeaf* leaf = leaf_iter->second;
272 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
279 cerr <<
"ERROR in LauSPlot::checkLeaves : Could not find expected leaf \""
280 << expectedName <<
"\"." << endl;
289 const TString& specName = free_iter->first;
300 const TString& varName = ( *vars_iter );
301 TString expectedName( specName );
302 expectedName += varName;
303 expectedName +=
"Like";
304 Bool_t found( kFALSE );
305 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
307 const TString& leafName = leaf_iter->first;
308 const TLeaf* leaf = leaf_iter->second;
310 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
317 cerr <<
"ERROR in LauSPlot::checkLeaves : Could not find expected leaf \""
318 << expectedName <<
"\"." << endl;
331 const TString& varName = ( *vars_iter );
332 TString expectedName(
"sigTM" );
333 expectedName += varName;
334 expectedName +=
"Like";
335 Bool_t found( kFALSE );
336 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
338 const TString& leafName = leaf_iter->first;
339 const TLeaf* leaf = leaf_iter->second;
341 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
348 cerr <<
"ERROR in LauSPlot::checkLeaves : Could not find expected leaf \""
349 << expectedName <<
"\"." << endl;
353 expectedName =
"sigSCF";
354 expectedName += varName;
355 expectedName +=
"Like";
357 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
359 const TString& leafName = leaf_iter->first;
360 const TLeaf* leaf = leaf_iter->second;
362 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
369 cerr <<
"ERROR in LauSPlot::checkLeaves : Could not find expected leaf \""
370 << expectedName <<
"\"." << endl;
374 expectedName =
"sigSCFFrac";
376 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
378 const TString& leafName = leaf_iter->first;
379 const TLeaf* leaf = leaf_iter->second;
381 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
388 cerr <<
"ERROR in LauSPlot::checkLeaves : Could not find expected leaf \""
389 << expectedName <<
"\"." << endl;
429 cerr <<
"ERROR in LauSPlot::defineCNBranches : Already defined branches, not doing it again."
434 cerr <<
"ERROR in LauSPlot::defineCNBranches : No entries in the cN container, can't define branches."
439 cerr <<
"ERROR in LauSPlot::defineCNBranches : Invalid pointer to the tree, can't define branches."
447 for ( std::map<TString, NumbMap>::iterator excl_iter =
cN_.begin(); excl_iter !=
cN_.end();
449 const TString& exclName = excl_iter->first;
450 NumbMap& species = excl_iter->second;
451 for ( NumbMap::iterator spec_iter = species.begin(); spec_iter != species.end();
453 const TString& specName = spec_iter->first;
454 Double_t* pointer = &( spec_iter->second );
455 TString
name( specName );
457 if ( exclName ==
"none" ) {
463 TString thirdPart(
name );
474 cerr <<
"ERROR in LauSPlot::defineSWeightBranches : Already defined branches, not doing it again."
479 cerr <<
"ERROR in LauSPlot::defineSWeightBranches : No entries in the sWeights container, can't define branches."
484 cerr <<
"ERROR in LauSPlot::defineSWeightBranches : Invalid pointer to the tree, can't define branches."
489 for ( std::map<TString, NumbMap>::iterator excl_iter =
sWeightsCurrent_.begin();
492 const TString& exclName = excl_iter->first;
493 NumbMap& species = excl_iter->second;
494 for ( NumbMap::iterator spec_iter = species.begin(); spec_iter != species.end();
496 const TString& specName = spec_iter->first;
497 Double_t* pointer = &( spec_iter->second );
498 TString
name( specName );
500 if ( exclName ==
"none" ) {
506 TString thirdPart(
name );
517 cerr <<
"ERROR in LauSPlot::setExperiment : Invalid pointer to data tree." << endl;
523 eventList_ =
new TEventList(
"splotelist",
"splotelist" );
529 listName.Prepend(
">>" );
530 TString selection(
"iExpt==" );
532 cout <<
"LauSPlot::setExperiment : Setting tree to experiment number " << iExpt <<
"." << endl;
537 cout <<
" Found " <<
nEvents_ <<
" events." << endl;
555 for ( Int_t iEvt( 0 ); iEvt <
nEvents_; ++iEvt ) {
559 cerr <<
"ERROR in LauSPlot::readExpt : Problem retrieving event." << endl;
560 gSystem->Exit( EXIT_FAILURE );
568 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
570 const TString& leafName = leaf_iter->first;
571 const TLeaf* leaf = leaf_iter->second;
572 if ( ( leafName ==
"sigSCFFrac" ) && ( leaf != 0 ) ) {
580 for ( TwoDMap::const_iterator twodim_iter =
twodimPDFs_.begin();
583 const TString& specName = twodim_iter->first;
584 const TString& firstVarName = twodim_iter->second.first;
585 const TString& secondVarName = twodim_iter->second.second;
587 TString varName = firstVarName + secondVarName;
589 TString expectedName( specName );
590 expectedName += varName;
591 expectedName +=
"Like";
593 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
595 const TString& leafName = leaf_iter->first;
596 const TLeaf* leaf = leaf_iter->second;
597 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
598 discPdf_[iEvt][specName][varName] = leaf->GetValue();
604 Bool_t needSignalSearch( kFALSE );
605 for ( NumbMap::const_iterator fixd_iter =
fixdSpecies_.begin();
608 const TString& specName = fixd_iter->first;
613 needSignalSearch = kTRUE;
620 const TString& varName = ( *vars_iter );
621 TString expectedName( specName );
622 expectedName += varName;
623 expectedName +=
"Like";
624 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
626 const TString& leafName = leaf_iter->first;
627 const TLeaf* leaf = leaf_iter->second;
628 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
629 discPdf_[iEvt][specName][varName] = leaf->GetValue();
636 for ( NumbMap::const_iterator free_iter =
freeSpecies_.begin();
639 const TString& specName = free_iter->first;
644 needSignalSearch = kTRUE;
651 const TString& varName = ( *vars_iter );
652 TString expectedName( specName );
653 expectedName += varName;
654 expectedName +=
"Like";
655 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
657 const TString& leafName = leaf_iter->first;
658 const TLeaf* leaf = leaf_iter->second;
659 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
660 discPdf_[iEvt][specName][varName] = leaf->GetValue();
667 if ( needSignalSearch ) {
671 const TString& varName = ( *vars_iter );
672 TString specName(
"sigTM" );
673 TString expectedName( specName );
674 expectedName += varName;
675 expectedName +=
"Like";
676 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
678 const TString& leafName = leaf_iter->first;
679 const TLeaf* leaf = leaf_iter->second;
680 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
681 discPdf_[iEvt][specName][varName] = leaf->GetValue();
686 expectedName = specName;
687 expectedName += varName;
688 expectedName +=
"Like";
689 for ( LeafMap::const_iterator leaf_iter =
leaves_.begin(); leaf_iter !=
leaves_.end();
691 const TString& leafName = leaf_iter->first;
692 const TLeaf* leaf = leaf_iter->second;
693 if ( ( leafName == expectedName ) && ( leaf != 0 ) ) {
694 discPdf_[iEvt][specName][varName] = leaf->GetValue();
712 cerr <<
"ERROR in LauSPlot::runCalculations : The input ntuple has not been successfully read, can't calculate anything."
718 cerr <<
"ERROR in LauSPlot::runCalculations : Numbers of events in species have not been set."
723 TString opt( option );
725 opt.ReplaceAll(
"VV",
"W" );
736 cerr <<
"ERROR in LauSPlot::runCalculations : Zero events in experiment " <<
iExpt_
737 <<
", skipping..." << endl;
744 excludePdf.insert(
"none" );
747 excludePdf.insert(
"none" );
750 for ( NameSet::const_iterator excl_iter = excludePdf.begin(); excl_iter != excludePdf.end();
753 const TString& exclName = ( *excl_iter );
755 cout <<
"LauSPlot::runCalculations : Calculating sWeights, excluding PDF: " << exclName
775 Double_t* covmat( 0 );
777 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
778 covmat = fitter->GetCovarianceMatrix();
780 if ( opt.Contains(
"W" ) ) {
791 if ( opt.Contains(
"W" ) ) {
806 TString minuitName(
"TFitter" );
807 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
809 TString fitterName( fitter->IsA()->GetName() );
810 if ( fitterName != minuitName ) {
815 fitter = TVirtualFitter::Fitter( 0 );
820 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
822 fitter->SetFCN( Yields );
823 fitter->SetObjectFit(
this );
826 Double_t arglist[10];
827 if ( opt.Contains(
"Q" ) ) {
830 if ( opt.Contains(
"V" ) ) {
833 if ( opt.Contains(
"W" ) ) {
836 fitter->ExecuteCommand(
"SET PRINT", arglist, 1 );
841 fitter->ExecuteCommand(
"SET ERR", arglist, 1 );
846 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
852 for ( NumbMap::const_iterator spec_iter = species.begin(); spec_iter != species.end();
854 const TString&
name( spec_iter->first );
862 Double_t
value( 0.0 );
864 value = fixd_iter->second;
866 value = free_iter->second;
868 fitter->SetParameter( ispecies,
name,
value, 1, 0, 0 );
870 fitter->FixParameter( ispecies );
878 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
880 Double_t arglist[10];
885 Int_t fitStatus = fitter->ExecuteCommand(
"MIGRAD", arglist, 2 );
887 if ( fitStatus != 0 ) {
888 cerr <<
"ERROR in LauSPlot::runFit : Error during MIGRAD minimisation." << endl;
891 fitStatus = fitter->ExecuteCommand(
"HESSE", arglist, 1 );
892 if ( fitStatus != 0 ) {
893 cerr <<
"ERROR in LauSPlot::runFit : Error during HESSE error calculation." << endl;
908 cout <<
"CovMat Elem: [" << iter_a->first <<
"," << iter_b->first
909 <<
"] = " << covmat[a *
nSpecies_ + b] << endl;
911 cout <<
"CovMat Elem: [" << iter_a->first <<
"," << iter_b->first
912 <<
"] = " <<
covMat_( a, b ) << endl;
927 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
933 for ( NumbMap::const_iterator spec_iter = species.begin(); spec_iter != species.end();
935 const TString&
name( spec_iter->first );
938 free_iter->second = fitter->GetParameter( ispecies );
939 if ( ! opt.Contains(
"Q" ) ) {
940 cout <<
"Estimated # of events in species " <<
name <<
" = " << free_iter->second
946 if ( ! opt.Contains(
"Q" ) ) {
955 const TString& specName = spec_iter->first;
956 Double_t sumweight( 0.0 );
957 for ( Int_t iEvt( 0 ); iEvt <
nEvents_; ++iEvt ) {
959 Double_t weight = specWeightMap.find( specName )->second;
962 cout <<
"Sum of sWeights for species \"" << specName <<
"\" = " << sumweight << endl;
974 std::vector<Double_t> denominator(
nEvents_ );
975 for ( Int_t iEvt( 0 ); iEvt <
nEvents_; ++iEvt ) {
976 denominator[iEvt] = 0.0;
977 for ( NumbMap::const_iterator spec_iter =
freeSpecies_.begin();
980 const TString& specName = spec_iter->first;
981 Double_t specNumEvents = spec_iter->second;
982 denominator[iEvt] += specNumEvents *
pdfTot_[iEvt][specName];
984 for ( NumbMap::const_iterator spec_iter =
fixdSpecies_.begin();
987 const TString& specName = spec_iter->first;
988 Double_t specNumEvents = spec_iter->second;
989 denominator[iEvt] += specNumEvents *
pdfTot_[iEvt][specName];
992 denominator[iEvt] *= denominator[iEvt];
997 for ( NumbMap::const_iterator spec_iter_i =
freeSpecies_.begin();
1000 const TString& specName_i = spec_iter_i->first;
1002 for ( NumbMap::const_iterator spec_iter_j =
freeSpecies_.begin();
1005 const TString& specName_j = spec_iter_j->first;
1006 invMatrix( i, j ) = 0.0;
1007 for ( Int_t iEvt( 0 ); iEvt <
nEvents_; ++iEvt ) {
1008 Double_t numerator =
pdfTot_[iEvt][specName_i] *
pdfTot_[iEvt][specName_j];
1009 invMatrix( i, j ) += numerator / denominator[iEvt];
1017 if ( invMatrix.Determinant() < 1e-15 ) {
1018 cerr <<
"ERROR in LauSPlot::calcCovMatrix : Calculated inverse covariance matrix is singular, can't invert it."
1025 covMat_ = TMatrixD( TMatrixD::kInverted, invMatrix );
1032 for ( Int_t iEvt( 0 ); iEvt <
nEvents_; iEvt++ ) {
1034 Bool_t needSignalSearch( kFALSE );
1036 for ( NumbMap::const_iterator spec_iter =
fixdSpecies_.begin();
1039 const TString& specName = spec_iter->first;
1044 needSignalSearch = kTRUE;
1048 pdfTot_[iEvt][specName] = 1.0;
1052 for ( TwoDMap::const_iterator twodim_iter =
twodimPDFs_.begin();
1057 if ( specName != twodim_iter->first ) {
1062 const TString& firstVarName = twodim_iter->second.first;
1063 const TString& secondVarName = twodim_iter->second.second;
1064 if ( firstVarName != exclName && secondVarName != exclName ) {
1067 skipList.insert( firstVarName );
1068 skipList.insert( secondVarName );
1070 TString varName = firstVarName + secondVarName;
1079 const TString& varName = ( *var_iter );
1081 if ( exclName != varName ) {
1083 if ( skipList.find( varName ) == skipList.end() ) {
1091 for ( NumbMap::const_iterator spec_iter =
freeSpecies_.begin();
1094 const TString& specName = spec_iter->first;
1099 needSignalSearch = kTRUE;
1103 pdfTot_[iEvt][specName] = 1.0;
1107 for ( TwoDMap::const_iterator twodim_iter =
twodimPDFs_.begin();
1112 if ( specName != twodim_iter->first ) {
1117 const TString& firstVarName = twodim_iter->second.first;
1118 const TString& secondVarName = twodim_iter->second.second;
1119 if ( firstVarName != exclName && secondVarName != exclName ) {
1122 skipList.insert( firstVarName );
1123 skipList.insert( secondVarName );
1125 TString varName = firstVarName + secondVarName;
1134 const TString& varName = ( *var_iter );
1136 if ( exclName != varName ) {
1138 if ( skipList.find( varName ) == skipList.end() ) {
1146 if ( needSignalSearch ) {
1149 TString specName(
"sigTM" );
1150 Double_t tmPDFVal( 1.0 );
1152 for ( TwoDMap::const_iterator twodim_iter =
twodimPDFs_.begin();
1157 if ( specName != twodim_iter->first ) {
1162 const TString& firstVarName = twodim_iter->second.first;
1163 const TString& secondVarName = twodim_iter->second.second;
1164 if ( firstVarName != exclName && secondVarName != exclName ) {
1167 skipList.insert( firstVarName );
1168 skipList.insert( secondVarName );
1170 TString varName = firstVarName + secondVarName;
1171 tmPDFVal *=
discPdf_[iEvt][specName][varName];
1179 const TString& varName = ( *var_iter );
1181 if ( exclName != varName ) {
1183 if ( skipList.find( varName ) == skipList.end() ) {
1185 tmPDFVal *=
discPdf_[iEvt][specName][varName];
1190 tmPDFVal *= ( 1.0 -
scfFrac_[iEvt] );
1193 specName =
"sigSCF";
1194 Double_t scfPDFVal( 1.0 );
1196 for ( TwoDMap::const_iterator twodim_iter =
twodimPDFs_.begin();
1201 if ( specName != twodim_iter->first ) {
1206 const TString& firstVarName = twodim_iter->second.first;
1207 const TString& secondVarName = twodim_iter->second.second;
1208 if ( firstVarName != exclName && secondVarName != exclName ) {
1211 skipList.insert( firstVarName );
1212 skipList.insert( secondVarName );
1214 TString varName = firstVarName + secondVarName;
1215 scfPDFVal *=
discPdf_[iEvt][specName][varName];
1223 const TString& varName = ( *var_iter );
1225 if ( exclName != varName ) {
1227 if ( skipList.find( varName ) == skipList.end() ) {
1229 scfPDFVal *=
discPdf_[iEvt][specName][varName];
1234 if ( exclName ==
"DP" || ! this->
scfDPSmear() ) {
1238 pdfTot_[iEvt][
"sig"] = tmPDFVal + scfPDFVal;
1251 Int_t species_n( 0 );
1254 Int_t species_j( 0 );
1255 const TString& specName = iter_n->first;
1256 Double_t
value = iter_n->second;
1261 cN_[exclName][specName] -= covmat[species_n *
nSpecies_ + species_j];
1263 cN_[exclName][specName] -=
covMat_( species_n, species_j );
1278 Double_t numerator( 0.0 ), denominator( 0.0 );
1279 for ( Int_t iEvent = 0; iEvent <
nEvents_; ++iEvent ) {
1281 for ( NumbMap::const_iterator free_iter =
freeSpecies_.begin();
1284 denominator += free_iter->second *
pdfTot_[iEvent][free_iter->first];
1286 for ( NumbMap::const_iterator fixd_iter =
fixdSpecies_.begin();
1289 denominator += fixd_iter->second *
pdfTot_[iEvent][fixd_iter->first];
1291 Int_t species_n( 0 );
1295 Int_t species_j( 0 );
1299 numerator += covmat[species_n *
nSpecies_ + species_j] *
1300 pdfTot_[iEvent][iter_j->first];
1302 numerator +=
covMat_( species_n, species_j ) *
pdfTot_[iEvent][iter_j->first];
1306 sWeights_[iEvent][exclName][iter_n->first] = numerator / denominator;
1315 cerr <<
"ERROR in LauSPlot::fillCNBranches : Tree not created, cannot fill branches." << endl;
1326 cerr <<
"ERROR in LauSPlot::fillSWeightBranches : Tree not created, cannot fill branches."
1330 cerr <<
"ERROR in LauSPlot::fillSWeightBranches : No sWeights calculated, can't fill branches."
1338 for ( Int_t iEvent = 0; iEvent <
nEvents_; ++iEvent ) {
1346 for ( std::map<TString, NumbMap>::const_iterator excl_iter =
sWeights_[iEvent].begin();
1349 const TString& exclName = excl_iter->first;
1350 const NumbMap& species = excl_iter->second;
1351 for ( NumbMap::const_iterator spec_iter = species.begin(); spec_iter != species.end();
1353 const TString& specName = spec_iter->first;
1387 cerr <<
"ERROR in LauSPlot::addFriendTree : Tree not created, cannot add friend." << endl;
1393 void Yields( Int_t&, Double_t*, Double_t& f, Double_t* x, Int_t )
1399 TVirtualFitter* fitter = TVirtualFitter::GetFitter();
1401 const std::vector<LauSPlot::NumbMap>& pdfTot = theModel->
totalPdf();
1403 Double_t ntot( 0.0 );
1404 for ( std::vector<LauSPlot::NumbMap>::const_iterator evt_iter = pdfTot.begin();
1405 evt_iter != pdfTot.end();
1408 Int_t ispecies( 0 );
1409 Double_t lik( 0.0 );
1411 for ( LauSPlot::NumbMap::const_iterator spec_iter = species.begin();
1412 spec_iter != species.end();
1414 lik += x[ispecies] * spec_iter->second;
1415 ntot += x[ispecies];
1424 f += TMath::Log( lik );