void SelectorTest::testSelect(TestDataSuite* tds)
{
    while (tds->hasMoreTestData()) {
	std::cerr << "Updating strstream: " << strstream->str() << '|' << std::endl
		  << " with: " << tds->getUpdateString() << '|' << std::endl;
	*strstream << tds->getUpdateString();
	std::vector<std::string> selectedTokens;
	selectedTokens = selector->select(tds->getInputPrediction());

	Prediction expected = tds->getOutputPrediction();
	CPPUNIT_ASSERT_EQUAL( (size_t)expected.size(), selectedTokens.size() );

	std::vector<std::string>::const_iterator actual_it = selectedTokens.begin();
	int index = 0;
	while (actual_it != selectedTokens.end()) {
	    std::cerr << "[expected] " << expected.getSuggestion(index).getWord()
		      << "  [actual] " << *actual_it << std::endl;
	    CPPUNIT_ASSERT_EQUAL(expected.getSuggestion(index).getWord(),
				 *actual_it);
	    
	    index++;
	    actual_it++;
	}

	contextTracker->update();
	tds->nextTestData();
    }
}
Prediction DummyPlugin::predict(const size_t max_partial_predictions_size, const char** filter) const
{
    // A real plugin would query its resources to retrieve the most 
    // probable completion of the prefix based on the current history,
    // but this is just a dummy plugin that returns random suggestions.
    //
    Prediction result;

    result.addSuggestion (Suggestion("foo1", 0.99));
    result.addSuggestion (Suggestion("foo2", 0.98));
    result.addSuggestion (Suggestion("foo3", 0.97));
    result.addSuggestion (Suggestion("foo4", 0.96));
    result.addSuggestion (Suggestion("foo5", 0.95));
    result.addSuggestion (Suggestion("foo6", 0.94));

    result.addSuggestion (Suggestion("bar1", 0.89));
    result.addSuggestion (Suggestion("bar2", 0.88));
    result.addSuggestion (Suggestion("bar3", 0.87));
    result.addSuggestion (Suggestion("bar4", 0.86));
    result.addSuggestion (Suggestion("bar5", 0.85));
    result.addSuggestion (Suggestion("bar6", 0.84));

    result.addSuggestion (Suggestion("foobar1", 0.79));
    result.addSuggestion (Suggestion("foobar2", 0.78));
    result.addSuggestion (Suggestion("foobar3", 0.77));
    result.addSuggestion (Suggestion("foobar4", 0.76));
    result.addSuggestion (Suggestion("foobar5", 0.75));
    result.addSuggestion (Suggestion("foobar6", 0.74));

    return result;
}
/** SQLite callback function
    Builds prediction from query results.

*/
int buildPrediction( void* callbackDataPtr,
		     int argc,
		     char** argv,
		     char** column )
{
	// cast pointer to void back to pointer to CallbackData object
	CallbackData* dataPtr = static_cast<CallbackData*>(callbackDataPtr);

	Prediction* predictionPtr = dataPtr->predPtr;
	size_t maxPredictionSize = dataPtr->predSize;

	if (predictionPtr->size() > maxPredictionSize) {
		return 1;
	} else {

		if( argc == 2 &&
		    strcmp( "word", column[ 0 ] ) == 0 &&
		    strcmp( "count", column[ 1 ] ) == 0 ) {
			
			predictionPtr->addSuggestion( 
				Suggestion( argv[ argc - 2 ],
					    atof( argv[ argc - 1 ] )
					)
				);
			
		} else {
			std::cerr << "Invalid invocation of buildPrediction method!"
				  << std::endl;
			exit( 1 );
		}
	}
	return 0;
}
Prediction DictionaryPlugin::predict(const size_t max_partial_predictions_size, const char** filter) const
{
    Prediction result;

    std::string candidate;
    std::string prefix = contextTracker->getPrefix();

    std::ifstream dictionary_file;
    dictionary_file.open(dictionary_path.c_str());
    if(!dictionary_file)
        logger << ERROR << "Error opening dictionary: " << dictionary_path << endl;
    assert(dictionary_file); // REVISIT: handle with exceptions

    // scan file entries until we get enough suggestions
    unsigned int count = 0;
    while(dictionary_file >> candidate && count < max_partial_predictions_size) {
	if(candidate.find(prefix) == 0) {
	    result.addSuggestion(Suggestion(candidate,probability));
	    count++;
	    logger << NOTICE << "Found valid token: " << candidate << endl;
	} else {
	    logger << INFO << "Discarding invalid token: " << candidate << endl;
	}
    }

    dictionary_file.close();

    return result;
}
Exemple #5
0
gMat2D<T>* KernelRLSWrapper<T>::eval(const gMat2D<T> &X)
{
    Prediction<T> *pred;
    PredKernelTrainTest<T> predkTrainTest;

    gMat2D<T> empty;

    switch (this->kType)
    {
    case KernelWrapper<T>::LINEAR:
        pred = new PredPrimal<T>();
        break;
    case KernelWrapper<T>::RBF:
        pred = new PredDual<T>();
        this->opt->addOpt("predkernel", predkTrainTest.execute(X, empty, *(this->opt)));
    }

    OptMatrix<gMat2D<T> >* result = OptMatrix<gMat2D<T> >::dynacast(pred->execute(X, empty, *(this->opt)));
    result->detachValue();
    delete pred;

    gMat2D<T>* ret = &(result->getValue());

    delete result;
    return ret;
}
Prediction RecencyPredictor::predict (const size_t max, const char** filter) const
{
    Prediction result;

    std::string prefix = contextTracker->getPrefix();
    logger << INFO << "prefix: " << prefix << endl;
    if (!prefix.empty()) {
        // Only build recency prediction if prefix is not empty: when
        // prefix is empty, all previosly seen tokens are candidates
        // for prediction. This is not desirable, because it means
        // that recency prediction reduces to repetion of max previous
        // tokens (i.e. the prediction would contain the most recent
        // tokens in reverse order).
        //
        Suggestion  suggestion;
        size_t      index = 1;
        std::string token = contextTracker->getToken(index);
	double      prob = 0;
        while (!token.empty()                // context history exhausted
	       && result.size() < max        // need only max suggestions
	       && index <= cutoff_threshold  // look back only as far as cutoff
	    ) {
	    logger << INFO << "token: " << token << endl;

            if (token.find(prefix) == 0) { // if token starts with prefix

		if (token_satisfies_filter (token, prefix, filter)) {
		    // compute probability according to exponential decay
		    // formula
		    //
		    prob = n_0 * exp(-(lambda * (index - 1)));
		    logger << INFO << "probability: " << prob << endl;
		    suggestion.setWord(token);
		    suggestion.setProbability(prob);
		    result.addSuggestion(suggestion);
		}

            }

            index++;
            token = contextTracker->getToken(index);
        }
    }

    return result;
}
bool Prediction::operator== (const Prediction& right) const
{
    // same instance is obviously equal to itself
    if (&right == this) {
	return true;
    } else {
	if (size() != right.size()) {
	    return false;
	} else {
	    // need to compare each suggestion
	    bool result = true;
	    size_t i = 0;
	    while (i < size() && result) {
		if (getSuggestion(i) != right.getSuggestion(i)) {
		    result = false;
		}
		i++;
	    }
	    return result;
	}
    }
}
void PredictorRegistryTest::testNext()
{
    ContextTracker* pointer = static_cast<ContextTracker*>((void*)0xdeadbeef);
    registry->setContextTracker(pointer);

    PredictorRegistry::Iterator it = registry->iterator();
    Predictor* predictor = 0;

    while (it.hasNext()) {
	predictor = it.next();
    }

    // since we've iterated till the end of the predictors list, predictor
    // is now pointing to the DummyPredictor, so let's test we got the
    // dummy prediction back
    Prediction prediction = predictor->predict(20, 0);

    CPPUNIT_ASSERT(predictor != 0);
    size_t expected_size = 18;
    CPPUNIT_ASSERT_EQUAL(expected_size, prediction.size());
    CPPUNIT_ASSERT_EQUAL(Suggestion("foo1", 0.99), prediction.getSuggestion(0));
    CPPUNIT_ASSERT_EQUAL(Suggestion("foobar6", 0.74), prediction.getSuggestion(17));
}
void NewSmoothedNgramPluginTest::testLearning()
{
    // get pointer to plugin
    Plugin* plugin = pluginRegistry->iterator().next();

    {
	*stream << "f";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), actual.size());
    }

    {
	*stream << "o";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), actual.size());
    }

    {
	*stream << "o ";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), actual.size());
	CPPUNIT_ASSERT_EQUAL(std::string("foo"), actual.getSuggestion(0).getWord());
	ct->update();
    }

    {
	*stream << "bar";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
    }

    {
	*stream << " ";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), actual.size());
	CPPUNIT_ASSERT_EQUAL(std::string("foo"), actual.getSuggestion(0).getWord());
	CPPUNIT_ASSERT_EQUAL(std::string("bar"), actual.getSuggestion(1).getWord());
    }

    {
	*stream << "foobar ";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), actual.size());
	CPPUNIT_ASSERT_EQUAL(std::string("foobar"), actual.getSuggestion(0).getWord());
	CPPUNIT_ASSERT_EQUAL(std::string("foo"), actual.getSuggestion(1).getWord());
	CPPUNIT_ASSERT_EQUAL(std::string("bar"), actual.getSuggestion(2).getWord());
    }

    {
	*stream << "f";
	ct->update();
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), actual.size());
	CPPUNIT_ASSERT_EQUAL(std::string("foobar"), actual.getSuggestion(0).getWord());
	CPPUNIT_ASSERT_EQUAL(std::string("foo"), actual.getSuggestion(1).getWord());
    }
}
Prediction ARPAPlugin::predict(const size_t max_partial_prediction_size, const char** filter) const
{
  logger << DEBUG << "predict()" << endl;
  Prediction prediction;

  int cardinality = 3;
  std::vector<std::string> tokens(cardinality);

  std::string prefix = strtolower(contextTracker->getToken(0));
  std::string wd2Str = strtolower(contextTracker->getToken(1));
  std::string wd1Str = strtolower(contextTracker->getToken(2));

  std::multimap<float,std::string,cmp> result;

  logger << DEBUG << "["<<wd1Str<<"]"<<" ["<<wd2Str<<"] "<<"["<<prefix<<"]"<<endl;

  //search for the past tokens in the vocabulary
  std::map<std::string,int>::const_iterator wd1It,wd2It;
  wd1It = vocabCode.find(wd1Str);
  wd2It = vocabCode.find(wd2Str);

  /**
   * note if we have not tokens to compute 3-gram probabilities we compute 2-gram or 1-gram probabilities.
   * the following code might be repetitive but more efficient than having the main loop outside.
   */

  //we have two valid past tokens available
  if(wd1It!=vocabCode.end() && wd2It!=vocabCode.end())
  {
    //iterate over all vocab words
    for(std::map<int,std::string>::const_iterator it = vocabDecode.begin(); it!=vocabDecode.end(); it++)
    {
      //if wd3 matches prefix and filter -> compute its backoff probability and add to the result set
      if(matchesPrefixAndFilter(it->second,prefix,filter))
      {
        std::pair<float,std::string> p;
        p.first = computeTrigramBackoff(wd1It->second,wd2It->second,it->first);
        p.second = it->second;
        result.insert(p);
      }
    }
  }

  //we have one valid past token available
  else if(wd2It!=vocabCode.end())
  {
    //iterate over all vocab words
    for(std::map<int,std::string>::const_iterator it = vocabDecode.begin(); it!=vocabDecode.end(); it++)
    {
      //if wd3 matches prefix and filter -> compute its backoff probability and add to the result set
      if(matchesPrefixAndFilter(it->second,prefix,filter))
      {
        std::pair<float,std::string> p;
        p.first = computeBigramBackoff(wd2It->second,it->first);
        p.second = it->second;
        result.insert(p);
      }
    }
  }

  //we have no valid past token available
  else
  {
    //iterate over all vocab words
    for(std::map<int,std::string>::const_iterator it = vocabDecode.begin(); it!=vocabDecode.end(); it++)
    {
      //if wd3 matches prefix and filter -> compute its backoff probability and add to the result set
      if(matchesPrefixAndFilter(it->second,prefix,filter))
      {
        std::pair<float,std::string> p;
        p.first = unigramMap.find(it->first)->second.logProb;
        p.second = it->second;
        result.insert(p);
      }
    }
  }


  size_t numSuggestions = 0;
  for(std::multimap<float,std::string>::const_iterator it = result.begin(); it != result.end() && numSuggestions < max_partial_prediction_size; ++it)
  {
    prediction.addSuggestion(Suggestion(it->second,exp(it->first)));
    numSuggestions++;
  }

  return prediction;
}
void SelectorTest::TestDataSuite_S6_NR_T0::init()
{
    TestData* td;
    Prediction* ip;
    Prediction* op;

    td = new TestData;
    ip = new Prediction;
    op = new Prediction;
    ip->addSuggestion(Suggestion("foo",        0.9));
    ip->addSuggestion(Suggestion("foo1",       0.8));
    ip->addSuggestion(Suggestion("foo2",       0.7));
    ip->addSuggestion(Suggestion("foo3",       0.6));
    ip->addSuggestion(Suggestion("foo4",       0.5));
    ip->addSuggestion(Suggestion("foo5",       0.4));
    ip->addSuggestion(Suggestion("foo6",       0.3));
    ip->addSuggestion(Suggestion("foobar",     0.2));
    ip->addSuggestion(Suggestion("foobar1",    0.1));
    ip->addSuggestion(Suggestion("foobar2",    0.09));
    ip->addSuggestion(Suggestion("foobar3",    0.08));
    ip->addSuggestion(Suggestion("foobar4",    0.07));
    ip->addSuggestion(Suggestion("foobar5",    0.06));
    ip->addSuggestion(Suggestion("foobar6",    0.05));
    ip->addSuggestion(Suggestion("foobar7",    0.04));
    ip->addSuggestion(Suggestion("foobar8",    0.03));
    ip->addSuggestion(Suggestion("foobar9",    0.02));
    ip->addSuggestion(Suggestion("foobarfoo",  0.01));
    ip->addSuggestion(Suggestion("foobarfoo1", 0.009));
    ip->addSuggestion(Suggestion("foobarfoo2", 0.008));
    ip->addSuggestion(Suggestion("foobarfoo3", 0.007));
    op->addSuggestion(Suggestion("foo",     0.9));
    op->addSuggestion(Suggestion("foo1",    0.8));
    op->addSuggestion(Suggestion("foo2",    0.7));
    op->addSuggestion(Suggestion("foo3",    0.6));
    op->addSuggestion(Suggestion("foo4",    0.5));
    op->addSuggestion(Suggestion("foo5",    0.4));
    td->updateString     = "f";
    td->inputPrediction  = *ip;
    td->outputPrediction = *op;
    testData.push_back(*td);
    delete td;
    delete op;

    td = new TestData;
    op = new Prediction;
    op->addSuggestion(Suggestion("foo6",       0.3));
    op->addSuggestion(Suggestion("foobar",     0.2));
    op->addSuggestion(Suggestion("foobar1",    0.1));
    op->addSuggestion(Suggestion("foobar2",    0.09));
    op->addSuggestion(Suggestion("foobar3",    0.08));
    op->addSuggestion(Suggestion("foobar4",    0.07));
    td->updateString     = "o";
    td->inputPrediction  = *ip;
    td->outputPrediction = *op;
    testData.push_back(*td);
    delete td;
    delete op;
    
    td = new TestData;
    op = new Prediction;
    op->addSuggestion(Suggestion("foobar5",    0.06));
    op->addSuggestion(Suggestion("foobar6",    0.05));
    op->addSuggestion(Suggestion("foobar7",    0.04));
    op->addSuggestion(Suggestion("foobar8",    0.03));
    op->addSuggestion(Suggestion("foobar9",    0.02));
    op->addSuggestion(Suggestion("foobarfoo",  0.01));
    td->updateString     = "o";
    td->inputPrediction  = *ip;
    td->outputPrediction = *op;
    testData.push_back(*td);
    delete td;
    delete op;

    iter = testData.begin();
}
Prediction SmoothedNgramPredictor::predict(const size_t max_partial_prediction_size, const char** filter) const
{
    logger << DEBUG << "predict()" << endl;

    // Result prediction
    Prediction prediction;

    // Cache all the needed tokens.
    // tokens[k] corresponds to w_{i-k} in the generalized smoothed
    // n-gram probability formula
    //
    std::vector<std::string> tokens(cardinality);
    for (int i = 0; i < cardinality; i++) {
	tokens[cardinality - 1 - i] = contextTracker->getToken(i);
	logger << DEBUG << "Cached tokens[" << cardinality - 1 - i << "] = " << tokens[cardinality - 1 - i] << endl;
    }

    // Generate list of prefix completition candidates.
    //
    // The prefix completion candidates used to be obtained from the
    // _1_gram table because in a well-constructed ngram database the
    // _1_gram table (which contains all known tokens). However, this
    // introduced a skew, since the unigram counts will take
    // precedence over the higher-order counts.
    //
    // The current solution retrieves candidates from the highest
    // n-gram table, falling back on lower order n-gram tables if
    // initial completion set is smaller than required.
    //
    std::vector<std::string> prefixCompletionCandidates;
    for (size_t k = cardinality; (k > 0 && prefixCompletionCandidates.size() < max_partial_prediction_size); k--) {
        logger << DEBUG << "Building partial prefix completion table of cardinality: " << k << endl;
        // create n-gram used to retrieve initial prefix completion table
        Ngram prefix_ngram(k);
        copy(tokens.end() - k, tokens.end(), prefix_ngram.begin());

	if (logger.shouldLog()) {
	    logger << DEBUG << "prefix_ngram: ";
	    for (size_t r = 0; r < prefix_ngram.size(); r++) {
		logger << DEBUG << prefix_ngram[r] << ' ';
	    }
	    logger << DEBUG << endl;
	}

        // obtain initial prefix completion candidates
        db->beginTransaction();

        NgramTable partial;

        if (filter == 0) {
	    partial = db->getNgramLikeTable(prefix_ngram,max_partial_prediction_size - prefixCompletionCandidates.size());
	} else {
	    partial = db->getNgramLikeTableFiltered(prefix_ngram,filter, max_partial_prediction_size - prefixCompletionCandidates.size());
	}

        db->endTransaction();

	if (logger.shouldLog()) {
	    logger << DEBUG << "partial prefixCompletionCandidates" << endl
	           << DEBUG << "----------------------------------" << endl;
	    for (size_t j = 0; j < partial.size(); j++) {
		for (size_t k = 0; k < partial[j].size(); k++) {
		    logger << DEBUG << partial[j][k] << " ";
		}
		logger << endl;
	    }
	}

        logger << DEBUG << "Partial prefix completion table contains " << partial.size() << " potential completions." << endl;

        // append newly discovered potential completions to prefix
        // completion candidates array to fill it up to
        // max_partial_prediction_size
        //
        std::vector<Ngram>::const_iterator it = partial.begin();
        while (it != partial.end() && prefixCompletionCandidates.size() < max_partial_prediction_size) {
            // only add new candidates, iterator it points to Ngram,
            // it->end() - 2 points to the token candidate
            //
            std::string candidate = *(it->end() - 2);
            if (find(prefixCompletionCandidates.begin(),
                     prefixCompletionCandidates.end(),
                     candidate) == prefixCompletionCandidates.end()) {
                prefixCompletionCandidates.push_back(candidate);
            }
            it++;
        }
    }

    if (logger.shouldLog()) {
	logger << DEBUG << "prefixCompletionCandidates" << endl
	       << DEBUG << "--------------------------" << endl;
	for (size_t j = 0; j < prefixCompletionCandidates.size(); j++) {
	    logger << DEBUG << prefixCompletionCandidates[j] << endl;
	}
    }

    // compute smoothed probabilities for all candidates
    //
    db->beginTransaction();
    // getUnigramCountsSum is an expensive SQL query
    // caching it here saves much time later inside the loop
    int unigrams_counts_sum = db->getUnigramCountsSum(); 
    for (size_t j = 0; (j < prefixCompletionCandidates.size() && j < max_partial_prediction_size); j++) {
        // store w_i candidate at end of tokens
        tokens[cardinality - 1] = prefixCompletionCandidates[j];

	logger << DEBUG << "------------------" << endl;
	logger << DEBUG << "w_i: " << tokens[cardinality - 1] << endl;

	double probability = 0;
	for (int k = 0; k < cardinality; k++) {
	    double numerator = count(tokens, 0, k+1);
	    // reuse cached unigrams_counts_sum to speed things up
	    double denominator = (k == 0 ? unigrams_counts_sum : count(tokens, -1, k));
	    double frequency = ((denominator > 0) ? (numerator / denominator) : 0);
	    probability += deltas[k] * frequency;

	    logger << DEBUG << "numerator:   " << numerator << endl;
	    logger << DEBUG << "denominator: " << denominator << endl;
	    logger << DEBUG << "frequency:   " << frequency << endl;
	    logger << DEBUG << "delta:       " << deltas[k] << endl;

            // for some sanity checks
	    assert(numerator <= denominator);
	    assert(frequency <= 1);
	}

        logger << DEBUG << "____________" << endl;
        logger << DEBUG << "probability: " << probability << endl;

	if (probability > 0) {
	    prediction.addSuggestion(Suggestion(tokens[cardinality - 1], probability));
	}
    }
    db->endTransaction();

    logger << DEBUG << "Prediction:" << endl;
    logger << DEBUG << "-----------" << endl;
    logger << DEBUG << prediction << endl;

    return prediction;
}
void run_DataGammaJetsZllToZnunu(){

	// defs ---------------------------------------------------------
	gSystem->CompileMacro("../MT2Code/src/MT2Shapes.cc", "k");
	
	// logStream
	fLogStream = new std::ostringstream();
	
	// create dir
	if(!fOutDir.EndsWith("/")) fOutDir += "/";
	char cmd[500];
	sprintf(cmd,"mkdir -p %s", fOutDir.Data());
	system(cmd);

	DefineCutStreams(fHTmin, fHTmax, fMT2min, fMT2max);
	TString filename=fOutDir+"/"+fRootFile;
	fOutFile = new TFile(filename.Data(), "RECREATE");
	fDir     = (TDirectory*) fOutFile;

	// fix output dir
//	if(fHTmax <10000) fOutDir= TString::Format("%s_%d_HT_%d",  fOutDir.Data(), abs(fHTmin),  abs(fHTmax));
//	else              fOutDir= TString::Format("%s_%d_HT_%s",  fOutDir.Data(), abs(fHTmin),  "Inf");
//	if(fMT2max<10000) fOutDir= TString::Format("%s_%d_MT2_%d", fOutDir.Data(), abs(fMT2min), abs(fMT2max));
//	else              fOutDir= TString::Format("%s_%d_MT2_%s", fOutDir.Data(), abs(fMT2min), "Inf");
	
	// log MT2 and HT cuts
	*fLogStream << "------------------------------------------------------------------------------------------------" << endl;
	*fLogStream << "+++ new Znunu with Gamma+jets prediction                                                     +++" << endl;
	*fLogStream << "+++ outputdir: " << fOutDir <<                                                              "+++" << endl; 
	*fLogStream << "------------------------------------------------------------------------------------------------" << endl;
	
	// new prediction ------------------------------------------------------
	Prediction* prediction = new Prediction();
	prediction->fVerbose=fVerbose;
	prediction->fSave   =fSaveResults;


	// Photon Pt
	if(fDoPhotonPtShape){
  	const int gNMT2bins                   = 11;
  	double  gMT2bins[gNMT2bins+1]   = {150, 160, 170, 180, 190, 200, 225, 250, 300, 400, 550, 800}; 	
	prediction->ChPhotonPt = new Channel("PhotonPt", "photon[0].lv.Pt()", cutStream_PhotonPt.str().c_str(), fTriggerStream.str().c_str(),fSamplesPhotonPt);
	prediction->ChPhotonPt->fVerbose =prediction->fVerbose;
//	prediction->ChPhotonPt->GetShapes("PhotonPt", "#gamma Pt", 2, 300, 800);
	prediction->ChPhotonPt->GetShapes("PhotonPt", "#gamma Pt", 100, 150, 800);
//	prediction->ChPhotonPt->GetShapes("PhotonPt", "#gamma Pt", gNMT2bins, gMT2bins);
	}
	
	// Zll Pt
	if(fDoZllPtShape){
  	const int gNMT2bins                   = 11;
  	double  gMT2bins[gNMT2bins+1]   = {150, 160, 170, 180, 190, 200, 225, 250, 300, 400, 550, 800}; 	
	prediction->ChZllPt = new Channel("ZllPt", "RecoOSDiLeptPt(20,2.4,71,111)", cutStreamZll.str().c_str(), fTriggerStream.str().c_str(),fSamplesZllPt);
	prediction->ChZllPt->fVerbose =prediction->fVerbose;
//	prediction->ChZllPt->GetShapes("ZllPt", "Zll Pt", 2, 300, 800);
	prediction->ChZllPt->GetShapes("ZllPt", "Zll Pt", 100, 150, 800);
//	prediction->ChZllPt->GetShapes("ZllPt", "Zll Pt", gNMT2bins, gMT2bins);
	}

	// compute Zll/gamma pt ratio
	if(fDoPhotonPtShape && fDoZllPtShape){
		TH1D* hPhotonToZllPtRatio = prediction->GetRatio(fDoDataZllToPhotonRatio? prediction->ChZllPt->hData    : prediction->ChZllPt->hZJetsToLL, 
				                                 fDoDataZllToPhotonRatio? prediction->ChPhotonPt->hData : prediction->ChPhotonPt->hPhotons, 4);
		DrawHisto(hPhotonToZllPtRatio,hPhotonToZllPtRatio->GetName(),"EX0");
		TString rationame=hPhotonToZllPtRatio->GetName();
		rationame +="_fittedRatio";
		TF1 *f_lin = new TF1(rationame,"pol0(0)", fZllToGammaFitMin , fZllToGammaFitMax);   f_lin->SetLineColor(8);
		if(fDoFits){ 
			hPhotonToZllPtRatio->Fit(rationame,"0L","", fZllToGammaFitMin, fZllToGammaFitMax);    // set al weights to 1
			fZllToPhotonRatio   = f_lin->GetParameter(0);
		} else{
			fZllToPhotonRatio   = prediction->GetLimitedRatio(fDoDataZllToPhotonRatio? prediction->ChZllPt->hData: prediction->ChZllPt->hZJetsToLL,
			                                              fDoDataZllToPhotonRatio? prediction->ChPhotonPt->hData : prediction->ChPhotonPt->hPhotons,
				                                      fZllToGammaFitMin, fZllToGammaFitMax, false, fZllToPhotonRatioRelErr);
			f_lin->SetParameter(0,fZllToPhotonRatio);
		}
  		const int nBins= 3;
		const double Bins[nBins+1] = {150, fZllToGammaFitMin>150?fZllToGammaFitMin:150.0001, fZllToGammaFitMax<800? fZllToGammaFitMax:799.99, 800};
		TH1D* hErrorbar = new TH1D(rationame.Data(), "", nBins, Bins);
		hErrorbar->SetBinContent(2,fZllToPhotonRatio);
		hErrorbar->SetBinError(2,fZllToPhotonRatioRelErr*fZllToPhotonRatio);
		hErrorbar->SetBinContent(1,-10);
		hErrorbar->SetBinError( 1,0);
		hErrorbar->SetFillColor(5);
		hErrorbar->SetFillStyle(3001);
		hErrorbar->Draw("e2same");
		f_lin->Draw("same");
		hPhotonToZllPtRatio->Draw("EX0same");
	}
	
	// GenLevel Zll Pt, no acceptance cuts
	if(fDoGenZllShape){
	prediction->ChGenZllPt = new Channel("GenZllPt", "GenDiLeptPt(0,10,0,1000,true)", cutStreamGenZll.str().c_str(), fTriggerStream.str().c_str(),fSamplesZllPtMConly);
	prediction->ChGenZllPt->fVerbose =prediction->fVerbose;
	prediction->ChGenZllPt->GetShapes("GenZllPt", "GenZll Pt", 8, 0, 800);
	}
	
	// GenLevel Zll Pt, within acceptance
	if(fDoGenAccZllShape){
	prediction->ChGenZllPtAcc = new Channel("GenZllPtAcc", "GenDiLeptPt(20,2.4,71,111,true)", cutStreamGenZllAcc.str().c_str(), fTriggerStream.str().c_str(),fSamplesZllPtMConly);
	prediction->ChGenZllPtAcc->fVerbose =prediction->fVerbose;
	prediction->ChGenZllPtAcc->GetShapes("GenZllPtAcc", "GenZll Pt", 8, 0, 800);
	}
	// Get Acceptance Efficiency
	if(fDoGenZllShape && fDoGenAccZllShape){
		Bool_t binomial =true;
		TH1D* hZllAccEff = prediction->GetRatio(prediction->ChGenZllPtAcc->hZJetsToLL, prediction->ChGenZllPt->hZJetsToLL, 1, binomial);
		DrawHisto(hZllAccEff,hZllAccEff->GetName(),"EX");
//		TString rationame=hZllAccEff->GetName();
//		rationame +="_fittedRatio";
//		TF1 *f_lin = new TF1(rationame,"pol0(0)", fZllAccFitMin ,   fZllAccFitMax);   f_lin->SetLineColor(8);
//		if(fDoFits){ 
//			hZllAccEff->Fit(rationame,"0L","", fZllAccFitMin, fZllAccFitMax);    // set al weights to 1
//			fZllAccEff= f_lin->GetParameter(0);
//		} else{
//			fZllAccEff= prediction->GetLimitedRatio(prediction->ChGenZllPtAcc->hZJetsToLL,prediction->ChGenZllPt->hZJetsToLL,
//				                                fZllAccFitMin, fZllAccFitMax, true, fZllAccEffRelErr);
//			f_lin->SetParameter(0,fZllAccEff);
//		}
//  		const int nBins= 3;
//		const double Bins[nBins+1] = {150, fZllAccFitMin>150?fZllAccFitMin:150.0001, fZllAccFitMax<800? fZllAccFitMax:799.99, 800};
//		TH1D* hErrorbar = new TH1D(rationame.Data(), "", nBins,Bins);
//		hErrorbar->SetBinContent(2,fZllAccEff);
//		hErrorbar->SetBinError(2,fZllAccEffRelErr*fZllAccEff);
//		hErrorbar->SetBinContent(1,-1);
//		hErrorbar->SetBinError(1,0);
//		hErrorbar->SetFillColor(5);
//		hErrorbar->SetFillStyle(3001);
//		hErrorbar->Draw("e2same");
//		f_lin->Draw("same");
//		hZllAccEff->Draw("EX0same");
	}
	
	// GenLevel Zll Pt, within acceptance, OS dilepton recoed
	if(fDoGenAccZllRecoShape){
	prediction->ChGenZllPtAccRecoed = new Channel("GenZllPtAccRecoed", "GenDiLeptPt(20,2.4,71,111,true)", cutStreamGenZllAcc_recoed.str().c_str(), fTriggerStream.str().c_str(),fSamplesZllPtMConly);
	prediction->ChGenZllPtAccRecoed->fVerbose =prediction->fVerbose;
	prediction->ChGenZllPtAccRecoed->GetShapes("GenZllPtAccRecoed", "GenZll Pt", 100, 150, 800);
	}
	if(fDoGenAccZllShape && fDoGenAccZllRecoShape){
		Bool_t binomial =true;
		TH1D* hZllRecoEff = prediction->GetRatio(prediction->ChGenZllPtAccRecoed->hZJetsToLL, prediction->ChGenZllPtAcc->hZJetsToLL, 4, binomial);
		DrawHisto(hZllRecoEff,hZllRecoEff->GetName(),"EX");
		TString rationame=hZllRecoEff->GetName();
		rationame +="_fittedRatio";
		TF1 *f_lin = new TF1(rationame,"pol0(0)", fZllRecoEffFitMin , fZllRecoEffFitMax);   f_lin->SetLineColor(8);
		if(fDoFits){ 
			hZllRecoEff->Fit(rationame,"0L","", fZllRecoEffFitMin, fZllRecoEffFitMax);    // set al weights to 1
			fZllRecoEff= f_lin->GetParameter(0);
		} else{
			fZllRecoEff= prediction->GetLimitedRatio(prediction->ChGenZllPtAccRecoed->hZJetsToLL,prediction->ChGenZllPtAcc->hZJetsToLL,
				                                fZllRecoEffFitMin, fZllRecoEffFitMax, true, fZllRecoEffRelErr);
			f_lin->SetParameter(0,fZllRecoEff);
		}
  		const int nBins= 3;
		const double Bins[nBins+1] = {150, fZllRecoEffFitMin>150?fZllRecoEffFitMin:150.001, fZllRecoEffFitMax<800? fZllRecoEffFitMax:799.99, 800};
		TH1D* hErrorbar = new TH1D(rationame.Data(), "", nBins,Bins);
		hErrorbar->SetBinContent(2,fZllRecoEff);
		hErrorbar->SetBinError(2,fZllRecoEffRelErr*fZllRecoEff);
		hErrorbar->SetBinContent(1,-1);
		hErrorbar->SetBinError(1,0);
		hErrorbar->SetFillColor(5);
		hErrorbar->SetFillStyle(3001);
		hErrorbar->Draw("e2same");
		f_lin->Draw("same");
		hZllRecoEff->Draw("EX0same");
	}
	
	// Photons Hadronic Search MT2
	if(fDoPhotonMT2Shape){
	prediction->ChPhotonsMT2 = new Channel("PhotonsMT2", "photon[0].lv.Pt()", cutStreamPhotonMT2.str().c_str(), fTriggerStream.str().c_str(),fSamplesPhotonPt);
	prediction->ChPhotonsMT2->fVerbose =prediction->fVerbose;
	prediction->ChPhotonsMT2->GetShapes("PhotonsMT2", "MET", 40, 0, 800);
	}

	// Znunu Hadronic Search MT2
	if(fDoZnunuMT2Shape){
	prediction->ChZnunuMT2 = new Channel("ZnunuMT2", "misc.MET", cutStreamZnunuMT2.str().c_str(), fTriggerStream.str().c_str(),fSamplesZnunu);
	prediction->ChZnunuMT2->fVerbose =prediction->fVerbose;
	prediction->ChZnunuMT2->GetShapes("ZnunuMT2", "MET", 40, 0, 800);
	}

	if(fDoZnunuMT2Shape && fDoPhotonMT2Shape){
	TH1D* ZnunuToPhotonMT2Ratio = prediction->GetRatio(prediction->ChZnunuMT2->hZJetsToNuNu, prediction->ChPhotonsMT2->hPhotons, 1);
	DrawHisto(ZnunuToPhotonMT2Ratio,ZnunuToPhotonMT2Ratio->GetName(),"EX0");
	}


	
	// Do Pt spectra comparison plot
	if(fDoPtSpectraComparison && fDoPhotonPtShape && fDoZllPtShape){
		*fLogStream<< "************************* produce pr spectra plot ****************************** " << endl;
		TH1D* hZllMC_cp      = prediction->GetScaledHisto(prediction->ChZllPt->hZJetsToLL, fDoPtSpectraComparisonScaling?(prediction->ChZllPt->hData->Integral())/(prediction->ChZllPt->hZJetsToLL->Integral())    :1, 0, 1);
		TH1D* hZllData_cp    = prediction->GetScaledHisto(prediction->ChZllPt->hData,    1, 0, 1) ;
		TH1D* hPhotonMC_cp   = prediction->GetScaledHisto(prediction->ChPhotonPt->hPhotons,fDoPtSpectraComparisonScaling?(prediction->ChPhotonPt->hData->Integral())/(prediction->ChPhotonPt->hPhotons->Integral()):1, 0, 1);
		TH1D* hPhotonData_cp = prediction->GetScaledHisto(prediction->ChPhotonPt->hData,    1, 0, 1);

		if(fDoPtSpectraComparisonAccCorr){
			TFile *f = new TFile("../RootMacros/ZllAcceptance.root", "OPEN");
			TH1D*  hZllAcc = (TH1D*) f->Get("ZJetsToLL_GenZllPtAcc_ZJetsToLL_GenZllPt_Ratio");
			if (hZllAcc==0) {cout << "WARNING: could not get histo ZJetsToLL_GenZllPtAcc_ZJetsToLL_GenZllPt_Ratio" << endl; exit(1);}
			for(int i=1; i<=hZllMC_cp->GetNbinsX(); ++i){
				if(hZllAcc->GetBinLowEdge(i) != hZllMC_cp->GetBinLowEdge(i)) {cout << "Zll Acc Correction: binnin does not match!!" << endl; exit(1);}
				if(hZllMC_cp  ->GetBinContent(i)<=0) continue;
				double acc_eff   = hZllAcc->GetBinContent(i);
				double orig_mc   = hZllMC_cp     ->GetBinContent(i);
				double orig_data = hZllData_cp   ->GetBinContent(i);
				cout << "bin i " << i << " acc eff " << acc_eff << " orig_mc " << orig_mc << " become " << orig_mc/acc_eff 
				     << " orig_data " << orig_data << " becomes " << orig_data/acc_eff << endl;
				hZllMC_cp   ->SetBinContent(i, orig_mc  /acc_eff);
				hZllData_cp ->SetBinContent(i, orig_data/acc_eff);
			}
			delete f;
		}
		
		hZllMC_cp->SetMarkerStyle(22);
		hZllMC_cp->SetLineColor(kOrange);
		hZllMC_cp->SetMarkerColor(kOrange);
		hZllMC_cp->SetMarkerSize(1.2);
		hZllData_cp->SetMarkerStyle(26);
		hZllData_cp->SetMarkerColor(kBlack);
		hZllData_cp->SetLineColor(kBlack);
		hPhotonMC_cp->SetLineColor(kMagenta+2);
		hPhotonMC_cp->SetMarkerColor(kMagenta+2);
		hPhotonMC_cp->SetMarkerStyle(20);
		hPhotonMC_cp->SetMarkerSize(1.2);
		hPhotonData_cp->SetMarkerStyle(4);
		hPhotonData_cp->SetMarkerColor(kBlack);
		hPhotonData_cp->SetLineColor(kBlack);

		TCanvas *col = new TCanvas("ZllToGammaPtSpectra", "", 0, 0, 500, 500);
		col->SetFillStyle(0);
		col->SetFrameFillStyle(0);
//		col->cd();
		gPad->SetFillStyle(0);
		gStyle->SetPalette(1);
		gPad->SetRightMargin(0.15);

		TLegend* leg2 = new TLegend(0.2, 0.6, 0.5, 0.9);	
		leg2->AddEntry(hPhotonMC_cp,"Gamma+Jets MC","p" );
		leg2->AddEntry(hPhotonData_cp,"Photon Data","p" );
		leg2->AddEntry(hZllMC_cp ,"Zll MC","p" );
		leg2->AddEntry(hZllData_cp,"Zll Data","p" );
		leg2 -> SetFillColor(0);
		leg2 -> SetBorderSize(0);
	//	
		hPhotonMC_cp->SetXTitle("V boson p_{T} (GeV)");
		hPhotonMC_cp->SetYTitle("Events / 7 GeV");
		hPhotonMC_cp->Draw("EX");
		hPhotonData_cp->Draw("EXsame");
		hZllMC_cp->Draw("EXsame");
		hZllData_cp->Draw("EXsame");
		leg2->Draw();
	
		TCanvas *c3 = new TCanvas("ZllToGammaPtSpectraRatio", "", 500, 500);
		TH1D* h_ratio         = (TH1D*) hZllMC_cp      ->Clone("h_ratio");	
		TH1D* h_ratioData     = (TH1D*) hZllData_cp    ->Clone("h_ratioData");	
		TH1D* hPhotonMC_cp2   = (TH1D*) hPhotonMC_cp   ->Clone("hPhotonMC_cp2");
		TH1D* hPhotonData_cp2 = (TH1D*) hPhotonData_cp ->Clone("hPhotonData_cp2");
		h_ratio->Rebin(1); h_ratioData->Rebin(1);
		h_ratio->SetYTitle("#frac{Z(ll)}{#gamma}");
		h_ratio->SetXTitle("boson p_{T} (GeV)");
		h_ratio    ->Divide(hPhotonMC_cp2->Rebin(1));
		h_ratioData->Divide(hPhotonData_cp2->Rebin(1));
		h_ratio    ->SetMarkerStyle(20);
		h_ratio    ->SetMarkerSize(1.2);
		h_ratio    ->SetLineColor(kMagenta+2);
		h_ratio    ->SetMarkerColor(kMagenta+2);
		h_ratioData->SetMarkerStyle(26);
		h_ratioData->SetMarkerColor(kBlack);
		h_ratio    ->Draw("EX0");
		h_ratioData->Draw("EX0same");
		
		TLegend* leg3 = new TLegend(0.2, 0.6, 0.5, 0.9);	
		leg3->AddEntry(h_ratioData,"Zll/photon Data","p" );
		leg3->AddEntry(h_ratio    ,"Zll/photon MC","p" );
		leg3 -> SetFillColor(0);
		leg3 -> SetBorderSize(0);
		leg3 ->Draw();
	}

	if(fDoMT2SpectraCompaison && fDoZnunuMT2Shape && fDoPhotonMT2Shape){
		*fLogStream<<  "************************* MT2 spectra comparison ***************  " << endl;
		TH1D* hZNunuMT2      = prediction->GetScaledHisto(prediction->ChZnunuMT2   ->hZJetsToNuNu, fDoMT2SpectraCompaisonScaling?(prediction->ChPhotonsMT2 ->hData->Integral())/(prediction->ChPhotonsMT2 ->hPhotons->Integral()):1, 0, 1);
		TH1D* hPhotonMT2     = prediction->GetScaledHisto(prediction->ChPhotonsMT2 ->hPhotons,     fDoMT2SpectraCompaisonScaling?(prediction->ChPhotonsMT2 ->hData->Integral())/(prediction->ChPhotonsMT2 ->hPhotons->Integral()):1, 0, 1);
		TH1D* hPhotonDataMT2 = prediction->GetScaledHisto(prediction->ChPhotonsMT2 ->hData, 1, 0, 1);
		
		hZNunuMT2->SetFillStyle(0);
		hZNunuMT2->SetLineWidth(3);

		TCanvas *col = new TCanvas("ZnunuToGammaPtSpectra", "", 0, 0, 500, 500);
		col->SetFillStyle(0);
		col->SetFrameFillStyle(0);
//		col->cd();
		gPad->SetFillStyle(0);
		gStyle->SetPalette(1);
		gPad->SetRightMargin(0.15);
		gPad->SetLogy(1);

		TLegend* leg2 = new TLegend(0.2, 0.6, 0.5, 0.9);	
		leg2->AddEntry(hPhotonMT2,"#gamma+jets MC","f" );
		leg2->AddEntry(hZNunuMT2,"Z(#nu#nu)+jets MC","l" );
		leg2->AddEntry(hPhotonDataMT2,"data","p" );
		leg2 -> SetFillColor(0);
		leg2 -> SetBorderSize(0);
	//	
		hPhotonMT2->SetXTitle("min #Delta #phi(jets,MET)");
		hPhotonMT2->SetYTitle("Events");
		hPhotonMT2->Draw("hist");
		hZNunuMT2->Draw("histsame");
		hPhotonDataMT2->Draw("EXsame");
		leg2->Draw();
//		gPad->RedrawAxis();
		// cout integral above 250 in MT2 and ratio
//		double sumPhotons=0;
//		double sumZnunu  =0;
//		for(int i=1;  i<=hPhotonMT2->GetNbinsX(); ++i){
//			if(hPhotonMT2->GetBinLowEdge(i)>=250){
//				sumPhotons+=hPhotonMT2->GetBinContent(i);
//				sumZnunu  +=hZNunuMT2 ->GetBinContent(i);
//			}
//		}
//		*fLogStream<< ">>> hPhotonMT2: integral above 250: " << sumPhotons << endl;
//		*fLogStream<< ">>> hZNunuMT2 : integral above 250: " << sumZnunu   << endl;
//		*fLogStream<< ">>> -> Ratio  : "                     << sumZnunu/sumPhotons << endl;
		
	}

	if(fDoZnunuGammaGenPtSpectraComparison){
		*fLogStream<< "*************************ZnunuGammaGenPtSpectraComparison**********" << endl;
		// gen photons
		prediction->ChGenPhotonPt = new Channel("GenPhotonPt", "GenPhoton[0].Pt()", cutStreamGenPhoton.str().c_str(), fTriggerStream.str().c_str(),fSamplesPhotonPtMConly);
		prediction->ChGenPhotonPt->fVerbose =prediction->fVerbose;
		prediction->ChGenPhotonPt->GetShapes("GenPhotonPt", "Gen-level #gamma Pt", 50, 150, 800);
		
		// Gen Znunu
		prediction->ChGenZnunuPt = new Channel("GenZnunuPt", "GenZ[0].Pt()", cutStreamGenZnunu.str().c_str(), fTriggerStream.str().c_str(),fSamplesZnunu);
		prediction->ChGenZnunuPt->fVerbose =prediction->fVerbose;
		prediction->ChGenZnunuPt->GetShapes("GenZnunuPt", "Gen-level Z(#nu#nu) Pt", 50, 150, 800);
		
		prediction->DrawHistos(prediction->ChGenPhotonPt->hPhotons, prediction->ChGenZnunuPt->hZJetsToNuNu,
				       "EX0"                              , "EX0same",
				       kViolet+3                          , kBlack,
				       20                                 ,4,
				       "#gamma Pt"                        ,"Z(#nu#nu)");

		TH1D* hGenPhotonToZnunuRatio = prediction->GetRatio(prediction->ChGenZnunuPt->hZJetsToNuNu, prediction->ChGenPhotonPt->hPhotons, 1, false);
		hGenPhotonToZnunuRatio->SetMarkerColor(kBlack);
		hGenPhotonToZnunuRatio->SetLineColor(kBlack);
		hGenPhotonToZnunuRatio->SetMarkerStyle(4);
		DrawHisto(hGenPhotonToZnunuRatio, "GenPhotonToZnunuRatio","EX0", prediction->ChGenPhotonPt);
	}

	if(fDoPhotonRecoEff){
		prediction->ChGenPhotonPtForRecoEff = new Channel("GenPhotonPtForRecoEff", "GenPhoton[0].Pt()", cutStream_GenPhotonforRecoEff.str().c_str(), fTriggerStream.str().c_str(),fSamplesPhotonPtMConly);
		prediction->ChGenPhotonPtForRecoEff->fVerbose =prediction->fVerbose;
		prediction->ChGenPhotonPtForRecoEff->GetShapes("GenPhotonPtForRecoEff", "Gen-level #gamma Pt", 50, 150, 800);
		
		prediction->ChPhotonPtForRecoEff = new Channel("PhotonPtForRecoEff", "GenPhoton[0].Pt()", cutStream_PhotonPtforRecoEff.str().c_str(), fTriggerStream.str().c_str(),fSamplesPhotonPtMConly);
		prediction->ChPhotonPtForRecoEff->fVerbose =prediction->fVerbose;
		prediction->ChPhotonPtForRecoEff->GetShapes("PhotonPtForRecoEff", "Gen-level #gamma Pt", 50, 150, 800);
		
		prediction->DrawHistos(prediction->ChGenPhotonPtForRecoEff->hPhotons, prediction->ChPhotonPtForRecoEff->hPhotons,
				       "EX0"                              , "EX0same",
				       kViolet+3                          , kBlack,
				       20                                 ,4,
				       "#all"                             ,"recoed");
		TH1D* hPhotonRecoEff = prediction->GetRatio(prediction->ChPhotonPtForRecoEff->hPhotons, prediction->ChGenPhotonPtForRecoEff->hPhotons, 1, false);
		hPhotonRecoEff->SetMarkerColor(kBlack);
		hPhotonRecoEff->SetLineColor(kBlack);
		hPhotonRecoEff->SetMarkerStyle(4);
		DrawHisto(hPhotonRecoEff, "PhotonRecoEff","EX0", prediction->ChPhotonPtForRecoEff);
	}

	// Prediction
	if(fDoPrediction){
		*fLogStream<< "************************* Prediction ****************************** " << endl;

		TH1D* MCZnunu = prediction->GetScaledHisto(prediction->ChZnunuMT2->hZJetsToNuNu,fLumiCorr,0);  // scale to lumi 4400
		double MCZnunuEst    = MCZnunu->GetBinContent(1);
		double MCZnunuEstErr = MCZnunu->GetBinError(1);
		delete MCZnunu;
		if(fDoData){ 
		double nGamma    = prediction->ChPhotonsMT2->hData->Integral();
		double nGammaErr = sqrt(nGamma);
		*fLogStream << "********** Data Prediction ***************************************************** " << endl;
		MakePrediction(nGamma, nGammaErr, fZllToPhotonRatio, fZllToPhotonRatioRelErr, fZllAccEff, fZllAccEffRelErr, fZllRecoEff, fZllRecoEffRelErr, MCZnunuEst, MCZnunuEstErr);
		}
		TH1D* hDummy =   prediction->GetScaledHisto(prediction->ChPhotonsMT2->hPhotons, 1, 0);
		float nGammaMC   =hDummy->GetBinContent(1);
		float nGammaMCErr=hDummy->GetBinError(1);
		*fLogStream << "********** MC Prediction ***************************************************** " << endl;
		MakePrediction(nGammaMC, nGammaMCErr, fZllToPhotonRatio, fZllToPhotonRatioRelErr, fZllAccEff, fZllAccEffRelErr, fZllRecoEff, fZllRecoEffRelErr, MCZnunuEst, MCZnunuEstErr);
	}

	// write -----------
	if(fWriteToFile){
		TString logname =fOutDir + ".log"; 
		ofstream f_log (logname.Data(), ios::app);
		f_log << fLogStream->str();
	} else{
		cout << fLogStream->str();
	}

//	fOutFile->Close();

}
Exemple #14
0
int GetBinPrediction()
{

    cout << "Loading Ra2bBin.C" << endl;
    gROOT->ProcessLine(".L RA2bBin.C+");
    cout << "Loading Prediction.C" << endl;
    gROOT->ProcessLine(".L Prediction.C+");
    cout << "Loading Expectation.C" << endl;
    gROOT->ProcessLine(".L Expectation.C+");

    // ------------------------------------------------------------------- //


    Expectation* expec = new Expectation();
    TChain* expectation = new TChain("PredictionTree");
    Prediction* predic = new Prediction();
    TChain* prediction = new TChain("PredictionTree");

    // ------------------------------------------------------------------- //

    ifstream myfile1 ("filelist.txt");

    string root_file;
    if (myfile1.is_open()) {
        while( myfile1.good() ) {
            getline (myfile1,root_file);
            cout << root_file << endl;
            if (root_file.length() > 0) {
                TString path = root_file;
                expectation->Add(path);
                prediction->Add(path);
            }
        }
        myfile1.close();
    }

    // ------------------------------------------------------------------- //

    expectation->Process(expec);
    prediction->Process(predic);

    // ------------------------------------------------------------------- //

    gROOT->SetStyle("Plain");
    gStyle->SetPalette(51, 0);

    // For the canvas:
    gStyle->SetCanvasColor(0);
    gStyle->SetCanvasBorderMode(0);

    // For the Pad:
    gStyle->SetPadColor(0);
    gStyle->SetPadTickX(1);
    gStyle->SetPadTickY(1);
    gStyle->SetPadBorderSize(2);
    gStyle->SetPadBorderMode(0);

    // For the frame:
    gStyle->SetFrameBorderMode(0);

    // For the histo:
    gStyle->SetMarkerSize(0.7);
    gStyle->SetMarkerStyle(20);
    gStyle->SetMarkerColor(1);

    // For the statistics box:
    gStyle->SetOptStat(0);
    gStyle->SetOptFit(1011);

    // Margins:
    gStyle->SetPadBottomMargin(0.25);
    gStyle->SetPadTopMargin(0.15);
    gStyle->SetPadLeftMargin(0.15);
    gStyle->SetPadRightMargin(0.1);

    // For the Global title:
    gStyle->SetOptTitle(0);
    gStyle->SetTitleColor(1);
    gStyle->SetTitleFillColor(10);
    gStyle->SetTitleTextColor(1);
    gStyle->SetTitleFont(42);
    gStyle->SetTitleFontSize(0.05);
    gStyle->SetTitleBorderSize(0);

    // For the axis
    gStyle->SetNdivisions(510, "X");
    gStyle->SetNdivisions(510, "Y");
    gStyle->SetTickLength(0.03);

    // For the axis titles:
    gStyle->SetTitleOffset(1.4, "X");
    gStyle->SetTitleOffset(1.25, "Y");
    gStyle->SetTitleOffset(1.2, "Y");
    gStyle->SetTitleOffset(0.5, "Z");
    gStyle->SetTitleSize(0.05, "XYZ");
    gStyle->SetTitleSize(0.061, "XYZ");
    gStyle->SetTitleFont(42, "XYZ");
    gStyle->SetTitleX(0.15);
    gStyle->SetTitleY(0.99);

    // For the axis labels:
    gStyle->SetLabelSize(0.04, "XYZ");
    gStyle->SetLabelOffset(0.01, "XYZ");
    gStyle->SetLabelFont(42, "XYZ");

    // For the legend
    gStyle->SetLegendBorderSize(0);

    gROOT->ForceStyle();

    enum {cqcd=kRed+3, cltau=kYellow, cllep=kRed+1, czinv=kGreen+1, cdata=kBlack, cpred=kBlue+2};

    TH1F* h_pred = predic->CalcPrediction(predic->yields_2D);
    TH1F* h_exp = expec->yields;

    int NBins = h_pred->GetNbinsX();

    h_pred->SetTitle("");
    h_exp->SetTitle("");

    h_pred->SetLineColor(cqcd );
    h_exp->SetLineColor(cdata);

    h_pred->SetFillColor(cqcd );
    h_exp->SetFillColor(cdata);

    h_pred->SetMarkerColor(cqcd );
    h_exp->SetMarkerColor(cdata);

    h_exp->SetMarkerStyle(20);
    h_exp->SetMarkerSize (1.2);
    h_exp->SetLineWidth(2);

    h_exp->GetYaxis()->SetTitleSize(0.08);
    h_exp->GetYaxis()->SetLabelSize(0.06);

    h_pred->GetYaxis()->SetTitleSize(0.08);
    h_pred->GetYaxis()->SetLabelSize(0.06);

    gStyle->SetHatchesSpacing(0.5);
    gStyle->SetHatchesLineWidth(1);

    TCanvas *c = new TCanvas("c", "c", 1200, 800);
    c->cd();
    TPad *pad1 = new TPad("pad1", "pad1", 0, 0.45, 1, 1);
    pad1->SetFillStyle(4000);
    pad1->Draw();
    pad1->SetLogy();
    pad1->SetTopMargin(0.1);
    pad1->SetBottomMargin(0);
    pad1->SetRightMargin(0.05);
    pad1->SetLeftMargin(0.15);
    pad1->cd();

    TH1F *vFrame1 = pad1->DrawFrame(0.0, 0.1, (double) NBins, 20000.0);
    vFrame1->GetYaxis()->SetTitle("Events");
    vFrame1->GetYaxis()->SetTitleOffset(-1.3);
    vFrame1->GetYaxis()->SetTitleSize(0.15);
    vFrame1->GetYaxis()->SetTitleFont(42);
    vFrame1->GetYaxis()->SetLabelOffset(-0.04);
    vFrame1->GetYaxis()->SetLabelSize(0.05);
    vFrame1->GetYaxis()->SetLabelFont(42);
    vFrame1->GetXaxis()->SetLabelOffset(1.0);
    vFrame1->GetYaxis()->SetTickLength(0.02);
    vFrame1->GetYaxis()->SetTicks("+");
    vFrame1->SetFillStyle(4000);
    h_pred->Draw("same histe");
    h_exp->Draw("same pe");

    TLatex *title = new TLatex(0., 30000, "Simulation, L = 10 fb^{ -1}, #sqrt{s} = 13 TeV");
    title->SetNDC(0);
    title->SetTextFont(42);
    title->SetTextSize(0.06);
    title->Draw("same");

    pad1->cd();
    c->cd();
    TLegend *leg1 = new TLegend(0.7,0.85,0.95,0.93,NULL,"NDC");
    leg1->SetLineColor(0);
    leg1->SetLineStyle(1);
    leg1->SetLineWidth(1);
    leg1->SetFillColor(0);
    leg1->SetFillStyle(4000);
    leg1->SetTextSize(0.025);
    leg1->SetTextFont(42);
    leg1->AddEntry(h_exp, "MC Expectation", "lp");
    leg1->AddEntry(h_pred,  "R+S Prediction", "lf");
    leg1->Draw("same");

    double pred[NBins];
    double pred_err[NBins];
    double exp[NBins];
    double exp_err[NBins];
    double x[NBins];
    double y[NBins];
    double ey_UP[NBins];
    double ey_DN[NBins];
    double r[NBins];
    double r_e[NBins];
    double bins_e[NBins];
    for(int ii=0; ii<NBins; ii++) {
        pred[ii] = h_pred->GetBinContent(ii+1);
        pred_err[ii] = h_pred->GetBinError(ii+1);
        exp[ii] = h_exp->GetBinContent(ii+1);
        exp_err[ii] = h_exp->GetBinError(ii+1);
        bins_e[ii] = 0.5;
        x[ii] = 0.5+ii;
        y[ii] = 0.;
        ey_UP[ii] = pred_err[ii]/pred[ii];
        ey_DN[ii] = pred_err[ii]/pred[ii];
        r[ii] = (pred[ii]-exp[ii])/exp[ii];
        r_e[ii] = sqrt((pred_err[ii]/exp[ii])*(pred_err[ii]/exp[ii])+(exp_err[ii]*pred[ii]/exp[ii]/exp[ii])*(exp_err[ii]*pred[ii]/exp[ii]/exp[ii]));
    }

    TGraphAsymmErrors *ratio = new TGraphAsymmErrors(NBins, x, r, bins_e, bins_e, r_e, r_e);

    ratio->SetMarkerColor(cdata);
    ratio->SetMarkerStyle(20);
    ratio->SetMarkerSize (1.2);
    ratio->SetLineColor(cdata);
    ratio->SetLineWidth(2);

    TH1F *h_ratio = new TH1F("h_ratio_mc", "h_ratio_mc", NBins, 0, NBins);

    h_ratio->SetMarkerColor(cdata);

    h_ratio->SetMarkerStyle(20);
    h_ratio->SetMarkerSize (1.2);
    h_ratio->SetLineWidth(1);
    h_ratio->SetMinimum(-1.2);
    h_ratio->SetMaximum(2.2);

    for (int ibin=1; ibin<=h_exp->GetNbinsX(); ibin++) {
        h_ratio->SetBinContent(ibin, 0);
        h_ratio->SetBinError(ibin, 0);
        h_ratio->GetXaxis()->SetBinLabel(ibin, h_pred->GetXaxis()->GetBinLabel(ibin));
    }

    c->cd();
    TPad *pad2 = new TPad("pad2", "pad2", 0., 0., 1, 0.5);
    pad2->SetTopMargin(0.0);
    pad2->SetRightMargin(0.05);
    pad2->SetLeftMargin(0.15);
    pad2->SetBottomMargin(0.4);
    pad2->Draw("same");
    pad2->cd();

    TH1F *vFrame2 = pad2->DrawFrame(0.0, 0.45, (double) NBins, 1.5);
    vFrame2->GetYaxis()->SetTitle("(Pred.-Exp.)/Exp.");
    vFrame2->GetYaxis()->SetTitleOffset(2.6);
    vFrame2->GetYaxis()->SetTitleSize(0.08);
    vFrame2->GetYaxis()->SetTitleFont(42);
    vFrame2->GetYaxis()->SetLabelOffset(0.08);
    vFrame2->GetYaxis()->SetLabelSize(0.06);
    vFrame2->GetYaxis()->SetLabelFont(42);
    vFrame2->GetXaxis()->SetLabelOffset(1.0);
    vFrame2->GetYaxis()->SetTickLength(-0.02);
    vFrame2->GetYaxis()->SetTicks("+");


    h_ratio->SetTitle("");
    h_ratio->GetYaxis()->SetTitle("(Pred.-Exp.)/Exp.");
    h_ratio->GetYaxis()->SetTitleOffset(0.6);
    h_ratio->GetYaxis()->SetLabelOffset(0.02);
    h_ratio->GetYaxis()->SetTickLength(0.02);
    h_ratio->GetYaxis()->SetTitleSize(0.08);
    h_ratio->GetYaxis()->SetLabelSize(0.06);
    h_ratio->GetXaxis()->SetLabelOffset(0.01);
    h_ratio->Draw("e5");
    ratio->Draw("P0Z");

    c->SaveAs("BinByBinClosure_test.pdf");

    return 1;

}
void DejavuPredictorTest::testPredict()
{
    *stream << "polly wants a cracker ";
    ct->update();

    // get pointer to dejavu predictor
    Predictor* predictor = predictorRegistry->iterator().next();
    
    {
        *stream << "polly ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "wants ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cracker", 1.0));
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    *stream << "soda ";
    ct->update();

    {
        *stream << "polly ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "wants ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cracker", 1.0));
        expected.addSuggestion(Suggestion("soda",    1.0));
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    *stream << "cake ";
    ct->update();

    {
        *stream << "polly ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "wants ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cake",    1.0));
        expected.addSuggestion(Suggestion("cracker", 1.0));
        expected.addSuggestion(Suggestion("soda",    1.0));
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, 0));
        ct->update();
    }

    *stream << "crumble ";
    ct->update();

    {
        // test filter
        const char* filter[] = { "cra", "so", 0 };

        *stream << "polly wants a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cracker",    1.0));
        expected.addSuggestion(Suggestion("soda",    1.0));
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, filter));
        ct->update();
    }

    *stream << "break ";
    ct->update();

    {
        // test filter
        const char* filter[] = { "r", 0 };

        *stream << "polly wants a c";
        Prediction expected;
        expected.addSuggestion(Suggestion("cracker",    1.0));
        expected.addSuggestion(Suggestion("crumble",    1.0));
        CPPUNIT_ASSERT_EQUAL(expected, predictor->predict(SIZE, filter));
        ct->update();
    }

    *stream << "uddle ";
    ct->update();
}
void NewSmoothedNgramPluginTest::testFilter()
{
    // get pointer to plugin
    Plugin* plugin = pluginRegistry->iterator().next();

    std::vector<std::string> change;
    change.push_back("foo");
    change.push_back("bar");
    change.push_back("foobar");
    change.push_back("foz");
    change.push_back("baz");
    change.push_back("fozbaz");
    change.push_back("roo");
    change.push_back("rar");
    change.push_back("roobar");

    // Learn some context so that we have data to create non-empty
    // predictions
    // 
    plugin->learn(change);

    // Alternatively, plugin could have learnt thus...
    //    *stream << "foo bar foobar foz baz fozbaz roo rar roobar ";
    //    ct->update();

    {
	Prediction actual = plugin->predict(SIZE, 0);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(9), actual.size());
    }

    {
	const char* filters[] = {"f", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), actual.size());
    }

    {
	const char* filters[] = {"b", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), actual.size());
    }

    {
	const char* filters[] = {"r", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), actual.size());
    }

    {
	const char* filters[] = {"f", "b", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(6), actual.size());
    }

    {
	const char* filters[] = {"f", "r", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), actual.size());
    }

    {
	const char* filters[] = {"f", "b", "r", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(9), actual.size());
    }

    {
	const char* filters[] = {"fo", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), actual.size());
    }

    {
	const char* filters[] = {"foo", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), actual.size());
    }

    {
	const char* filters[] = {"fo", "ba", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(6), actual.size());
    }

    {
	const char* filters[] = {"fo", "ba", "ro", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(8), actual.size());
    }

    {
	const char* filters[] = {"foo", "bar", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), actual.size());
    }

    {
	const char* filters[] = {"foobar", "fozba", "roo", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), actual.size());
    }

    {
	const char* filters[] = {"foobar", "fozbaz", "roobar", 0};
	Prediction actual = plugin->predict(SIZE, filters);
	CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), actual.size());
    }

}
int main(int argc, char** argv) {
  int nrf = 250;
  Vector trainMSE(2);
  Vector testMSE(2);
  Vector noise_min(2);
  Vector noise_max(2);
  // train + test time for transformer and machine
  double tic;
  double trtrtime = 0.0;
  double trtetime = 0.0;
  double mctrtime = 0.0;
  double mctetime = 0.0; 

  if(argc > 1) sscanf(argv[1], "%d", &nrf);

  // initialize catalogue of machine factory
  registerMachines();
  // initialize catalogue of transformers
  registerTransformers();

  std::cout << "LearningMachine library example (portable)" << std::endl;

  // create Regularized Least Squares learner
  std::string name("RLS");
  MachinePortable mp = MachinePortable("RLS");
  Property p;
  p.put("dom", Value(nrf));
  p.put("cod", Value(2));
  p.put("lambda", Value(0.5));
  mp.getWrapped().configure(p);
  std::cout << "Learner:" << std::endl << mp.getWrapped().getInfo() << std::endl;

  // create Random Feature transformer
  TransformerPortable tp = TransformerPortable("RandomFeature");
  p.clear();
  p.put("dom", Value(2));
  p.put("cod", Value(nrf));
  p.put("gamma", Value(16.0));
  tp.getWrapped().configure(p);
  std::cout << "Transformer:" << std::endl << tp.getWrapped().getInfo() << std::endl;


  // create and feed training samples
  noise_min = NOISE_MIN;
  noise_max = NOISE_MAX;
  
  
  trainMSE = 0.0;
  for(int i = 0; i < NO_TRAIN; i++) {
    // create a new training sample
    std::pair<Vector, Vector> sample = createSample();

    // add some noise to output for training
    Vector noisyOutput = sample.second + Rand::vector(noise_min, noise_max);

    // transform input using RF
    tic = yarp::os::Time::now();
    Vector transInput = tp.getWrapped().transform(sample.first);
    trtrtime += (yarp::os::Time::now() - tic);

    // make prediction before feeding full sample
    tic = yarp::os::Time::now();
    Prediction prediction = mp.getWrapped().predict(transInput);

    // train on complete sample with noisy output
    mp.getWrapped().feedSample(transInput, noisyOutput);
    mctrtime += (yarp::os::Time::now() - tic);

    Vector diff = prediction.getPrediction() - sample.second;
    elementProd(diff, diff);
    trainMSE = trainMSE + diff;
  }
  trainMSE = elementDiv(trainMSE, NO_TRAIN);
  std::cout << "Train MSE: " << trainMSE.toString() << std::endl;
  std::cout << "Train Transformer Time per Sample: " << (trtrtime / NO_TRAIN) << std::endl;
  std::cout << "Train Machine Time per Sample: " << (mctrtime / NO_TRAIN) << std::endl;
  std::cout << "Combined Time per Sample: " << ((trtrtime + mctrtime) / NO_TRAIN) << std::endl;

  std::cout << std::endl;
  std::cout << "Saving machine portable to file 'mp.txt'...";
  bool ok = mp.writeToFile("mp.txt");
  std::cout << ((ok) ? "ok!" : "failed :(") << std::endl;

  std::cout << "Saving transformer portable to file 'tp.txt'...";
  ok = tp.writeToFile("tp.txt");
  std::cout << ((ok) ? "ok!" : "failed :(") << std::endl;

  std::cout << "Loading machine portable from file 'mp.txt'...";
  ok = mp.readFromFile("mp.txt");
  std::cout << ((ok) ? "ok!" : "failed :(") << std::endl;

  std::cout << "Loading transformer portable from file 'tp.txt'...";
  ok = tp.readFromFile("tp.txt");
  std::cout << ((ok) ? "ok!" : "failed :(") << std::endl;
  std::cout << std::endl;

  // predict test samples
  testMSE = 0.;
  for(int i = 0; i < NO_TEST; i++) {
    // create a new testing sample
    std::pair<Vector, Vector> sample = createSample();

    // transform input using RF
    tic = yarp::os::Time::now();
    Vector transInput = tp.getWrapped().transform(sample.first);
    trtetime += (yarp::os::Time::now() - tic);

    // make prediction
    tic = yarp::os::Time::now();
    Prediction prediction = mp.getWrapped().predict(transInput);
    mctetime += (yarp::os::Time::now() - tic);
    Vector diff = prediction.getPrediction() - sample.second;
    elementProd(diff, diff);
    //std::cout << "Sample: " << sample.input <<
    testMSE = testMSE + diff;
  }
  testMSE = elementDiv(testMSE, NO_TEST);
  std::cout << "Test MSE: " << testMSE.toString() << std::endl;
  std::cout << "Test Transformer Time per Sample: " << (trtetime / NO_TEST) << std::endl;
  std::cout << "Test Machine Time per Sample: " << (mctetime / NO_TEST) << std::endl;
  std::cout << "Combined Time per Sample: " << ((trtetime + mctetime) / NO_TEST) << std::endl;

  return 0;
}
Prediction SmoothedCountPlugin::predict(const size_t max_partial_predictions_size, const char** filter) const
{
    // get w_2, w_1, and prefix from HistoryTracker object
    std::string prefix = strtolower( contextTracker->getPrefix() );
    std::string word_1 = strtolower( contextTracker->getToken(1) );
    std::string word_2 = strtolower( contextTracker->getToken(2) );
    
    std::string query; // string used to build sql query
    int result;        // database interrogation diagnostic
    CallbackData data; // data to pass through to callback function
	

    // get most likely unigrams whose w contains prefix
    Prediction predUnigrams;
    
    data.predPtr = &predUnigrams;
    data.predSize = MAX_PARTIAL_PREDICTION_SIZE;
    
    query = 
	"SELECT word, count "
	"FROM _1_gram "
	"WHERE word LIKE \"" + prefix + "%\" "
	"ORDER BY count DESC;";
    
#if defined(HAVE_SQLITE3_H)
    result = sqlite3_exec(
#elif defined(HAVE_SQLITE_H)
    result = sqlite_exec(
#endif
	db,
	query.c_str(),
	buildPrediction,
	&data,
	NULL
    );
    assert(result == SQLITE_OK);


    // get most likely bigrams having matching w_1 whose w contains prefix
    Prediction predBigrams;
    
    data.predPtr = &predBigrams;
    
    query = 
    "SELECT word, count "
    "FROM _2_gram "
    "WHERE word_1 = \"" + word_1 + "\" "
    "AND word LIKE \"" + prefix + "\" "
    "ORDER BY count DESC;";
    
#if defined(HAVE_SQLITE3_H)
    result = sqlite3_exec(
#elif defined(HAVE_SQLITE_H)
    result = sqlite_exec(
#endif
	db,
	query.c_str(),
	buildPrediction,
	&data,
	NULL
    );
    assert(result == SQLITE_OK);


    // get most likely trigrams having matching w_2, w_1 whose w contains prefix
    Prediction predTrigrams;
    
    data.predPtr = &predTrigrams;
    
    query = 
    "SELECT word, count "
    "FROM _3_gram "
    "WHERE word_2 = \"" + word_2 + "\" "
    "AND word_1 = \"" + word_1 + "\" "
    "AND word LIKE \"" + prefix + "\" "
    "ORDER BY count DESC;";
    
#if defined(HAVE_SQLITE3_H)
    result = sqlite3_exec(
#elif defined(HAVE_SQLITE_H)
    result = sqlite_exec(
#endif
	db,
	query.c_str(),
	buildPrediction,
	&data,
	NULL
    );
    assert(result == SQLITE_OK);
	
    
    Prediction p;     // combined result of uni/bi/tri gram predictions
    std::string word; // pivot unigram word (used in next for loop)
    double ccount;    // combined count
    
    // compute smoothed probability estimation
    
    // TODO !!!!!!!! Everything should be scaled down to probabilities!!!
    // TODO That means that counts should be scaled down to values between
    // TODO 0 and 1. We need total word count to do that.
    
    // TODO : after correct word has been found in inner loops, execution
    // TODO : can break out of it.
    for (size_t i = 0; i < predUnigrams.size(); i++) {

	word   = predUnigrams.getSuggestion( i ).getWord();
	ccount = unigram_weight *
	    predUnigrams.getSuggestion( i ).getProbability();
	
	for (size_t j = 0; j < predBigrams.size(); j++) {

	    if( predBigrams.getSuggestion(j).getWord() == word ) {
		
		for (size_t k = 0; k < predTrigrams.size(); k++ ) {
		    
		    if( predTrigrams.getSuggestion(k).getWord() == word ) {
			
			ccount += trigram_weight *
			    predTrigrams.getSuggestion(k).getProbability();
			
		    }
		}
		
		ccount += bigram_weight *
		    predBigrams.getSuggestion(j).getProbability();
		
	    }
	    
	}
	
	p.addSuggestion( Suggestion( word, ccount ) );
	
    }

    return p; // Return combined prediction
}
void DejavuPluginTest::testPredict()
{
    *stream << "polly wants a cracker ";
    ct->update();

    // get pointer to dejavu plugin
    Plugin* plugin = pluginRegistry->iterator().next();

    {
        *stream << "polly ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "wants ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cracker", 1.0));
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    *stream << "soda ";
    ct->update();

    {
        *stream << "polly ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "wants ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cracker", 1.0));
        expected.addSuggestion(Suggestion("soda",    1.0));
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    *stream << "cake ";
    ct->update();

    {
        *stream << "polly ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "wants ";
        Prediction expected;
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }

    {
        *stream << "a ";
        Prediction expected;
        expected.addSuggestion(Suggestion("cake",    1.0));
        expected.addSuggestion(Suggestion("cracker", 1.0));
        expected.addSuggestion(Suggestion("soda",    1.0));
        CPPUNIT_ASSERT_EQUAL(expected, plugin->predict(SIZE, 0));
        ct->update();
    }
}
int main(int argc, char ** argv)
{   
    Matrix allRegr;
    Vector allFT;
    
    Matrix static_identifiable_parameters;
    Vector cad_parameters;
    
    Vector ftStdDev;
    
    onlineMean<Vector> cad_errors;
    onlineMean<Vector> errors;
    
    unsigned num_samples, training_samples;
    
    bool set_weight_prior = false;
    bool set_noise_scaling = false;
    
    ftStdDev = Vector(6);
    ftStdDev[0] = 0.2;
    ftStdDev[1] = 0.2;
    ftStdDev[2] = 0.2;
    ftStdDev[3] = 0.01;
    ftStdDev[4] = 0.01;
    ftStdDev[5] = 0.005;
    
    
    Property params;
    params.fromCommand(argc, argv);
    
    if( params.check("help") ) {
        std::cout << "Run in directory with data produced by inertiaObserver --dump_static" << std::endl;
        return 0;
    }
    
    if( params.check("set_weight_prior") ) {
        set_weight_prior = true;
    }
    
    if( params.check("set_noise_scaling") ) {
        set_noise_scaling = true;
    }
    
    Matrix_read("staticRegr_right_arm.ymt",allRegr);
    Vector_read("staticFT_right_arm.yvc",allFT);
    Matrix_read("staticIdentiableParameters_right_arm.ymt",static_identifiable_parameters);
    Vector_read("cad_parameters_right_arm.yvc",cad_parameters);
    
    Matrix test = eye(2,2);
    Matrix test2;
    
    Matrix_write("test_matrix_lala.ymt",test);
    Matrix_read("test_matrix_lala.ymt",test2);
    
    std::cout << "Wrote matrix: " << std::endl << test.toString() << std::endl;
    std::cout << "Read matrix   " << std::endl << test2.toString() << std::endl;
    
    test2.resize(3,3);
    
    std::cout << "Resized matrix " << std::endl << test2.toString() << std::endl;
    
    assert(allRegr.rows() == allFT.size());
    
    num_samples = allRegr.rows()/6;
    training_samples = 3*num_samples/4;
    
    std::cout << "Read " << allRegr.rows()/6 << " data samples " << std::endl;
    std::cout << "Using " << training_samples << " for training " << std::endl;
    std::cout << "Using " << num_samples-training_samples << " for testing  " << std::endl;

    
    IParameterLearner * param_learner, * offset_cad_learner ;
            
    //~~~~~~~~~~~
    param_learner = new MultiTaskLinearGPRLearner(allRegr.cols(),6);
    param_learner->setName("RLS");
    if( set_weight_prior ) {
        param_learner->setNoiseStandardDeviation(ftStdDev);
    }
    if( set_noise_scaling ) {
        param_learner->setWeightsStandardDeviation(Vector(static_identifiable_parameters.cols()+6,1.0));
    }
    
    //~~~~~~~~~~~
    offset_cad_learner = new MultiTaskLinearGPRLearner(6,6);
    offset_cad_learner->setName("OffsetLearner");
    if( set_noise_scaling ) {
        offset_cad_learner->setNoiseStandardDeviation(ftStdDev);
    }

    size_t i;
    
    //std::cout << "allRegr          " << allRegr.rows() << " " << allRegr.cols() << std::endl;
    //std::cout << "allRegr          " << allRegr.toString() << std::endl;
    
    for(i = 0; i < training_samples; i++ ) {
        //~~~~~~~~~~
        //std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
        std::cout << "~~~~~~~~~~~~~~~~~I : " << i << std::endl;
        //std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
        Matrix regr, regr_no_offset;
        Vector FT;
        regr = allRegr.submatrix(6*i,6*i+6-1,0,allRegr.cols()-1);
        FT = allFT.subVector(6*i,6*i+6-1);
        regr_no_offset = regr.submatrix(0,regr.rows()-1,0,regr.cols()-1-6);
        param_learner->feedSample(regr,FT);
        
        std::cout << "regr 15 col" << regr.getCol(11).toString() << std::endl;

    
        //std::cout << "Regr" << regr.toString() << std::endl;
        
        //std::cout << "allRegr          " << allRegr.rows() << " " << allRegr.cols() << std::endl;
        //std::cout << "allRegr          " << allRegr.getCol(0).toString() << std::endl;
        /*
        std::cout << "regr_no_offset" << regr_no_offset.rows() << " " << regr_no_offset.cols() << std::endl;
        std::cout << "sip           " <<  static_identifiable_parameters.rows() << " " << static_identifiable_parameters.cols() << std::endl;
        std::cout << "cad_parameters " << cad_parameters.size() << std::endl;
        std::cout.flush();
        */
        //std::cout << "regr 1 col" << regr.getCol(0).toString() << std::endl;

        
        Vector FTcad = regr_no_offset*static_identifiable_parameters.transposed()*cad_parameters;
        
        Vector res = FT-FTcad;
        
        offset_cad_learner->feedSample(eye(6,6),res);
    }
    
    Vector offset_cad;
    offset_cad = offset_cad_learner->getParameters();
    
    Vector learned_param = param_learner->getParameters();
    
    Vector learned_param_oneshot;
    
    learned_param_oneshot = pinv(allRegr,1e-7)*allFT;
    
    std::cout << allRegr.getCol(16).toString() << std::endl;
    std::cout << "Learned param offset   " << learned_param.toString() << std::endl;
    std::cout << "Cad parameters         " << (static_identifiable_parameters.transposed()*cad_parameters).toString() << std::endl;
    std::cout << "CAD learned offset     " << offset_cad.toString() << std::endl;
    std::cout << "Learned oneshot offset " << learned_param_oneshot.toString() << std::endl;

    
    for( ; i < num_samples; i++ ) {
        std::cout << "~~~~~~~~~~~~~~~~~I : " << i << std::endl;
        Prediction pred;
        Matrix regr, regr_no_offset;
        Vector FT, predFT, predFTCAD;
        regr = allRegr.submatrix(6*i,6*i+6-1,0,allRegr.cols()-1);
        regr_no_offset = regr.submatrix(0,regr.rows()-1,0,regr.cols()-1-6);
        FT = allFT.subVector(6*i,6*i+6-1);
        
        pred = param_learner->predict(regr);
        predFT = pred.getPrediction();
        
    
        
        predFTCAD = regr_no_offset*static_identifiable_parameters.transposed()*cad_parameters + offset_cad;
        
        
        std::cout << "regr 16 col" << regr.getCol(16).toString() << std::endl;
        /*
        std::cout << "FT        " << FT.toString() << std::endl;
        std::cout << "predFT    " << predFT.toString() << std::endl;
        std::cout << "predFTCAD " << predFTCAD.toString() << std::endl;
        */
        
        errors.feedSample(abs(predFT-FT));
        //std::cout << "Submitted learned error " << abs(predFT-FT).toString() << std::endl;
        cad_errors.feedSample(abs(predFTCAD-FT));
        //std::cout << "Submitted cad error " << abs(predFTCAD-FT).toString() << std::endl;

    }
    
    
    std::cout << "Errors for learned parameters " << errors.getMean().toString() << std::endl;
    std::cout << "Errors for CAD     parameters " << cad_errors.getMean().toString() << std::endl;

    return 0;
}