35   namespace HistFactory {
 
   38     vector< pair<std::string, std::string> > get_comb(vector<std::string> names){
 
   39       vector< pair<std::string, std::string> > list;
 
   40       for(vector<std::string>::iterator itr=names.begin(); itr!=names.end(); ++itr){
 
   41    vector<std::string>::iterator itr2=itr; 
 
   42    for(++itr2; itr2!=names.end();++itr2){
 
   43      list.push_back(pair<std::string, std::string>(*itr, *itr2));
 
   49     vector<EstimateSummary>*  loadSavedInputs(TFile* outFile, std::string channel ){
 
   50       outFile->ShowStreamerInfo();
 
   52       vector<EstimateSummary>* summaries = 
new  vector<EstimateSummary>;
 
   53       outFile->cd(channel.c_str());
 
   56       TIter next(gDirectory->GetListOfKeys()); 
 
   57       EstimateSummary* summary; 
 
   58       while ((summary=(EstimateSummary*) next())) { 
 
   61         cout << 
"was able to read summary with name " << summary->name << std::endl;
 
   62         cout << 
" nominal hist = " << summary->nominal << std::endl;
 
   64      cout << 
" hist name = " << summary->nominal->GetName() <<endl;
 
   65         cout << 
"still ok" << std::endl;
 
   67         summaries->push_back(*summary);
 
   79     void saveInputs(TFile* outFile, std::string channel, vector<EstimateSummary> summaries){
 
   80       vector<EstimateSummary>::iterator it = summaries.begin();
 
   81       vector<EstimateSummary>::iterator end = summaries.end();
 
   82       vector<TH1*>::iterator histIt;
 
   83       vector<TH1*>::iterator histEnd;
 
   84       outFile->mkdir(channel.c_str());
 
   87    if(channel != it->channel){
 
   88      cout << 
"channel mismatch" << std::endl;
 
   91    outFile->cd(channel.c_str());
 
   96    gDirectory->mkdir(it->name.c_str());
 
   97    gDirectory->cd(it->name.c_str());
 
  101    histIt = it->lowHists.begin();
 
  102    histEnd = it->lowHists.end();
 
  103    for(; histIt!=histEnd; ++histIt)
 
  106    histIt = it->highHists.begin();
 
  107    histEnd = it->highHists.end();
 
  108    for(; histIt!=histEnd; ++histIt)
 
  115     TH1 * GetHisto( TFile * inFile, 
const std::string name ){
 
  117       if(!inFile || name.empty()){
 
  118    cerr << 
"Not all necessary info are set to access the input file. Check your config" << std::endl;
 
  119    cerr << 
"fileptr: " << inFile
 
  120         << 
"path/obj: " << name << std::endl;
 
  124       cout << 
"Retrieving " << name ;
 
  126       TH1 * ptr = (TH1 *) (inFile->Get( name.c_str() )->Clone());  
 
  128       cout << 
" found at " << ptr << 
" with integral " << ptr->Integral() << 
" and mean " << ptr->GetMean() << std::endl;
 
  130       if (ptr) ptr->SetDirectory(0); 
 
  136     TH1 * GetHisto( 
const std::string file, 
const std::string path, 
const std::string obj){
 
  139       cout << 
"Retrieving " << file << 
":" << path << obj ;
 
  142       TFile inFile(file.c_str());
 
  143       TH1 * ptr = (TH1 *) (inFile.Get( (path+obj).c_str() )->Clone());
 
  146       cout << 
" found at " << ptr << 
" with integral " << ptr->Integral() 
 
  147       << 
" and mean " << ptr->GetMean() << std::endl;
 
  152    cerr << 
"Not all necessary info are set to access the input file. Check your config"  
  154    cerr << 
"filename: " << file
 
  156         << 
"obj: " << obj << std::endl;
 
  159    ptr->SetDirectory(0); 
 
  165     void AddSubStrings( vector<std::string> & vs, std::string s){
 
  166       const std::string delims(
"\\ ");
 
  167       std::string::size_type begIdx, endIdx;
 
  168       begIdx=s.find_first_not_of(delims);
 
  169       while(begIdx!=string::npos){
 
  170    endIdx=s.find_first_of(delims, begIdx);
 
  171    if(endIdx==string::npos) endIdx=s.length();
 
  172    vs.push_back(s.substr(begIdx,endIdx-begIdx));
 
  173    begIdx=s.find_first_not_of(delims, endIdx);
 
  179     std::vector<std::string> GetChildrenFromString( std::string str ) {
 
  181       std::vector<std::string> child_vec;
 
  183       const std::string delims(
"\\ ");
 
  184       std::string::size_type begIdx, endIdx;
 
  185       begIdx=str.find_first_not_of(delims);
 
  186       while(begIdx!=string::npos){
 
  187    endIdx=str.find_first_of(delims, begIdx);
 
  188    if(endIdx==string::npos) endIdx=str.length();
 
  189    std::string child_name = str.substr(begIdx,endIdx-begIdx);
 
  190    child_vec.push_back(child_name);
 
  191    begIdx=str.find_first_not_of(delims, endIdx);
 
  199     void AddParamsToAsimov( RooStats::HistFactory::Asimov& asimov, std::string str ) {
 
  203       std::vector<std::string> string_list = GetChildrenFromString( str );
 
  208       std::map<std::string, double> param_map;
 
  210       for( 
unsigned int i=0; i < string_list.size(); ++i) {
 
  212    std::string param = string_list.at(i);
 
  214    size_t eql_location = param.find(
"=");
 
  218    if( eql_location==string::npos ) {
 
  219      asimov.SetFixedParam(param);
 
  223      std::string param_name = param.substr(0,eql_location);
 
  224      double param_val = atof( param.substr(eql_location+1, param.size()).c_str() );
 
  226      std::cout << 
"ASIMOV - Param Name: " << param_name
 
  227           << 
" Param Val: " << param_val << std::endl;
 
  229      asimov.SetParamValue(param_name, param_val);
 
  230      asimov.SetFixedParam(param_name);
 
  257     std::vector<EstimateSummary> GetChannelEstimateSummaries(Measurement& measurement, 
 
  264       std::vector<EstimateSummary> channel_estimateSummary;
 
  266       std::cout << 
"Processing data: " << std::endl;
 
  269       EstimateSummary data_es;
 
  270       data_es.name = 
"Data";
 
  271       data_es.channel = channel.GetName();
 
  272       TH1* data_hist = (TH1*) channel.GetData().GetHisto();
 
  273       if( data_hist != NULL ) {
 
  275    data_es.nominal = data_hist;
 
  276    channel_estimateSummary.push_back( data_es );
 
  280       for( 
unsigned int sampleItr = 0; sampleItr < channel.GetSamples().size(); ++sampleItr ) {
 
  282    EstimateSummary sample_es;
 
  283    RooStats::HistFactory::Sample& sample = channel.GetSamples().at( sampleItr );
 
  285    std::cout << 
"Processing sample: " << sample.GetName() << std::endl;
 
  288    sample_es.name = sample.GetName();
 
  289    sample_es.channel = sample.GetChannelName();
 
  290    sample_es.nominal = (TH1*) sample.GetHisto()->Clone();
 
  292    std::cout << 
"Checking NormalizeByTheory" << std::endl;
 
  294    if( sample.GetNormalizeByTheory() ) {
 
  295      sample_es.normName = 
"" ; 
 
  299      lumiStr += measurement.GetLumi();
 
  300      lumiStr.ReplaceAll(
' ', TString());
 
  301      sample_es.normName = lumiStr ;
 
  304    std::cout << 
"Setting the Histo Systs" << std::endl;
 
  307    for( 
unsigned int histoItr = 0; histoItr < sample.GetHistoSysList().size(); ++histoItr ) {
 
  309      RooStats::HistFactory::HistoSys& histoSys = sample.GetHistoSysList().at( histoItr );
 
  311      sample_es.systSourceForHist.push_back( histoSys.GetName() );
 
  312      sample_es.lowHists.push_back( (TH1*) histoSys.GetHistoLow()->Clone()  );
 
  313      sample_es.highHists.push_back( (TH1*) histoSys.GetHistoHigh()->Clone() );
 
  317    std::cout << 
"Setting the NormFactors" << std::endl;
 
  319    for( 
unsigned int normItr = 0; normItr < sample.GetNormFactorList().size(); ++normItr ) {
 
  321      RooStats::HistFactory::NormFactor& normFactor = sample.GetNormFactorList().at( normItr );
 
  323      EstimateSummary::NormFactor normFactor_es;
 
  324      normFactor_es.name = normFactor.GetName();
 
  325      normFactor_es.val  = normFactor.GetVal();
 
  326      normFactor_es.high = normFactor.GetHigh();
 
  327      normFactor_es.low  = normFactor.GetLow();
 
  328      normFactor_es.constant = normFactor.GetConst();
 
  330      sample_es.normFactor.push_back( normFactor_es );
 
  334    std::cout << 
"Setting the OverallSysList" << std::endl;
 
  336    for( 
unsigned int sysItr = 0; sysItr < sample.GetOverallSysList().size(); ++sysItr ) {
 
  338      RooStats::HistFactory::OverallSys& overallSys = sample.GetOverallSysList().at( sysItr );
 
  340      std::pair<double, double> DownUpPair( overallSys.GetLow(), overallSys.GetHigh() );
 
  341      sample_es.overallSyst[ overallSys.GetName() ]  = DownUpPair; 
 
  345    std::cout << 
"Checking Stat Errors" << std::endl;
 
  348    sample_es.IncludeStatError  = sample.GetStatError().GetActivate();
 
  351    sample_es.RelErrorThreshold = channel.GetStatErrorConfig().GetRelErrorThreshold();
 
  352    if( sample.GetStatError().GetErrorHist() ) {
 
  353      sample_es.relStatError      = (TH1*) sample.GetStatError().GetErrorHist()->Clone();
 
  356      sample_es.relStatError    = NULL;
 
  361    Constraint::Type type = channel.GetStatErrorConfig().GetConstraintType();
 
  364    sample_es.StatConstraintType = EstimateSummary::Gaussian;
 
  366    if( type == Constraint::Gaussian) {
 
  367      std::cout << 
"Using Gaussian StatErrors" << std::endl;
 
  368      sample_es.StatConstraintType = EstimateSummary::Gaussian;
 
  370    if( type == Constraint::Poisson ) {
 
  371      std::cout << 
"Using Poisson StatErrors" << std::endl;
 
  372      sample_es.StatConstraintType = EstimateSummary::Poisson;
 
  376    std::cout << 
"Getting the shape Factor" << std::endl;
 
  379    if( sample.GetShapeFactorList().size() > 0 ) {
 
  380      sample_es.shapeFactorName = sample.GetShapeFactorList().at(0).GetName();
 
  382    if( sample.GetShapeFactorList().size() > 1 ) {
 
  383      std::cout << 
"Error: Only One Shape Factor currently supported" << std::endl;
 
  388    std::cout << 
"Setting the ShapeSysts" << std::endl;
 
  391    for( 
unsigned int shapeItr=0; shapeItr < sample.GetShapeSysList().size(); ++shapeItr ) {
 
  393      RooStats::HistFactory::ShapeSys& shapeSys = sample.GetShapeSysList().at( shapeItr );
 
  395      EstimateSummary::ShapeSys shapeSys_es;
 
  396      shapeSys_es.name = shapeSys.GetName();
 
  397      shapeSys_es.hist = shapeSys.GetErrorHist();
 
  400      Constraint::Type systype = shapeSys.GetConstraintType();
 
  403      shapeSys_es.constraint = EstimateSummary::Gaussian;
 
  405      if( systype == Constraint::Gaussian) {
 
  406        shapeSys_es.constraint = EstimateSummary::Gaussian;
 
  408      if( systype == Constraint::Poisson ) {
 
  409        shapeSys_es.constraint = EstimateSummary::Poisson;
 
  412      sample_es.shapeSysts.push_back( shapeSys_es );
 
  416    std::cout << 
"Adding this sample" << std::endl;
 
  419    channel_estimateSummary.push_back( sample_es );
 
  423       return channel_estimateSummary;
 
  474       // mcInWs -> The ModelCofig for this likelihood
 
  475       // doConditional -> Minimize parameters for asimov quantities
 
  476       // b_only -> Make the 'background only' asimov dataset, ie mu=0 (set muVal = 0)
 
  477       // doNuisPro -> Set all nuisance parameters to '0' and to constant
 
  478       //              before minimizing. This should be done with *care*!!
 
  479       //              i.e. It should probably be removed as an option.
 
  480       // signalInjection -> If true, then do the following:
 
  481       //                    Perform the fit with m=0
 
  482       //                    After the fit, set the value to mu=muVal
 
  483       //                    so that the asimov is created with that value of mu fixed
 
  484       // doMuHat -> Set 'mu' to be float during the fit (in the range -10 to 100)
 
  485       //            Even if it floats in the fit, it is still set to
 
  486       //            'muVal' before the dataset is made (so the only difference
 
  487       //            comes from the other parameters that can float simultaneously with mu
 
  490       // double doMuHat = false 
 
  491       // double muVal = -999, 
 
  492       // bool signalInjection = false 
 
  493       // bool doNuisPro = true
 
  495       if( b_only ) muVal = 0.0;
 
  499       // If using signal injection or a non-zero mu value, 
 
  500       // add a suffix showing that value 
 
  501       std::stringstream muStr;
 
  502       if(signalInjection || !b_only) { 
 
  503    muStr << "_" << muVal;
 
  506       // Create the name of the resulting dataset
 
  507       std::string dataSetName;
 
  508       if(signalInjection) dataSetName = "signalInjection" + muStr.str();
 
  509       else dataSetName = "asimovData" + muStr.str();
 
  511       // Set the parameter of interest
 
  512       // to the 'background' value
 
  513       RooRealVar* mu = (RooRealVar*) mcInWs->GetParametersOfInterest()->first();
 
  515    std::cout << "Asimov: Setting " << mu->GetName() << " value to 0 for fit" << std::endl;
 
  519    std::cout << "Asimov: Setting " << mu->GetName() << " value to " << muVal << " for fit" << std::endl;
 
  523       // Get necessary info from the ModelConfig
 
  524       RooArgSet mc_obs = *mcInWs->GetObservables();
 
  525       RooArgSet mc_globs = *mcInWs->GetGlobalObservables();
 
  526       RooArgSet mc_nuis = *mcInWs->GetNuisanceParameters();
 
  528       // Create a set of constraint terms, which 
 
  529       // is stored in 'constraint_set'
 
  530       // Make some temporary variables and use the
 
  531       // unfoldConstrants function to do this.
 
  532       RooArgSet constraint_set;
 
  534       RooArgSet mc_nuis_tmp = mc_nuis;
 
  535       RooArgSet constraint_set_tmp(*combPdf->getAllConstraints(mc_obs, mc_nuis_tmp, false));
 
  536       unfoldConstraints(constraint_set_tmp, constraint_set, mc_obs, mc_nuis_tmp, counter_tmp);
 
  538       // Now that we have the constraint terms, we 
 
  539       // can create the full lists of nuisance parameters
 
  540       // and global variables
 
  541       RooArgList nui_list("ordered_nuis");
 
  542       RooArgList glob_list("ordered_globs");
 
  544       TIterator* cIter = constraint_set.createIterator();
 
  546       while ((arg = (RooAbsArg*)cIter->Next())) {
 
  547    RooAbsPdf* pdf = (RooAbsPdf*) arg;
 
  550    TIterator* nIter = mc_nuis.createIterator();
 
  551    RooRealVar* thisNui = NULL;
 
  553    while((nui_arg = (RooAbsArg*)nIter->Next())) {
 
  554      if(pdf->dependsOn(*nui_arg)) {
 
  555        thisNui = (RooRealVar*) nui_arg;
 
  561    // need this in case the observable isn't fundamental. 
 
  562    // in this case, see which variable is dependent on the nuisance parameter and use that.
 
  563    RooArgSet* components = pdf->getComponents();
 
  564    components->remove(*pdf);
 
  565    if(components->getSize()) {
 
  566      TIterator* itr1 = components->createIterator();
 
  568      while ((arg1 = (RooAbsArg*)itr1->Next())) {
 
  569        TIterator* itr2 = components->createIterator();
 
  571        while ((arg2 = (RooAbsArg*)itr2->Next())) {
 
  572          if(arg1 == arg2) continue;
 
  573          if(arg2->dependsOn(*arg1)) {
 
  574       components->remove(*arg1);
 
  581    if (components->getSize() > 1) {
 
  582      std::cout << "ERROR::Couldn't isolate proper nuisance parameter" << std::endl;
 
  585    else if (components->getSize() == 1) {
 
  586      thisNui = (RooRealVar*)components->first();
 
  589    TIterator* gIter = mc_globs.createIterator();
 
  590    RooRealVar* thisGlob = NULL;
 
  592    while ((glob_arg = (RooAbsArg*)gIter->Next()))
 
  594        if (pdf->dependsOn(*glob_arg))
 
  596       thisGlob = (RooRealVar*)glob_arg;
 
  602    if (!thisNui || !thisGlob)
 
  604        std::cout << "WARNING::Couldn't find nui or glob for constraint: " << pdf->GetName() << std::endl;
 
  608    if (_printLevel >= 1) std::cout << "Pairing nui: " << thisNui->GetName() << ", with glob: " << thisGlob->GetName() << ", from constraint: " << pdf->GetName() << std::endl;
 
  610    nui_list.add(*thisNui);
 
  611    glob_list.add(*thisGlob);
 
  613       } // End Loop over Constraint Terms
 
  616       //save the snapshots of nominal parameters
 
  617       combWS->saveSnapshot("nominalGlobs",glob_list);
 
  618       combWS->saveSnapshot("nominalNuis", nui_list);
 
  620       RooArgSet nuiSet_tmp(nui_list);
 
  622       // Interesting line here:
 
  624    std::cout << "Asimov: Setting mu constant in fit" << std::endl;
 
  625    mu->setConstant(true);
 
  628    std::cout << "Asimov: Letting mu float in fit (muHat)" << std::endl;
 
  629    mu->setRange(-10,100);
 
  632       // Conditional: "Minimize the parameters"
 
  635    std::cout << "Starting minimization.." << std::endl;
 
  637    // Consider removing this option:
 
  639      std::cout << "Asimov: Setting nuisance parameters constant in the fit (ARE YOU SURE YOU WANT THIS)" << std::endl;
 
  640      TIterator* nIter = nuiSet_tmp.createIterator();
 
  641      RooRealVar* thisNui = NULL;
 
  642      while((thisNui = (RooRealVar*) nIter->Next())) {
 
  644        thisNui->setConstant();
 
  647      // This should be checked, we don't want to 
 
  648      if(combWS->var("Lumi")) {
 
  649        combWS->var("Lumi")->setVal(1);
 
  650        combWS->var("Lumi")->setConstant();
 
  654    // Create the nll and its minimizer    
 
  655    RooAbsReal* nll = combPdf->createNLL(*combData, RooFit::Constrain(nuiSet_tmp));
 
  656    RooMinimizer minim(*nll);
 
  657    minim.setStrategy(2); 
 
  658    minim.setPrintLevel(999);
 
  660    // Do the minimization
 
  661    std::cout << "Minimizing to make Asimov dataset:" << std::endl;
 
  662    int status = minim.minimize(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str(), "migrad");
 
  664      // status==0 means fit was successful
 
  665    std::cout << "Successfully minimized to make Asimov dataset:" << std::endl;     
 
  666    RooFitResult* fit_result = minim.lastMinuitFit();
 
  667    std::cout << "Asimov: Final Fitted Parameters" << std::endl;
 
  668    fit_result->Print("V");
 
  670      std::cout << "Fit failed for mu = " << mu->getVal() << " with status " << status << std::endl;
 
  671      std::cout << "Trying minuit..." << std::endl;
 
  672      status = minim.minimize("Minuit", "migrad");
 
  674        cout << "Fit failed for mu = " << mu->getVal() << " with status " << status << std::endl;
 
  679    // Undo the 'doNuisPro' part
 
  680    // Again, may want to remove this
 
  682      TIterator* nIter = nuiSet_tmp.createIterator();
 
  683      RooRealVar* thisNui = NULL;
 
  684      while ((thisNui = (RooRealVar*)nIter->Next())) {
 
  685        thisNui->setConstant(false);
 
  688      if (combWS->var("Lumi")) {
 
  689        combWS->var("Lumi")->setConstant(false);
 
  693    std::cout << "Done" << std::endl;
 
  694       } // END: DoConditional
 
  696       mu->setConstant(false);
 
  698       //loop over the nui/glob list, grab the corresponding variable from the tmp ws, 
 
  699       // and set the glob to the value of the nui
 
  700       int nrNuis = nui_list.getSize();
 
  701       if (nrNuis != glob_list.getSize()) {
 
  702    std::cout << "ERROR::nui_list.getSize() != glob_list.getSize()!" << std::endl;
 
  706       for(int i=0; i<nrNuis; i++) {
 
  707    RooRealVar* nui = (RooRealVar*) nui_list.at(i);
 
  708    RooRealVar* glob = (RooRealVar*) glob_list.at(i);
 
  710    if (_printLevel >= 1) std::cout << "nui: " << nui << ", glob: " << glob << std::endl;
 
  711    if (_printLevel >= 1) std::cout << "Setting glob: " << glob->GetName() << ", which had previous val: " << glob->getVal() << ", to conditional val: " << nui->getVal() << std::endl;
 
  713    glob->setVal(nui->getVal());
 
  716       //save the snapshots of conditional parameters
 
  717       //cout << "Saving conditional snapshots" << std::endl;
 
  718       combWS->saveSnapshot(("conditionalGlobs"+muStr.str()).c_str(),glob_list);
 
  719       combWS->saveSnapshot(("conditionalNuis" +muStr.str()).c_str(), nui_list);
 
  722    combWS->loadSnapshot("nominalGlobs");
 
  723    combWS->loadSnapshot("nominalNuis");
 
  726       //cout << "Making asimov" << std::endl;
 
  727       //make the asimov data (snipped from Kyle)
 
  729       // Restore the value of mu to the target value
 
  732       ModelConfig* mc = mcInWs;
 
  736       const char* weightName = "weightVar";
 
  737       RooArgSet obsAndWeight;
 
  738       obsAndWeight.add(*mc->GetObservables());
 
  740       // Get the weightVar, or create one if necessary
 
  741       RooRealVar* weightVar = combWS->var(weightName); // NULL;
 
  742       //  if (!(weightVar = combWS->var(weightName)))
 
  743       if( weightVar==NULL ) {
 
  744    combWS->import(*(new RooRealVar(weightName, weightName, 1,0,1000)));
 
  745    weightVar = combWS->var(weightName);
 
  747       if (_printLevel >= 1) std::cout << "weightVar: " << weightVar << std::endl;
 
  748       obsAndWeight.add(*combWS->var(weightName));
 
  755       // MAKE ASIMOV DATA FOR OBSERVABLES
 
  757       // dummy var can just have one bin since it's a dummy
 
  758       if(combWS->var("ATLAS_dummyX"))  combWS->var("ATLAS_dummyX")->setBins(1);
 
  760       if (_printLevel >= 1) std::cout << " check expectedData by category" << std::endl;
 
  761       RooSimultaneous* simPdf = dynamic_cast<RooSimultaneous*>(mc->GetPdf());
 
  763       // Create the pointer to be returned
 
  764       RooDataSet* asimovData=NULL;
 
  766       // If the pdf isn't simultaneous:
 
  769    // Get pdf associated with state from simpdf
 
  770    RooAbsPdf* pdftmp = mc->GetPdf();//simPdf->getPdf(channelCat->getLabel()) ;
 
  772    // Generate observables defined by the pdf associated with this state
 
  773    RooArgSet* obstmp = pdftmp->getObservables(*mc->GetObservables()) ;
 
  775    if (_printLevel >= 1) {
 
  780    asimovData = new RooDataSet(dataSetName.c_str(), dataSetName.c_str(),
 
  781                 RooArgSet(obsAndWeight), RooFit::WeightVar(*weightVar));
 
  783    RooRealVar* thisObs = ((RooRealVar*)obstmp->first());
 
  784    double expectedEvents = pdftmp->expectedEvents(*obstmp);
 
  786    for(int jj=0; jj<thisObs->numBins(); ++jj){
 
  789      thisNorm=pdftmp->getVal(obstmp)*thisObs->getBinWidth(jj);
 
  790      if (thisNorm*expectedEvents <= 0)
 
  792          cout << "WARNING::Detected bin with zero expected events (" << thisNorm*expectedEvents << ") ! Please check your inputs. Obs = " << thisObs->GetName() << ", bin = " << jj << std::endl;
 
  794      if (thisNorm*expectedEvents > 0 && thisNorm*expectedEvents < pow(10.0, 18)) asimovData->add(*mc->GetObservables(), thisNorm*expectedEvents);
 
  797    if (_printLevel >= 1)
 
  800        std::cout <<"sum entries "<<asimovData->sumEntries()<<endl;
 
  802    if(asimovData->sumEntries()!=asimovData->sumEntries()){
 
  803      std::cout << "sum entries is nan"<<endl;
 
  807    // don't import, return (of course)
 
  808    //combWS->import(*asimovData);
 
  813      // If it IS a simultaneous pdf
 
  815      std::cout << "found a simPdf: " << simPdf << std::endl;
 
  816      map<std::string, RooDataSet*> asimovDataMap;
 
  818      RooCategory* channelCat = (RooCategory*)&simPdf->indexCat();
 
  819      TIterator* iter = channelCat->typeIterator() ;
 
  820      RooCatType* tt = NULL;
 
  822      while((tt=(RooCatType*) iter->Next())) {
 
  826      for (int i=0;i<nrIndices;i++){
 
  828        channelCat->setIndex(i);
 
  830        std::cout << "Checking channel: " << channelCat->getLabel() << std::endl;
 
  832        // Get pdf associated with state from simpdf
 
  833        RooAbsPdf* pdftmp = simPdf->getPdf(channelCat->getLabel()) ;
 
  835        // Generate observables defined by the pdf associated with this state
 
  836        RooArgSet* obstmp = pdftmp->getObservables(*mc->GetObservables()) ;
 
  838        if (_printLevel >= 1) {
 
  840          cout << "on type " << channelCat->getLabel() << " " << iFrame << std::endl;
 
  843        RooDataSet* obsDataUnbinned = new RooDataSet(Form("combAsimovData%d",iFrame),Form("combAsimovData%d",iFrame),
 
  844                       RooArgSet(obsAndWeight,*channelCat), RooFit::WeightVar(*weightVar));
 
  845        RooRealVar* thisObs = ((RooRealVar*)obstmp->first());
 
  846        double expectedEvents = pdftmp->expectedEvents(*obstmp);
 
  848        TString pdftmp_name = pdftmp->GetName();
 
  850        if (!expectedEvents) {
 
  851          std::cout << "Not expected events" << std::endl;
 
  852          if (pdftmp_name == "model_E")
 
  853       ((RooRealVar*)obstmp->first())->setVal(combWS->function("p_e")->getVal());
 
  855          else if (pdftmp_name == "model_MU")
 
  856       ((RooRealVar*)obstmp->first())->setVal(combWS->function("p_mu")->getVal());
 
  858          else if ((pdftmp_name == "model_ratio_ELMU") || (pdftmp_name == "model_comb")) {
 
  859       //((RooRealVar*)obstmp->first())->setVal(combWS->function("p_comb")->getVal());
 
  860       double p_asimov_val = combWS->var("p_asimov")->getVal();
 
  861       std::cout << "p_asimov val: " << p_asimov_val << std::endl;
 
  862       ((RooRealVar*)obstmp->first())->setVal(combWS->var("p_asimov")->getVal());
 
  866       std::cout << "Failed to set asimov data for non-extended pdf" << std::endl;
 
  869          obsDataUnbinned->add(*mc->GetObservables());
 
  873          std::cout << "expected events" << std::endl;
 
  874          for(int jj=0; jj<thisObs->numBins(); ++jj){
 
  877       thisNorm=pdftmp->getVal(obstmp)*thisObs->getBinWidth(jj);
 
  878       if (thisNorm*expectedEvents <= 0)
 
  880           std::cout << "WARNING::Detected bin with zero expected events (" << thisNorm*expectedEvents << ") ! Please check your inputs. Obs = " << thisObs->GetName() << ", bin = " << jj << std::endl;
 
  882       if (thisNorm*expectedEvents > pow(10.0, -9) && thisNorm*expectedEvents < pow(10.0, 9)) obsDataUnbinned->add(*mc->GetObservables(), thisNorm*expectedEvents);
 
  886        if (_printLevel >= 1)
 
  888       obsDataUnbinned->Print();
 
  889       std::cout <<"sum entries "<<obsDataUnbinned->sumEntries()<<endl;
 
  891        if(obsDataUnbinned->sumEntries()!=obsDataUnbinned->sumEntries()){
 
  892          cout << "sum entries is nan"<<endl;
 
  896        asimovDataMap[string(channelCat->getLabel())] = obsDataUnbinned;//tempData;
 
  898        if (_printLevel >= 1)
 
  900       std::cout << "channel: " << channelCat->getLabel() << ", data: ";
 
  901       obsDataUnbinned->Print();
 
  902       std::cout << std::endl;
 
  906      channelCat->setIndex(0);
 
  908      asimovData = new RooDataSet(dataSetName.c_str(),dataSetName.c_str(),
 
  909                   RooArgSet(obsAndWeight,*channelCat), RooFit::Index(*channelCat),
 
  910                   RooFit::Import(asimovDataMap), RooFit::WeightVar(*weightVar));
 
  912      // Don't import, return (of course)
 
  913      //combWS->import(*asimovData);
 
  914    } // End if over simultaneous pdf
 
  916       combWS->loadSnapshot("nominalNuis");
 
  917       combWS->loadSnapshot("nominalGlobs");