Ejemplo n.º 1
0
/* =============================================================================
 =============================================================================== */
bool CWorld::delRec(int iTag, int iRec)
{
	bool	bSuccess = false;

	//  layer objects
	if (iTag == ID_OBJECTS)
	{
		if (getEditLayoutValid()) 
			bSuccess = getEditLayout().delObject(iRec);
	}
	else if (iTag == ID_LINES)
	{
		if (getEditLayoutValid())
			bSuccess = getEditLayout().delLine(iRec);
	}

	//  world objects
	else if (iTag == ID_TERRAIN)
		bSuccess = getTerrain().delTerrain(iRec);
	else if (iTag == ID_TEXTURES)
		bSuccess = getTerrain().delTexture(iRec);
	else if (iTag == ID_LAYOUTS)
		bSuccess = getLayoutSet().delLayout(iRec);
	else if (iTag == ID_DATASETS)
		bSuccess = getDataSet().delDataLayer(iRec);
	else if (iTag == ID_VIEWS)
		bSuccess = getViewSet().delView(iRec);
	else if (iTag != ID_SETTINGS)
		cout << (" Invalid edit tag : " + getRecTypeName(iTag));
	return bSuccess;
}
Ejemplo n.º 2
0
/* =============================================================================
 =============================================================================== */
bool CWorld::addDataLayer(CDataLayer &DataLayer, int iNdx)
{
	if (!DataLayer.getName().size()) 
		DataLayer.setName(getFileNameBase(DataLayer.getLink()));
	if (!DataLayer.getName().size())
		DataLayer.setName("New Dataset");
	if (getDataSet().getDataLayerIndex(DataLayer.getName()) != -1)
	{
		string	str;
		for (int i = 1; getDataSet().getDataLayerIndex(str = getNewName(DataLayer.getName(), i)) != -1; i++);
		DataLayer.setName(str);
	}

	bool	bRet = getDataSet().addDataLayer(DataLayer, iNdx);
	return bRet;
}
Ejemplo n.º 3
0
DataSet DatabaseSubsystem::getDataSet(ResponsePtr response)
{
    RWLock::ScopedLock lock(_dbLock, false);

    FeatureSet features;

    Session session = getSession();
    getAvailableFeatures(session, response->labels, features);
    return getDataSet(response, features);
}
Ejemplo n.º 4
0
void KWEFile::startNewRecording(int recordingNumber, KWIKRecordingInfo* info)
{
    this->recordingNumber = recordingNumber;
    kwdIndex=0;
    String recordPath = String("/recordings/")+String(recordingNumber);
    CHECK_ERROR(createGroup(recordPath));
    CHECK_ERROR(setAttributeStr(info->name,recordPath,String("name")));
    CHECK_ERROR(setAttribute(U64,&(info->start_time),recordPath,String("start_time")));
    //	CHECK_ERROR(setAttribute(U32,&(info->start_sample),recordPath,String("start_sample")));
    CHECK_ERROR(setAttribute(F32,&(info->sample_rate),recordPath,String("sample_rate")));
    CHECK_ERROR(setAttribute(U32,&(info->bit_depth),recordPath,String("bit_depth")));
   // CHECK_ERROR(createGroup(recordPath + "/raw"));
  //  CHECK_ERROR(createGroup(recordPath + "/raw/hdf5_paths"));

    for (int i = 0; i < eventNames.size(); i++)
    {
        HDF5RecordingData* dSet;
        String path = "/event_types/" + eventNames[i] + "/events";
        dSet = getDataSet(path + "/time_samples");
        if (!dSet)
            std::cerr << "Error loading event timestamps dataset for type " << i << std::endl;
        timeStamps.add(dSet);
        dSet = getDataSet(path + "/recording");
        if (!dSet)
            std::cerr << "Error loading event recordings dataset for type " << i << std::endl;
        recordings.add(dSet);
        dSet = getDataSet(path + "/user_data/eventID");
        if (!dSet)
            std::cerr << "Error loading event ID dataset for type " << i << std::endl;
        eventID.add(dSet);
        dSet = getDataSet(path + "/user_data/nodeID");
        if (!dSet)
            std::cerr << "Error loading event node ID dataset for type " << i << std::endl;
        nodeID.add(dSet);
        dSet = getDataSet(path + "/user_data/" + eventDataNames[i]);
        if (!dSet)
            std::cerr << "Error loading event channel dataset for type " << i << std::endl;
        eventData.add(dSet);
    }
}
Ejemplo n.º 5
0
void KWXFile::startNewRecording(int recordingNumber)
{
    HDF5RecordingData* dSet;
    String path;
    this->recordingNumber = recordingNumber;

    for (int i=0; i < channelArray.size(); i++)
    {
        path = "/channel_groups/"+String(i);
        dSet=getDataSet(path+"/waveforms_filtered");
        if (!dSet)
            std::cerr << "Error loading spikes dataset for group " << i << std::endl;
        spikeArray.add(dSet);
        dSet=getDataSet(path+"/time_samples");
        if (!dSet)
            std::cerr << "Error loading spike timestamp dataset for group " << i << std::endl;
        timeStamps.add(dSet);
        dSet=getDataSet(path+"/recordings");
        if (!dSet)
            std::cerr << "Error loading spike recordings dataset for group " << i << std::endl;
        recordingArray.add(dSet);
    }
}
Ejemplo n.º 6
0
/* =============================================================================
 =============================================================================== */
bool CWorld::moveRec(int iTag, int iRec)
{
	bool	bSuccess = false;

	//  world objects
	if (iTag == ID_TERRAIN)
		bSuccess = getTerrainSet().moveTerrainLayerUp(iRec);
	else if (iTag == ID_TEXTURES)
		bSuccess = getTextureSet().moveTextureLayerUp(iRec);
	else if (iTag == ID_LAYOUTS)
		bSuccess = getLayoutSet().moveLayoutLayerUp(iRec);
	else if (iTag == ID_DATASETS)
		bSuccess = getDataSet().moveDataLayerUp(iRec);
	else if (iTag == ID_VIEWS)
		bSuccess = getViewSet().moveViewLayerUp(iRec);

	return bSuccess;
}
Ejemplo n.º 7
0
///////////////////////////////////////////////////////////
//
// Add a new transform to the chain
//
///////////////////////////////////////////////////////////
void transformsChain::addTransform(ptr<transform> pTransform)
{
	PUNTOEXE_FUNCTION_START(L"transformsChain::addTransform");

	// If the function endTransformsChain has already been
	//  called then reset the chain and restart
	///////////////////////////////////////////////////////////
	if(m_bEndTransformsChainCalled)
	{
		m_transformsList.clear();
		m_bEndTransformsChainCalled = false;
	}

	// This is the first transform. Copy the input images
	//  into it.
	///////////////////////////////////////////////////////////
	if(m_transformsList.size() == 0)
	{
		for(long copyInputImages = 0; ; ++copyInputImages)
		{
			ptr<image> inputImage = getInputImage(copyInputImages);
			if(inputImage == 0)
			{
				break;
			}
			pTransform->declareInputImage(copyInputImages, inputImage);
		}
	}

	// The transform will use the same dataset defined for
	//  the chain.
	///////////////////////////////////////////////////////////
	ptr<dataSet> pDataSet = getDataSet();
	if(pDataSet != 0)
	{
		pTransform->declareDataSet(pDataSet);
	}

	// Store the transform in the chain.
	///////////////////////////////////////////////////////////
	m_transformsList.push_back(pTransform);

	PUNTOEXE_FUNCTION_END();
}
Ejemplo n.º 8
0
/* =============================================================================
 =============================================================================== */
bool CWorld::gotoView(int ndx, float fTime, bool bUpdateCamera)
{
	if (ndx == -1) ndx = getViewSet().getCur();
	if (!getViewSet().isValid(ndx))
		return false;

	getViewSet().setCur(ndx);

	CView	&View = getView(ndx);
	applyViewActiveLayers(View.getActiveLayers());
	g_Set.deserialize(View.getSettings());

	//  after all data is loaded so we can set up the timeline
	gotoViewTime(View);
	g_Set.m_ShowTimeline = (getDataSet().getActiveCnt() > 0 || getTextureSet().getMovieActive());
	g_World.updateTerrain();
	g_Draw.drawGL();

	if (bUpdateCamera)
		g_Draw.gotoCameraView(View.getLookAt(), View.getElevation(), View.getAzimuth(), View.getDolly(), fTime);

	return true;
}
Ejemplo n.º 9
0
/* =============================================================================
 =============================================================================== */
void CWorld::gotoViewTime(CView &View)
{
	double	start = 0, finish = 0, curtime = 0;
	curtime = View.getTime();
	start = View.getStart();
	finish = View.getFinish();
	if (finish == NO_TIME) finish = start;
	if (start == NO_TIME) getDataSet().getTimeBounds(start, finish);

	double	selstart = View.getSelStart() != NO_TIME && View.getSelStart() >= start ? View.getSelStart() : start;
	double	selfinish = View.getSelFinish() != NO_TIME && View.getSelFinish() <= finish ? View.getSelFinish() : finish;
	getTimeLine().setStart(start);
	getTimeLine().setFinish(finish);
	getTimeLine().setSelStart(selstart);
	getTimeLine().setSelFinish(selfinish);
	getTimeLine().setLead(View.getLead());
	getTimeLine().setTrail(View.getTrail());
	if (curtime == NO_TIME) curtime = start;
	getTimeLine().setCurTime(curtime);

	getDataSet().setCurTime(curtime);
	getDataSet().setSelStart(selstart);
	getDataSet().setSelFinish(selfinish);
	getDataSet().setLead(View.getLead());
	getDataSet().setTrail(View.getTrail());
	getTimeLine().resetSpeed();
	//SLOW THE WORLD STARTING SPEED DOWN
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
	getTimeLine().setSlower();
}
Ejemplo n.º 10
0
					//----------
					vector<AddScan::DataPoint> AddScan::getFitPoints() const {
						Utils::ScopedProcess scopedProcess("Get fit points");

						this->throwIfMissingAConnection<Scan::Graycode>();
						auto graycodeNode = this->getInput<Scan::Graycode>();

						graycodeNode->throwIfMissingAConnection<Item::Camera>();
						auto cameraNode = graycodeNode->getInput<Item::Camera>();

						this->throwIfMissingAConnection<Item::Projector>();
						auto projectorNode = this->getInput<Item::Projector>();

						auto cameraView = cameraNode->getViewInWorldSpace();
						auto projectorView = projectorNode->getViewInWorldSpace();

						auto & scanDataSet = graycodeNode->getDataSet();
						if (!scanDataSet.getHasData()) {
							throw(ofxRulr::Exception("Scan has no data"));
						}

						//start with set of data
						typedef map<uint32_t, ofxGraycode::DataSet::const_iterator> PointSet;
						PointSet activeCameraPixels;
						{
							Utils::ScopedProcess scopedProcessActivePixels("Get all active pixels", false);
							for (ofxGraycode::DataSet::const_iterator pixelIt = scanDataSet.begin(); pixelIt != scanDataSet.end(); pixelIt.operator++()) {
								auto & pixel = pixelIt.operator->();
								if (pixel.active) {
									activeCameraPixels[pixel.camera] = pixelIt;
								}
							}
						}


						//split the data into a 4x4 grid (quad tree approach would be nicer if we have more time)
						//and separate into bins of distance (higher is best)
						const uint32_t GRID_RES = 4;

						//array (per grid square) of map<distance, vector<points>> 
						map<uint8_t, vector<ofxGraycode::DataSet::const_iterator>> pixelsByDistancePerGridSquare[GRID_RES * GRID_RES];

						{
							Utils::ScopedProcess scopedProcessActivePixels("Split scan points into grid and distance bins", false);
							uint32_t gridCellWidth = cameraNode->getWidth() / GRID_RES;
							uint32_t gridCellHeight = cameraNode->getHeight() / GRID_RES;

							for (auto & point : activeCameraPixels) {
								auto & pixel = point.second.operator->();
								auto cameraXY = pixel.getCameraXY();

								int gridIndex = ((uint32_t)cameraXY.x / gridCellWidth) + ((uint32_t)cameraXY.y / gridCellHeight) * GRID_RES;
								pixelsByDistancePerGridSquare[gridIndex][pixel.distance].push_back(point.second);
							}
						}

						
						vector<AddScan::DataPoint> dataPoints;
						size_t targetSize = (float)activeCameraPixels.size() * this->parameters.includeForFitRatio;

						//accumulate fit points and remove as we go along
						{
							Utils::ScopedProcess scopedProcessAccumulatePoints("Split scan points into grid and distance bins", false);
							size_t gridSquare = 0;
							while (dataPoints.size() < targetSize) {
								auto & pointsForThisSquare = pixelsByDistancePerGridSquare[gridSquare];

								//if no bins available, continue to next square
								//remove best bin if empty
								//if no bins available, continue to next square
								//find best point
								//remove it from bin

								{
									if (pointsForThisSquare.empty()) {
										goto continueToNextSquare;
									}
									auto bestPointsIt = pointsForThisSquare.rbegin();
									while (bestPointsIt->second.empty()) {
										pointsForThisSquare.erase(bestPointsIt->first);
										if (pointsForThisSquare.empty()) {
											goto continueToNextSquare;
										}
										bestPointsIt = pointsForThisSquare.rbegin();
									}
									auto point = bestPointsIt->second.back().operator->();
									DataPoint dataPoint{
										cameraView.pixelToCoordinate(point.getCameraXY()),
										projectorView.pixelToCoordinate(point.getProjectorXY()),
										point.median
									};
									dataPoints.push_back(dataPoint);

									bestPointsIt->second.pop_back();
								}
							continueToNextSquare:
								gridSquare++;
								gridSquare %= (GRID_RES * GRID_RES);
							}
						}
						
						scopedProcess.end();

						return dataPoints;
					}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{
    if (argc == 2 && std::string(argv[1]) == "test")
        return !test();
    if (argc < 10) {
        std::cerr << "Usage: " << argv[0] << " : "
                  << "dataset_directory rows cols hiddenNeurons outputNeurons epochs minibatchSize learningRate picsPerClass" << std::endl;
        return 1;
    }

    size_t inputLayer = atoi(argv[2]) * atoi(argv[3]);
    size_t hiddenLayer = atoi(argv[4]);
    size_t outputLayer = atoi(argv[5]);
    size_t epochs = atoi(argv[6]);
    size_t minibatchSize = atoi(argv[7]);
    double learningRate = atof(argv[8]);
    size_t maxPicsPerClass = atoi(argv[9]);

    std::cout << "==============" << std::endl;
    std::cout << "==Parameters==" << std::endl;
    std::cout << "==============" << std::endl;
    std::cout << "Number of neurons in input layer : " << atoi(argv[2]) << "x" << atoi(argv[3])
            << "=" << inputLayer << std::endl;
    std::cout << "Number of neurons in hidden layer : " << hiddenLayer << std::endl;
    std::cout << "Number of neurons in output layer : " << outputLayer << std::endl;
    std::cout << "Epochs : " << epochs << std::endl;
    std::cout << "Minibatch size : " << minibatchSize << std::endl;
    std::cout << "Learning rate : " << learningRate << std::endl;
    std::cout << "Maximum pictures per class : " << maxPicsPerClass << std::endl;


    std::cout << "===================" << std::endl;
    std::cout << "==Opening dataset==" << std::endl;
    std::cout << "===================" << std::endl;
    Network::TrainingData trainingSet = getDataSet(std::string(argv[1]), outputLayer, maxPicsPerClass);
    std::random_shuffle(trainingSet.begin(), trainingSet.end());
    Network::TrainingData testSet = slice(trainingSet, 0.9);

    std::cout << "Checking training and test inputs sanity" << std::endl;
    checkSanity(trainingSet);
    checkSanity(testSet);

    std::cout << "===========================" << std::endl;
    std::cout << "==Beginning NN processing==" << std::endl;
    std::cout << "===========================" << std::endl;

//    dumpInputs(testSet);

    /* !!!!!!!! */
    Network nn({inputLayer, hiddenLayer, outputLayer});

    nn.sgd(trainingSet, testSet, epochs, minibatchSize, learningRate);
//    nn.dump();


    //    cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
    //    cv::imshow("Display Image", image);

    //    cv::waitKey(0);

    std::cout << "isok" << std::endl;
    return 0;
}
Ejemplo n.º 12
0
DataSet DatabaseSubsystem::getDataSet(const map<int, int>& cloLabelsMap)
{
    ResponsePtr response = new Response;
    response->labels = cloLabelsMap;
    return getDataSet(response);
}
Ejemplo n.º 13
0
void makeDatacard(double mh, double massLow, double massHigh, double merrHigh, int ch, std::string cat, std::map<std::string, std::string> file, bool useModZ, bool doMassErr) {

    RooMsgService::instance().setSilentMode(kTRUE);
    RooMsgService::instance().setGlobalKillBelow(RooFit::WARNING) ;

    if (doMassErr && cat != "BB" && cat != "XX" && cat != "YY") {
        std::cout << "When using event-by-event mass errors, set cat = BB or XX or YY" << std::endl; 
        return;
    }

    /* Setting up the strings */
    std::string chstr = getChannelName(ch);

    stringstream mh_ss;
    mh_ss << mh;
    
    std::cout << "Creating datacard for " << mh_ss.str() << " GeV mass point, channel " << chstr << " and category " << cat << " ... " << std::endl;
   
    std::stringstream card_name_ss;
    card_name_ss << "card_";
    if      (!doMassErr) card_name_ss << "1D_";
    else                 card_name_ss << "2D_merr_";
    card_name_ss << "m" << mh_ss.str() << "_";
    card_name_ss << chstr << "_" << cat;
    std::string card_name = card_name_ss.str();

    std::string workspace = card_name+"_workspace.root";

    /* Higgs mass and dimuon mass variables */

    const char* massvarstr  = "CMS_hmumu_mass";
    const char* merrvarstr  = "CMS_hmumu_merr";
    const char* scalevarstr = "CMS_hmumu_scale";
    const char* resvarstr   = "CMS_hmumu_res";
    const char* mhvarstr    = "MH";

    RooRealVar rmh  ("MH"       , "MH"         , mh);
    RooRealVar m2mu (massvarstr , "Dimuon mass", mh  , massLow, massHigh, "GeV/c^{2}");
    RooRealVar merr (merrvarstr , "Mass Error" , 0.0 , 0.0    , merrHigh, ""         );
    RooRealVar scale(scalevarstr, "Scale unc. ", 0.0 , 0.0    , 1.0     , "GeV/c^{2}");
    RooRealVar res  (resvarstr  , "RFes. unc. ", 0.0 , 0.0    , 1.0);
    m2mu.setBins(200);   
    merr.setBins(200);   

    /* RooDataSet of the observed data */

    RooDataSet* data_obs = getDataSet(file["dat"].c_str(), false, ch, cat, massLow, massHigh, merrHigh, "data_obs", massvarstr, merrvarstr, false);
    
    /* Extract shape parameters */

    std::cout << "Extracting the signal fit parameters" << std::endl;
    std::map<std::string, double> sparams = doFit(file["ggH"],  true,   6, cat, 125.0, int(massHigh-massLow)*2, massLow,    140.0, false,  true, useModZ, false);
    std::cout << "Extracting the background fit parameters" << std::endl;
    std::map<std::string, double> bparams = doFit(file["dat"], false,  ch, cat, 125.0, int(massHigh-massLow) , massLow, massHigh, false, false, useModZ, true );

    std::string ecat = "XX";
    if (cat == "BB") ecat = "BB";
    if (cat == "YY") ecat = "YY";

    std::map<std::string, double> esparams;
    std::map<std::string, double> ebparams;
    if (doMassErr) {
        if (cat == "BB") {
            std::cout << "Extracting the signal mass error fit parameters" << std::endl;
            esparams = doEbEFit(file["ggH"], true, ch, ecat, 140, 0.006, 0.013, massLow, massHigh,  true,  true,  true);
            std::cout << "Extracting the background mass error fit parameters" << std::endl;
            ebparams = doEbEFit(file["DY"] , true, ch, ecat, 140, 0.006, 0.013, massLow, massHigh,  true,  true, false);
        }
        else {
            std::cout << "Extracting the signal mass error fit parameters" << std::endl;
            esparams = doEbEFit(file["ggH"], true, ch, ecat, 240, 0.005, 0.035, massLow, massHigh,  true,  true,  true);
            std::cout << "Extracting the background mass error fit parameters" << std::endl;
            ebparams = doEbEFit(file["DY"] , true, ch, ecat, 240, 0.005, 0.035, massLow, massHigh,  true,  true, false);
        }
    }

    /* Compute yields */

    double bkg_yield = computeYield(data_obs, massvarstr, false,  true);
    bkg_yield *= bparams["bkgsf"];
    std::cout << "Computing the expected background yield from the side-bands : " << bkg_yield << std::endl;

    RooDataSet* sig_gH_dset = getDataSet(file["ggH"], true, ch, cat, massLow, massHigh, merrHigh, "dset_gH", massvarstr, merrvarstr, false);
    RooDataSet* sig_qH_dset = getDataSet(file["qqH"], true, ch, cat, massLow, massHigh, merrHigh, "dset_qH", massvarstr, merrvarstr, false);
    RooDataSet* sig_PH_dset = getDataSet(file["WPH"], true, ch, cat, massLow, massHigh, merrHigh, "dset_PH", massvarstr, merrvarstr, false);
    RooDataSet* sig_MH_dset = getDataSet(file["WMH"], true, ch, cat, massLow, massHigh, merrHigh, "dset_MH", massvarstr, merrvarstr, false);
    RooDataSet* sig_ZH_dset = getDataSet(file["ZH" ], true, ch, cat, massLow, massHigh, merrHigh, "dset_ZH", massvarstr, merrvarstr, false);

    double sig_gH_yield  = computeYield(sig_gH_dset, massvarstr,  true, false);
    double sig_qH_yield  = computeYield(sig_qH_dset, massvarstr,  true, false);
    double sig_WH_yield  = computeYield(sig_PH_dset, massvarstr,  true, false);
           sig_WH_yield += computeYield(sig_MH_dset, massvarstr,  true, false);
    double sig_ZH_yield  = computeYield(sig_ZH_dset, massvarstr,  true, false);
    double sig_tH_yield  = 1e-5;

    delete sig_gH_dset;
    delete sig_qH_dset;
    delete sig_PH_dset;
    delete sig_MH_dset;
    delete sig_ZH_dset;

    std::cout << "Computing the ggH signal yield : " << sig_gH_yield << std::endl;
    std::cout << "Computing the qqH signal yield : " << sig_qH_yield << std::endl;
    std::cout << "Computing the  WH signal yield : " << sig_WH_yield << std::endl;
    std::cout << "Computing the  ZH signal yield : " << sig_ZH_yield << std::endl;
    std::cout << "Computing the ttH signal yield : " << sig_tH_yield << std::endl;

    std::string spdfstart = "";
    std::string bpdfstart = "";

    if   (doMassErr) spdfstart = "sig_mass_merr_";
    else             spdfstart = "sig_mass_";
    if   (doMassErr) bpdfstart = "bkg_mass_merr_";
    else             bpdfstart = "bkg_mass_";

    RooRealVar ggH_norm((spdfstart+"ggH_"+chstr+"_"+cat+"_pdf_norm").c_str(), "", sig_gH_yield);
    RooRealVar qqH_norm((spdfstart+"qqH_"+chstr+"_"+cat+"_pdf_norm").c_str(), "", sig_qH_yield);
    RooRealVar WH_norm ((spdfstart+"WH_" +chstr+"_"+cat+"_pdf_norm").c_str(), "", sig_WH_yield);
    RooRealVar ZH_norm ((spdfstart+"ZH_" +chstr+"_"+cat+"_pdf_norm").c_str(), "", sig_ZH_yield);
    RooRealVar ttH_norm((spdfstart+"ttH_"+chstr+"_"+cat+"_pdf_norm").c_str(), "", sig_tH_yield);
    RooRealVar bkg_norm((bpdfstart+""    +chstr+"_"+cat+"_pdf_norm").c_str(), "", bkg_yield   );

    ggH_norm.setConstant(kTRUE);
    WH_norm .setConstant(kTRUE);
    ZH_norm .setConstant(kTRUE);
    ggH_norm.setConstant(kTRUE);
    ttH_norm.setConstant(kTRUE);
    bkg_norm.setConstant(kTRUE);

    /* Define PDFs */

    // Background
    RooRealVar ra_mass(("bkg_mass_"+chstr+cat+"_a" ).c_str(), "", bparams["a"],  0.0, 1.0 );
    RooRealVar rb_mass(("bkg_mass_"+chstr+cat+"_b" ).c_str(), "", bparams["b"], -1.0, 10.0);

    RooRealVar rp_mass(("bkg_mass_"+chstr+cat+"_p" ).c_str(), "", bparams["p"], -2.0, 2.0 );
    RooRealVar rq_mass(("bkg_mass_"+chstr+cat+"_q" ).c_str(), "", bparams["q"], -2.0, 2.0 );
    RooRealVar rr_mass(("bkg_mass_"+chstr+cat+"_r" ).c_str(), "", bparams["r"], -2.0, 2.0 );

    RooAbsPdf* bkg_mass_pdf = NULL;
    if (useModZ) {
        RooModZPdf*    bkg_mass_mz_pdf = new RooModZPdf   (("bkg_mass_"+chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, rp_mass, rq_mass, rr_mass);
        bkg_mass_pdf = bkg_mass_mz_pdf;
    }
    else {
        RooZPhotonPdf* bkg_mass_zg_pdf = new RooZPhotonPdf(("bkg_mass_"+chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, ra_mass, rb_mass);
        bkg_mass_pdf = bkg_mass_zg_pdf;
    }
   
    // Signal
    std::stringstream meanss;
    std::stringstream sigmass;
    meanss  << "@0 - " << sparams["mean"]  << " + " << "@0*@1";
    if (!doMassErr) sigmass << sparams["sigma"]   << " * " << "(1+@0)";
    else            sigmass << "(@0*@1)" << " * " << "(1+@2)";

    RooArgList sigmalist;
    if (!doMassErr) sigmalist.add(res);
    else {
        sigmalist.add(merr);
        sigmalist.add(m2mu);
        sigmalist.add(res);
    }

    RooFormulaVar fmean_mass (("sig_mass_"+chstr+"_"+cat+"_fmean" ).c_str(), "", meanss .str().c_str(), RooArgList(rmh, scale));
    RooFormulaVar fsigma_mass(("sig_mass_"+chstr+"_"+cat+"_fsigma").c_str(), "", sigmass.str().c_str(), sigmalist);
    RooRealVar    raL_mass   (("sig_mass_"+chstr+"_"+cat+"_aL"    ).c_str(), "", sparams["aL"]);
    RooRealVar    rnL_mass   (("sig_mass_"+chstr+"_"+cat+"_nL"    ).c_str(), "", sparams["nL"]);
    RooRealVar    raR_mass   (("sig_mass_"+chstr+"_"+cat+"_aR"    ).c_str(), "", sparams["aR"]);
    RooRealVar    rnR_mass   (("sig_mass_"+chstr+"_"+cat+"_nR"    ).c_str(), "", sparams["nR"]);

    RooDoubleCB sig_mass_gH_pdf(("sig_mass_ggH_"+chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, fmean_mass, fsigma_mass, raL_mass, rnL_mass, raR_mass, rnR_mass);
    RooDoubleCB sig_mass_qH_pdf(("sig_mass_qqH_"+chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, fmean_mass, fsigma_mass, raL_mass, rnL_mass, raR_mass, rnR_mass);
    RooDoubleCB sig_mass_WH_pdf(("sig_mass_WH_" +chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, fmean_mass, fsigma_mass, raL_mass, rnL_mass, raR_mass, rnR_mass);
    RooDoubleCB sig_mass_ZH_pdf(("sig_mass_ZH_" +chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, fmean_mass, fsigma_mass, raL_mass, rnL_mass, raR_mass, rnR_mass);
    RooDoubleCB sig_mass_tH_pdf(("sig_mass_ttH_"+chstr+"_"+cat+"_pdf" ).c_str(), "", m2mu, fmean_mass, fsigma_mass, raL_mass, rnL_mass, raR_mass, rnR_mass);

    // Event-by-event mass error

    RooRealVar rm1_merr_sig (("sig_merr_"+chstr+"_"+cat+"_m1" ).c_str(), "", esparams["m1"] );
    RooRealVar rs1_merr_sig (("sig_merr_"+chstr+"_"+cat+"_s1" ).c_str(), "", esparams["s1"] );
    RooRealVar raL1_merr_sig(("sig_merr_"+chstr+"_"+cat+"_aL1").c_str(), "", esparams["aL1"]);
    RooRealVar rnL1_merr_sig(("sig_merr_"+chstr+"_"+cat+"_nL1").c_str(), "", esparams["nL1"]);
    RooRealVar raR1_merr_sig(("sig_merr_"+chstr+"_"+cat+"_aR1").c_str(), "", esparams["aR1"]);
    RooRealVar rnR1_merr_sig(("sig_merr_"+chstr+"_"+cat+"_nR1").c_str(), "", esparams["nR1"]);

    RooRealVar rm2_merr_sig (("sig_merr_"+chstr+"_"+cat+"_m2" ).c_str(), "", esparams["m2"] );
    RooRealVar rs2_merr_sig (("sig_merr_"+chstr+"_"+cat+"_s2" ).c_str(), "", esparams["s2"] );
    RooRealVar raL2_merr_sig(("sig_merr_"+chstr+"_"+cat+"_aL2").c_str(), "", esparams["aL2"]);
    RooRealVar rnL2_merr_sig(("sig_merr_"+chstr+"_"+cat+"_nL2").c_str(), "", esparams["nL2"]);
    RooRealVar raR2_merr_sig(("sig_merr_"+chstr+"_"+cat+"_aR2").c_str(), "", esparams["aR2"]);
    RooRealVar rnR2_merr_sig(("sig_merr_"+chstr+"_"+cat+"_nR2").c_str(), "", esparams["nR2"]);

    RooRealVar rm3_merr_sig (("sig_merr_"+chstr+"_"+cat+"_m3" ).c_str(), "", esparams["m3"] );
    RooRealVar rs3_merr_sig (("sig_merr_"+chstr+"_"+cat+"_s3" ).c_str(), "", esparams["s3"] );

    RooRealVar rc0_merr_sig (("sig_merr_"+chstr+"_"+cat+"_c0" ).c_str(), "", esparams["c0"] );
    RooRealVar rc1_merr_sig (("sig_merr_"+chstr+"_"+cat+"_c1" ).c_str(), "", esparams["c1"] );
    RooRealVar rc2_merr_sig (("sig_merr_"+chstr+"_"+cat+"_c2" ).c_str(), "", esparams["c2"] );

    RooRealVar rm1_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_m1" ).c_str(), "", ebparams["m1"] );
    RooRealVar rs1_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_s1" ).c_str(), "", ebparams["s1"] );
    RooRealVar raL1_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_aL1").c_str(), "", ebparams["aL1"]);
    RooRealVar rnL1_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_nL1").c_str(), "", ebparams["nL1"]);
    RooRealVar raR1_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_aR1").c_str(), "", ebparams["aR1"]);
    RooRealVar rnR1_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_nR1").c_str(), "", ebparams["nR1"]);

    RooRealVar rm2_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_m2" ).c_str(), "", ebparams["m2"] );
    RooRealVar rs2_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_s2" ).c_str(), "", ebparams["s2"] );
    RooRealVar raL2_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_aL2").c_str(), "", ebparams["aL2"]);
    RooRealVar rnL2_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_nL2").c_str(), "", ebparams["nL2"]);
    RooRealVar raR2_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_aR2").c_str(), "", ebparams["aR2"]);
    RooRealVar rnR2_merr_bkg(("bkg_merr_"+chstr+"_"+cat+"_nR2").c_str(), "", ebparams["nR2"]);

    RooRealVar rm3_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_m3" ).c_str(), "", ebparams["m3"] );
    RooRealVar rs3_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_s3" ).c_str(), "", ebparams["s3"] );

    RooRealVar rc0_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_c0" ).c_str(), "", ebparams["c0"] );
    RooRealVar rc1_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_c1" ).c_str(), "", ebparams["c1"] );
    RooRealVar rc2_merr_bkg (("bkg_merr_"+chstr+"_"+cat+"_c2" ).c_str(), "", ebparams["c2"] );

    RooDoubleCB   sig_merr_gH_pd1(("sig_merr_ggH_"+chstr+"_"+cat+"_pd1" ).c_str(), "", merr, rm1_merr_sig, rs1_merr_sig, raL1_merr_sig, rnL1_merr_sig, raR1_merr_sig, rnR1_merr_sig);
    RooDoubleCB   sig_merr_qH_pd1(("sig_merr_qqH_"+chstr+"_"+cat+"_pd1" ).c_str(), "", merr, rm1_merr_sig, rs1_merr_sig, raL1_merr_sig, rnL1_merr_sig, raR1_merr_sig, rnR1_merr_sig);
    RooDoubleCB   sig_merr_WH_pd1(("sig_merr_WH_" +chstr+"_"+cat+"_pd1" ).c_str(), "", merr, rm1_merr_sig, rs1_merr_sig, raL1_merr_sig, rnL1_merr_sig, raR1_merr_sig, rnR1_merr_sig);
    RooDoubleCB   sig_merr_ZH_pd1(("sig_merr_ZH_" +chstr+"_"+cat+"_pd1" ).c_str(), "", merr, rm1_merr_sig, rs1_merr_sig, raL1_merr_sig, rnL1_merr_sig, raR1_merr_sig, rnR1_merr_sig);
    RooDoubleCB   sig_merr_tH_pd1(("sig_merr_ttH_"+chstr+"_"+cat+"_pd1" ).c_str(), "", merr, rm1_merr_sig, rs1_merr_sig, raL1_merr_sig, rnL1_merr_sig, raR1_merr_sig, rnR1_merr_sig);

    RooDoubleCB   sig_merr_gH_pd2(("sig_merr_ggH_"+chstr+"_"+cat+"_pd2" ).c_str(), "", merr, rm2_merr_sig, rs2_merr_sig, raL2_merr_sig, rnL2_merr_sig, raR2_merr_sig, rnR2_merr_sig);
    RooDoubleCB   sig_merr_qH_pd2(("sig_merr_qqH_"+chstr+"_"+cat+"_pd2" ).c_str(), "", merr, rm2_merr_sig, rs2_merr_sig, raL2_merr_sig, rnL2_merr_sig, raR2_merr_sig, rnR2_merr_sig);
    RooDoubleCB   sig_merr_WH_pd2(("sig_merr_WH_" +chstr+"_"+cat+"_pd2" ).c_str(), "", merr, rm2_merr_sig, rs2_merr_sig, raL2_merr_sig, rnL2_merr_sig, raR2_merr_sig, rnR2_merr_sig);
    RooDoubleCB   sig_merr_ZH_pd2(("sig_merr_ZH_" +chstr+"_"+cat+"_pd2" ).c_str(), "", merr, rm2_merr_sig, rs2_merr_sig, raL2_merr_sig, rnL2_merr_sig, raR2_merr_sig, rnR2_merr_sig);
    RooDoubleCB   sig_merr_tH_pd2(("sig_merr_ttH_"+chstr+"_"+cat+"_pd2" ).c_str(), "", merr, rm2_merr_sig, rs2_merr_sig, raL2_merr_sig, rnL2_merr_sig, raR2_merr_sig, rnR2_merr_sig);

    RooGaussian   sig_merr_gH_pd3(("sig_merr_ggH_"+chstr+"_"+cat+"_pd3" ).c_str(), "", merr, rm3_merr_sig, rs3_merr_sig);
    RooGaussian   sig_merr_qH_pd3(("sig_merr_qqH_"+chstr+"_"+cat+"_pd3" ).c_str(), "", merr, rm3_merr_sig, rs3_merr_sig);
    RooGaussian   sig_merr_WH_pd3(("sig_merr_WH_" +chstr+"_"+cat+"_pd3" ).c_str(), "", merr, rm3_merr_sig, rs3_merr_sig);
    RooGaussian   sig_merr_ZH_pd3(("sig_merr_ZH_" +chstr+"_"+cat+"_pd3" ).c_str(), "", merr, rm3_merr_sig, rs3_merr_sig);
    RooGaussian   sig_merr_tH_pd3(("sig_merr_ttH_"+chstr+"_"+cat+"_pd3" ).c_str(), "", merr, rm3_merr_sig, rs3_merr_sig);

    RooAddPdf     sig_merr_gH_pda(("sig_merr_ggH_"+chstr+"_"+cat+"_pda" ).c_str(), "", RooArgList(sig_merr_gH_pd1, sig_merr_gH_pd2, sig_merr_gH_pd3), RooArgList(rc1_merr_sig, rc2_merr_sig));
    RooAddPdf     sig_merr_qH_pda(("sig_merr_qqH_"+chstr+"_"+cat+"_pda" ).c_str(), "", RooArgList(sig_merr_qH_pd1, sig_merr_qH_pd2, sig_merr_qH_pd3), RooArgList(rc1_merr_sig, rc2_merr_sig));
    RooAddPdf     sig_merr_WH_pda(("sig_merr_WH_" +chstr+"_"+cat+"_pda" ).c_str(), "", RooArgList(sig_merr_WH_pd1, sig_merr_WH_pd2, sig_merr_WH_pd3), RooArgList(rc1_merr_sig, rc2_merr_sig));
    RooAddPdf     sig_merr_ZH_pda(("sig_merr_ZH_" +chstr+"_"+cat+"_pda" ).c_str(), "", RooArgList(sig_merr_ZH_pd1, sig_merr_ZH_pd2, sig_merr_ZH_pd3), RooArgList(rc1_merr_sig, rc2_merr_sig));
    RooAddPdf     sig_merr_tH_pda(("sig_merr_ttH_"+chstr+"_"+cat+"_pda" ).c_str(), "", RooArgList(sig_merr_tH_pd1, sig_merr_tH_pd2, sig_merr_tH_pd3), RooArgList(rc1_merr_sig, rc2_merr_sig));

    RooAddPdf     sig_merr_gH_pdb(("sig_merr_ggH_"+chstr+"_"+cat+"_pdb" ).c_str(), "", RooArgList(                 sig_merr_gH_pd2, sig_merr_gH_pd3), RooArgList(rc0_merr_sig              ));
    RooAddPdf     sig_merr_qH_pdb(("sig_merr_qqH_"+chstr+"_"+cat+"_pdb" ).c_str(), "", RooArgList(                 sig_merr_gH_pd2, sig_merr_gH_pd3), RooArgList(rc0_merr_sig              ));
    RooAddPdf     sig_merr_WH_pdb(("sig_merr_WH_" +chstr+"_"+cat+"_pdb" ).c_str(), "", RooArgList(                 sig_merr_gH_pd2, sig_merr_gH_pd3), RooArgList(rc0_merr_sig              ));
    RooAddPdf     sig_merr_ZH_pdb(("sig_merr_ZH_" +chstr+"_"+cat+"_pdb" ).c_str(), "", RooArgList(                 sig_merr_gH_pd2, sig_merr_gH_pd3), RooArgList(rc0_merr_sig              ));
    RooAddPdf     sig_merr_tH_pdb(("sig_merr_ttH_"+chstr+"_"+cat+"_pdb" ).c_str(), "", RooArgList(                 sig_merr_gH_pd2, sig_merr_gH_pd3), RooArgList(rc0_merr_sig              ));

    RooDoubleCB   bkg_merr_pd1   (("bkg_merr_"    +chstr+"_"+cat+"_pd1" ).c_str(), "", merr, rm1_merr_bkg, rs1_merr_bkg, raL1_merr_bkg, rnL1_merr_bkg, raR1_merr_bkg, rnR1_merr_bkg);
    RooDoubleCB   bkg_merr_pd2   (("bkg_merr_"    +chstr+"_"+cat+"_pd2" ).c_str(), "", merr, rm2_merr_bkg, rs2_merr_bkg, raL2_merr_bkg, rnL2_merr_bkg, raR2_merr_bkg, rnR2_merr_bkg);
    RooGaussian   bkg_merr_pd3   (("bkg_merr_"    +chstr+"_"+cat+"_pd3" ).c_str(), "", merr, rm3_merr_bkg, rs3_merr_bkg);
    RooAddPdf     bkg_merr_pda   (("bkg_merr_"    +chstr+"_"+cat+"_pda" ).c_str(), "", RooArgList(bkg_merr_pd1, bkg_merr_pd2, bkg_merr_pd3), RooArgList(rc1_merr_bkg, rc2_merr_bkg));
    RooAddPdf     bkg_merr_pdb   (("bkg_merr_"    +chstr+"_"+cat+"_pdb" ).c_str(), "", RooArgList(              bkg_merr_pd2, bkg_merr_pd3), RooArgList(rc0_merr_bkg              ));

    RooAbsPdf* sig_merr_gH_abspdf;
    RooAbsPdf* sig_merr_qH_abspdf;
    RooAbsPdf* sig_merr_WH_abspdf;
    RooAbsPdf* sig_merr_ZH_abspdf;
    RooAbsPdf* sig_merr_tH_abspdf;
    RooAbsPdf* bkg_merr_abspdf;

    if      (cat == "BB") {
        sig_merr_gH_abspdf = &sig_merr_gH_pd1;
        sig_merr_qH_abspdf = &sig_merr_qH_pd1;
        sig_merr_WH_abspdf = &sig_merr_WH_pd1;
        sig_merr_ZH_abspdf = &sig_merr_ZH_pd1;
        sig_merr_tH_abspdf = &sig_merr_tH_pd1;
        bkg_merr_abspdf    = &bkg_merr_pd1;
    }
    else if (cat == "XX") {
        sig_merr_gH_abspdf = &sig_merr_gH_pda;
        sig_merr_qH_abspdf = &sig_merr_qH_pda;
        sig_merr_WH_abspdf = &sig_merr_WH_pda;
        sig_merr_ZH_abspdf = &sig_merr_ZH_pda;
        sig_merr_tH_abspdf = &sig_merr_tH_pda;
        bkg_merr_abspdf    = &bkg_merr_pda;
    }
    else {
        sig_merr_gH_abspdf = &sig_merr_gH_pdb;
        sig_merr_qH_abspdf = &sig_merr_qH_pdb;
        sig_merr_WH_abspdf = &sig_merr_WH_pdb;
        sig_merr_ZH_abspdf = &sig_merr_ZH_pdb;
        sig_merr_tH_abspdf = &sig_merr_tH_pdb;
        bkg_merr_abspdf    = &bkg_merr_pdb;
    }

    RooProdPdf sig_mass_merr_gH_pdf(("sig_mass_merr_ggH_"+chstr+"_"+cat+"_pdf" ).c_str(),  "", *sig_merr_gH_abspdf, RooFit::Conditional(sig_mass_gH_pdf , RooArgSet(m2mu)));
    RooProdPdf sig_mass_merr_qH_pdf(("sig_mass_merr_qqH_"+chstr+"_"+cat+"_pdf" ).c_str(),  "", *sig_merr_qH_abspdf, RooFit::Conditional(sig_mass_qH_pdf , RooArgSet(m2mu)));
    RooProdPdf sig_mass_merr_WH_pdf(("sig_mass_merr_WH_" +chstr+"_"+cat+"_pdf" ).c_str(),  "", *sig_merr_WH_abspdf, RooFit::Conditional(sig_mass_WH_pdf , RooArgSet(m2mu)));
    RooProdPdf sig_mass_merr_ZH_pdf(("sig_mass_merr_ZH_" +chstr+"_"+cat+"_pdf" ).c_str(),  "", *sig_merr_ZH_abspdf, RooFit::Conditional(sig_mass_ZH_pdf , RooArgSet(m2mu)));
    RooProdPdf sig_mass_merr_tH_pdf(("sig_mass_merr_ttH_"+chstr+"_"+cat+"_pdf" ).c_str(),  "", *sig_merr_tH_abspdf, RooFit::Conditional(sig_mass_tH_pdf , RooArgSet(m2mu)));

    RooProdPdf bkg_mass_merr_pdf   (("bkg_mass_merr_"    +chstr+"_"+cat+"_pdf" ).c_str(),  "", *bkg_mass_pdf      , *bkg_merr_abspdf);

    /* Creating the workspace the workspace */
    
    RooWorkspace w("w", "");

    w.import(*data_obs);
    w.import(ggH_norm);
    w.import(qqH_norm);
    w.import(WH_norm);
    w.import(ZH_norm);
    w.import(ttH_norm);
    w.import(bkg_norm);

    if (doMassErr) {        
    w.import(sig_mass_merr_gH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_merr_qH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_merr_WH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_merr_ZH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_merr_tH_pdf, RooFit::RecycleConflictNodes());
    w.import(bkg_mass_merr_pdf   , RooFit::RecycleConflictNodes());
    RooDataSet* data_pseudo = bkg_mass_merr_pdf.generate(RooArgSet(m2mu, merr), int(bkg_norm.getVal()));
    data_pseudo->SetName("data_pseudo");
    w.import(*data_pseudo);
    }
    else {
    w.import(sig_mass_gH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_qH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_WH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_ZH_pdf, RooFit::RecycleConflictNodes());
    w.import(sig_mass_tH_pdf, RooFit::RecycleConflictNodes());
    w.import(*bkg_mass_pdf  , RooFit::RecycleConflictNodes());
    RooDataSet* data_pseudo = bkg_mass_pdf->generate(RooArgSet(m2mu), int(bkg_norm.getVal()));
    data_pseudo->SetName("data_pseudo");
    w.import(*data_pseudo);
    }
    w.writeToFile(workspace.c_str());

    /* Create the data card text file */

    std::string card = createCardTemplate(mh, ch, cat, workspace, doMassErr);
    std::ofstream ofile;
    ofile.open ((card_name +".txt").c_str());
    ofile << card;
    ofile.close();

    if (bkg_mass_pdf != NULL) delete bkg_mass_pdf;
    delete data_obs;
}