Exemple #1
0
int main(int argc, char **argv)
{
	string scenefile;	//If aren't specified, it will render the default scene with only a cube
	string outfilename("scenes/default_output.bmp");	//NOTE: here we only support bmp output
	string outfilename_depth("scenes/default_depth_output.bmp");

    // Scene filename specified
	if (argc > 1)
	{
		scenefile = string(argv[1]);
		int dot = scenefile.find_last_of('.');
		outfilename = scenefile.substr(0, dot) + "_output.bmp";
		outfilename_depth = scenefile.substr(0, dot) + "_depth_output.bmp";
	}

    std::cout << "Rendering " << (scenefile.empty() ? "default scene" : scenefile) << std::endl;
    std::cout << "Output to " << outfilename << std::endl;
   
	Raytracer raytracer;
	if (scenefile.empty())
	{
		//render default scene
		raytracer.render(
			outfilename.c_str(),
			outfilename_depth.c_str(),
			Scene()
			);
	}
	else
	{
		// Parse the scene file
		Parser parser(new std::ifstream(scenefile.c_str()));
		if (!parser.parse()) {
			puts("Scene file can't be parsed. Use default scene.");
			raytracer.render(
				outfilename.c_str(),
				outfilename_depth.c_str(),
				Scene()
				);
		}
		else
		{
			// Render the input scene with our raytracer.
			raytracer.render(
				outfilename.c_str(),
				outfilename_depth.c_str(),
				parser.scene
				);
		}
	}
	
	// Use if you're running visual studio
	// system("pause");
	return 0;
}
Exemple #2
0
int main(int argc, char *argv[])
{
	/* first argument is the directory */
	if (argc < 4)
	{
		fprintf(stderr, "Usage:\npngcmp <image1> <image2> <outfile>\n");
		return 10;
	}
	astring imgfilename1(argv[1]);
	astring imgfilename2(argv[2]);
	astring outfilename(argv[3]);

	try {
		return generate_png_diff(imgfilename1, imgfilename2, outfilename);
	}
	catch(...)
	{
		printf("Exception occured");
		return 1000;
	}
}
Exemple #3
0
int main( int argc, char **argv ) {

    StkFloat lengthseconds = 2;
    StkFloat lofreq = 10;
    StkFloat hifreq = 22000;
    StkFloat partialsperoctave = 1500.0;
    unsigned long numpartials = 0;
    unsigned int noisetype = WHITENOISE;
    std::string outfilename( "outfile.wav" );

    // -(option parsing)--------------------------------------------------------

    int optionfound = -1;
    unsigned char ppospecified = 1;
    while( 1 ) {

        int option_index = 0; // getopt_long stores the option index here.

        optionfound = getopt_long (argc, argv, "l:s:e:p:n:wko:", long_options, &option_index);

        // end of options
        if (optionfound == -1)
            break;

        switch (optionfound) {

            case 'l':
                lengthseconds = atof( optarg );
                break;

            case 's':
                lofreq = atof( optarg );
                break;

            case 'e':
                hifreq = atof( optarg );
                break;

            case 'p':
                partialsperoctave = atof(optarg);
                ppospecified = 1;
                break;

            case 'n':
                numpartials = atol(optarg);
                ppospecified = 0;
                break;

            case 'w':
                noisetype = WHITENOISE;
                break;

            case 'k':
                noisetype = PINKNOISE;
                break;

            case 'o':
                outfilename = optarg;
                break;

            default:
                abort();

        }

    };

    unsigned long numsamples = static_cast<unsigned long>(lengthseconds * Stk::sampleRate());

    // need to do this at the end since it depends on lofreq/hifreq and can be
    // overridden by --numpartials
    if (ppospecified)
        numpartials = static_cast<unsigned long>(partialsperoctave * numoctaves( lofreq, hifreq ));

    if ( lofreq >= hifreq ) {
        std::cout << "ERROR: low freq (" << lofreq
            << ") is not lower than high freq (" << hifreq
            << ")" << std::endl;
        exit(0);
    };

    // -(main routine)----------------------------------------------------------

    // final output
    StkFrames output(0.0, numsamples, 1);

    // sine ugen
    SineWave thissine;

    std::cout << "computing " << numpartials << " partials" << std::endl;

    for ( unsigned long i = 0; i < numpartials; i++ ) {

        // progress indicator
        std::cout << "." << std::flush;

        if ( i % 20 == 0 )
            std::cout << " " << i << " " << std::flush;

        if ( noisetype == WHITENOISE ) {
            thissine.setFrequency( getlinrandfreq( lofreq, hifreq ) );
        } else { // PINKNOISE
            thissine.setFrequency( getlograndfreq( lofreq, hifreq ) );
        }

        // NB just changing the frequency in this way but using the same
        // SineWave ugen effectively randomises the starting phase.  we don't
        // want all of the partials phase aligned at the start since that leads
        // to a big amplitude spike (throwing off the normalisation step).

        for ( unsigned long j = 0; j < numsamples; j++ ) {
            // NB we're assuming here that we're not going to overflow the
            // doubles.  max I've seen is ~200.0, so we should be safe.
            output[j] += thissine.tick();
        }

    }

    std::cout << "done computing" << std::endl;

    normalize( output );

    std::cout << "done normalising" << std::endl;

    // write to file
    FileWrite outfile( outfilename, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
    outfile.write( output );
    outfile.close();

    std::cout << "wrote to " << outfilename << std::endl;

    std::cout << "all done" << std::endl;

}
Exemple #4
0
/* return 0 on success
 * extract a file in tarball to appropriate directory, ignoring any paths in archive
 * note that szDestPath lacks slash at end
 * szFName is the file to extract
 * szWantedFile is the file to extract
 * szDestPath is the path to extract to (without trailing slash) - if NULL, extracted data wont be saved to disk
 * retBuf is a pointer to a bufferpointer - will be g_try_malloc'ed and filled with data if retBuf != NULL
 * retFileSize will be filled with the size of the file, if it's != NULL
 *
 * extraction routines derived from logic in zlib's contrib untgz.c program
 */
int
UT_untgz(const char *szFName, const char *szWantedFile, const char *szDestPath, char **retBuf, int *retFileSize)
{
	gzFile tarball;
	union  tar_buffer buffer;
	int    getheader = 1;
	int    remaining = 0;
	int    len;
	char   fname[TGZ_BLOCKSIZE];
	FILE   *outfile = NULL;
	int    fileSize = 0;
	
	if (retBuf)
		FREEP(*retBuf);

	if ((tarball = gzopen(szFName, "rb")) == NULL)
	{
		UT_DEBUGMSG(("untgz: Error while opening downloaded dictionary archive"));
		return 1;
	}


	bool done = false;
	while (!done)
	{
		if ((len = gzread(tarball, &buffer, TGZ_BLOCKSIZE)) != TGZ_BLOCKSIZE)
		{
			// error (gzerror(in, &err));
			UT_DEBUGMSG(("untgz: gzread failed to read in complete block"));
			gzclose(tarball);
			return 1;
		}
		
		/*
		 * If we have to get a tar header
		 */
		if (getheader == 1)
		{
			/*
			 * if we met the end of the tar
			 * or the end-of-tar block,
			 * we are done
			 */
			if ((len == 0)  || (buffer.header.name[0]== 0)) 
			{ 
				done = true;
				continue; 
			}

			// tartime = static_cast<time_t>(getoct(buffer.header.mtime,12));
			strcpy(fname, buffer.header.name);
			strippath(fname);
	  
			if ((buffer.header.typeflag == '\0')	||	// [A]REGTYPE, ie regular files
				(buffer.header.typeflag == '0') )
			{
				remaining = getoct(buffer.header.size, 12);

				if ((remaining) && (g_ascii_strcasecmp(fname, szWantedFile) == 0))
				{
					fileSize = remaining;
					
					if (retBuf)
					{
						if (!(*retBuf = static_cast<char *>(g_try_malloc(fileSize))))
							*retBuf = NULL;
					}
					
					if (retFileSize)
						*retFileSize = fileSize;
					
					if (szDestPath) {
						UT_String outfilename(szDestPath);
						outfilename += "/";
						outfilename += fname;
						if ((outfile = fopen(outfilename.c_str(), "wb")) == NULL) {
							UT_DEBUGMSG(("untgz: Unable to save %s", outfilename.c_str()));
							}
					}
					else
						outfile = NULL;
				}
				else
					outfile = NULL;

				/*
				 * could have no contents
				 */
				getheader = (remaining) ? 0 : 1;
			}
		}
		else // if (getheader != 1)
		{
			unsigned int bytes = (remaining > TGZ_BLOCKSIZE) ? TGZ_BLOCKSIZE : remaining;
			
			if (retBuf && *retBuf)
			{
				memcpy(retBuf[fileSize - remaining], buffer.buffer, bytes);
			}
			
			if (outfile != NULL)
			{
				if (fwrite(&buffer,sizeof(char),bytes,outfile) != bytes)
				{
					UT_DEBUGMSG(("untgz: error writing, skipping %s", fname));
					fclose(outfile);
					g_unlink(fname);
				}
			}
			
			remaining -= bytes;
			if (remaining == 0)
			{
				getheader = 1;
				if (outfile != NULL)
				{
					// TODO: should actually set proper time from archive, oh well
					fclose(outfile);
					outfile = NULL;
				}
			}
		} // if (getheader == 1) else end
	}

	if (tarball != NULL) gzclose(tarball);
	return 0;
}
int evaluate( std::string filelist, std::string outfile )
{
  gStyle->SetOptStat(0);

  TCanvas *ctemp = new TCanvas();

  TCanvas *cres = new TCanvas("TimeDependence");
  TH1F* hres = new TH1F("hres","",100,0,650);
  hres->GetYaxis()->SetRangeUser(0,50);
  hres->SetTitle("");
  hres->GetXaxis()->SetTitle("time (s)");
  hres->GetYaxis()->SetTitle("B_{int} (mT)");
  hres->Draw();
  leg = new TLegend(0.2,0.6,0.9,0.9);
//  leg->SetHeader("The Legend Title"); // option "C" allows to center the header
  leg->SetNColumns(5);

  vector< double > v_Bint;
  vector< double > v_BintErr;
  vector< double > v_Bext;
  vector< double > v_BextErr;

  /* Loop over all lines in input file */
  std::ifstream infilelist(filelist);
  std::string line;

  unsigned colorcounter=38;

  while (std::getline(infilelist, line))
  {
    // skip lines with '#' and empty lines
    if ( line.find("#") != string::npos )
      {
        cout << "Skip line " << line << endl;
        continue;
      }

    if ( line == "" )
      continue;

    //cout << "Processing file " << line << endl;


    TString infilename("data_calib/");
    infilename.Append(line);

    TFile *fin = new TFile( infilename );
    TTree *tin = (TTree*)fin->Get("t");

    ctemp->cd();
    tin->Draw("Bi:time");
    TGraph *gtime = new TGraph(tin->GetEntries(), &(tin->GetV2()[0]), &(tin->GetV1()[0]));
    gtime->SetLineColor(colorcounter);
    colorcounter++;

    TH1F* hBext = new TH1F("hBext","",100,0,1000);
    tin->Draw("Bo >> hBext");

    cres->cd();
    gtime->Draw("lsame");

    double Bext_i = hBext->GetMean();
    double BextErr_i = hBext->GetRMS();

    double Bint_i = gtime->Eval(590);
    double BintErr_i = 0;

    /* add legend entry */
    TString legname("B_ext ~ ");
    legname += (int)Bext_i;
    leg->AddEntry(gtime,legname,"l");

    cout << "B_ext: " << Bext_i << " \t B_int: " << Bint_i << endl;

    v_Bint.push_back(Bint_i);
    v_BintErr.push_back(BintErr_i);
    v_Bext.push_back(Bext_i);
    v_BextErr.push_back(BextErr_i);

  }

  cres->cd();
  leg->Draw();

  TGraphErrors *gfinal = new TGraphErrors(v_Bext.size(), &(v_Bext[0]), &(v_Bint[0]), &(v_BextErr[0]), &(v_BintErr[0]));
  gfinal->Sort();
  gfinal->SetName("Bint_Vs_Bext");
  gfinal->SetTitle("");
  gfinal->GetXaxis()->SetTitle("B_{ext} (mT)");
  gfinal->GetYaxis()->SetTitle("B_{int} (mT)");

  TCanvas *cfinal = new TCanvas();
  gfinal->Draw("APL");

  /* Save output graph */
  TString outfilename("output/");
  outfilename.Append(outfile);
  TFile *fout = new TFile(outfilename,"RECREATE");

  cres->Write();
  gfinal->Write();

  fout->Close();

  /* Write result to txt output file */
  TString outfilenametxt = outfilename;
  outfilenametxt.ReplaceAll(".root",".txt");

  ofstream foutxt;
  foutxt.open( outfilenametxt );
  foutxt <<  "# Bo sig_Bo Bi sig_Bi shield sig_shield sf sig_sf time_dependent" << endl;

  for ( int i = 0; i < gfinal->GetN(); i++ )
  {
    double Bo = gfinal->GetX()[i];
    double sig_Bo = gfinal->GetEX()[i];
    double Bi = gfinal->GetY()[i];
    double sig_Bi = gfinal->GetEY()[i];
    double shield = 0;
    double sig_shield = 0;
    double sf = 0;
    double sig_sf = 0;
    double time_dependent = 0;

    foutxt <<  Bo << " " << sig_Bo << " " << Bi << " " << sig_Bi << " "
         << shield << " " << sig_shield << " " << sf << " " << sig_sf
         << " " << time_dependent << endl;
  }

  return 0;
}
/**
 * \brief Main function in astro namespace
 */
int	main(int argc, char *argv[]) {
	int	c;
	double	gamma = 1.0;
	double	minimum = -1.;
	double	maximum = -1.;
	bool	force = false;

	// parse the command line
	int	longindex;
	while (EOF != (c = getopt_long(argc, argv, "df?hm:M:g:", longopts, &longindex)))
		switch (c) {
		case 'd':
			debuglevel = LOG_DEBUG;
			break;
		case 'f':
			force = true;
			break;
		case 'g':
			gamma = std::stod(optarg);
			break;
		case 'm':
			minimum = std::stod(optarg);
			break;
		case 'M':
			maximum = std::stod(optarg);
			break;
		case '?':
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
			break;
		default:
			throw std::runtime_error("unknown option");
		}

	// two more arguments are required: infile and outfile
	if (2 != argc - optind) {
		std::string	msg("wrong number of arguments");
		debug(LOG_ERR, DEBUG_LOG, 0, "%s", msg.c_str());
		throw std::runtime_error(msg);
	}
	std::string	infilename(argv[optind++]);
	std::string	outfilename(argv[optind]);
	debug(LOG_DEBUG, DEBUG_LOG, 0, "calibrate %s to %s",
		infilename.c_str(), outfilename.c_str());

	// read the infile
	FITSin	infile(infilename);
	ImagePtr	image = infile.read();

	// convert pixels according to luminance
	ConstPixelValueAdapter<double>	from(image);

	// get the minimum and maximum values from the input image
	if (maximum < 0) {
		maximum = Max<double, double>()(from);
	}
	if (minimum < 0) {
		minimum = Min<double, double>()(from);
	}
	debug(LOG_DEBUG, DEBUG_LOG, 0, "min = %f, max = %f",
		minimum, maximum);

	// clamping filter
	ClampingAdapter<double, double>	ca(from, minimum, maximum);

	// rescaling
	double	scale = 1. / (maximum - minimum);
	RescalingAdapter<double>	ra(ca, minimum, scale);

	// gamma correction
	GammaAdapter<double>	ga(ra, gamma);

	// rescale back to the range 0-255
	RescalingAdapter<double>	ra2(ga, 0, 255.);

	// create image from last adapter
	Image<double>	*outimage = new Image<double>(ra2);
	ImagePtr	outimageptr(outimage);

	// remove previous file
	if (force) {
		unlink(outfilename.c_str());
	}

	// after all the calibrations have been performed, write the output
	// file
	FITSout	outfile(outfilename);
	outfile.write(outimageptr);

	// that's it
	return EXIT_SUCCESS;
}
void makeTemplatesWe() 
{
  gBenchmark->Start("makeTemplatesWe");

  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //==============================================================================================================   
  
  const Double_t PT_CUT  = 25;
  const Double_t ETA_CUT = 2.5;

  // input W signal file
  TString infilename("/data/blue/Bacon/Run2/wz_flat_07_23/Wenu/ntuples/we_select.root");
  
  // file name with Zll data
  TString datafname("ZeeData/fits_mva.root");
  // file name with Zl MC
  TString zllMCfname("ZeeMC/fits.root");
  // file name with Wp MC
  TString wpMCfname("WepMC/fits.root");
    // file name with Wm MC
  TString wmMCfname("WemMC/fits.root");
  
  // output file name
  TString outfilename("./testWe.root");


  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code 
  //==============================================================================================================  

  // Access recoil corrections
  //RecoilCorrector recoilCorr(datafname,zllMCfname,wpMCfname,wmMCfname);
  RecoilCorrector recoilCorr(datafname);

  //
  // Declare variables to read in ntuple
  //
  UInt_t  runNum, lumiSec, evtNum;
  UInt_t  npv, npu;
  Float_t genVPt, genVPhi;
  Float_t weight, scale1fb, puWeight;
  Float_t met, metPhi, sumEt, mt, u1, u2;
  Int_t   q;
  TLorentzVector *lep=0;
  TLorentzVector *sc=0;

  //
  // Set up output TTrees
  //
  Float_t out_met;
  TFile *outFile  = new TFile(outfilename,"RECREATE"); 
  
  TTree *rawWeTree  = new TTree("RawWe","RawWe");
  rawWeTree->Branch("weight", &weight, "weight/F");
  rawWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *rawWepTree  = new TTree("RawWep","RawWep");
  rawWepTree->Branch("weight", &weight, "weight/F");
  rawWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *rawWemTree  = new TTree("RawWem","RawWem");
  rawWemTree->Branch("weight", &weight, "weight/F");
  rawWemTree->Branch("out_met",  &out_met,  "out_met/F");

  TTree *corrWeTree = new TTree("CorrWe","corrWe");
  corrWeTree->Branch("weight", &weight, "weight/F");
  corrWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *corrWepTree = new TTree("CorrWep","corrWep");
  corrWepTree->Branch("weight", &weight, "weight/F");
  corrWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *corrWemTree = new TTree("CorrWem","corrWem");
  corrWemTree->Branch("weight", &weight, "weight/F");
  corrWemTree->Branch("out_met",  &out_met,  "out_met/F");

  TTree *corrUpWeTree = new TTree("CorrUpWe","corrUpWe");
  corrUpWeTree->Branch("weight", &weight, "weight/F");
  corrUpWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *corrUpWepTree = new TTree("CorrUpWep","corrUpWep");
  corrUpWepTree->Branch("weight", &weight, "weight/F");
  corrUpWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *corrUpWemTree = new TTree("CorrUpWem","corrUpWem");
  corrUpWemTree->Branch("weight", &weight, "weight/F");
  corrUpWemTree->Branch("out_met",  &out_met,  "out_met/F");

  TTree *corrDownWeTree = new TTree("CorrDownWe","corrDownWe");
  corrDownWeTree->Branch("weight", &weight, "weight/F");
  corrDownWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *corrDownWepTree = new TTree("CorrDownWep","corrDownWep");
  corrDownWepTree->Branch("weight", &weight, "weight/F");
  corrDownWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *corrDownWemTree = new TTree("CorrDownWem","corrDownWem");
  corrDownWemTree->Branch("weight", &weight, "weight/F");
  corrDownWemTree->Branch("out_met",  &out_met,  "out_met/F");
  
  TTree *lepScaleUpWeTree = new TTree("LepScaleUpWe","lepScaleUpWe");
  lepScaleUpWeTree->Branch("weight", &weight, "weight/F");
  lepScaleUpWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepScaleUpWepTree = new TTree("LepScaleUpWep","lepScaleUpWep");
  lepScaleUpWepTree->Branch("weight", &weight, "weight/F");
  lepScaleUpWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepScaleUpWemTree = new TTree("LepScaleUpWem","lepScaleUpWem");
  lepScaleUpWemTree->Branch("weight", &weight, "weight/F");
  lepScaleUpWemTree->Branch("out_met",  &out_met,  "out_met/F");

  TTree *lepScaleDownWeTree = new TTree("LepScaleDownWe","lepScaleDownWe");
  lepScaleDownWeTree->Branch("weight", &weight, "weight/F");
  lepScaleDownWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepScaleDownWepTree = new TTree("LepScaleDownWep","lepScaleDownWep");
  lepScaleDownWepTree->Branch("weight", &weight, "weight/F");
  lepScaleDownWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepScaleDownWemTree = new TTree("LepScaleDownWem","lepScaleDownWem");
  lepScaleDownWemTree->Branch("weight", &weight, "weight/F");
  lepScaleDownWemTree->Branch("out_met",  &out_met,  "out_met/F");

  TTree *lepResUpWeTree = new TTree("LepResUpWe","lepResUpWe");
  lepResUpWeTree->Branch("weight", &weight, "weight/F");
  lepResUpWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepResUpWepTree = new TTree("LepResUpWep","lepResUpWep");
  lepResUpWepTree->Branch("weight", &weight, "weight/F");
  lepResUpWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepResUpWemTree = new TTree("LepResUpWem","lepResUpWem");
  lepResUpWemTree->Branch("weight", &weight, "weight/F");
  lepResUpWemTree->Branch("out_met",  &out_met,  "out_met/F");

  TTree *lepResDownWeTree = new TTree("LepResDownWe","lepResDownWe");
  lepResDownWeTree->Branch("weight", &weight, "weight/F");
  lepResDownWeTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepResDownWepTree = new TTree("LepResDownWep","lepResDownWep");
  lepResDownWepTree->Branch("weight", &weight, "weight/F");
  lepResDownWepTree->Branch("out_met",  &out_met,  "out_met/F");
  TTree *lepResDownWemTree = new TTree("LepResDownWem","lepResDownWem");
  lepResDownWemTree->Branch("weight", &weight, "weight/F");
  lepResDownWemTree->Branch("out_met",  &out_met,  "out_met/F");
    
  TFile *infile=0;
  TTree *intree=0;

  // Read input file and get the TTrees
  cout << "Processing " << infilename << "..." << endl;
  infile = TFile::Open(infilename);	  assert(infile);
  intree = (TTree*)infile->Get("Events"); assert(intree);

  intree->SetBranchAddress("runNum",   &runNum);    // event run number
  intree->SetBranchAddress("lumiSec",  &lumiSec);   // event lumi section
  intree->SetBranchAddress("evtNum",   &evtNum);    // event number
  intree->SetBranchAddress("npv",      &npv);	    // number of primary vertices
  intree->SetBranchAddress("npu",      &npu);	    // number of in-time PU events (MC)
  intree->SetBranchAddress("genVPt",   &genVPt);    // GEN W boson pT (signal MC)
  intree->SetBranchAddress("genVPhi",  &genVPhi);   // GEN W boson phi (signal MC)
  intree->SetBranchAddress("scale1fb", &scale1fb);  // event weight per 1/fb (MC)
  intree->SetBranchAddress("puWeight", &puWeight);  // pileup reweighting
  intree->SetBranchAddress("mvaMet",   &met);	    // MET
  intree->SetBranchAddress("mvaMetPhi",&metPhi);    // phi(MET)
  intree->SetBranchAddress("mvaSumEt", &sumEt);     // Sum ET
  intree->SetBranchAddress("mvaMt",    &mt);	    // transverse mass
  intree->SetBranchAddress("mvaU1",    &u1);	    // parallel component of recoil
  intree->SetBranchAddress("mvaU2",    &u2);	    // perpendicular component of recoil
  intree->SetBranchAddress("q",        &q);	    // lepton charge
  intree->SetBranchAddress("lep",      &lep);	    // lepton 4-vector
  intree->SetBranchAddress("sc",       &sc);	    // electron Supercluster 4-vector
  
  //
  // loop over events
  //
  for(UInt_t ientry=0; ientry<intree->GetEntries(); ientry++) {
    intree->GetEntry(ientry);

    weight=scale1fb*puWeight;
    
    if(sc->Pt()        < PT_CUT)  continue;   
    if(fabs(sc->Eta()) > ETA_CUT) continue;

    // uncorrected MET  
    out_met = met;

    rawWeTree->Fill();
    if(q>0) rawWepTree->Fill();
    else    rawWemTree->Fill();
  	  
    Double_t corrMet=met, corrMetPhi=metPhi;
    Double_t lepPt=lep->Pt(), lepPhi=lep->Phi();
    
    // apply recoil corrections with nominal lepton scale and resolution corrections
    lepPt  = gRandom->Gaus(lep->Pt()*getEleScaleCorr(lep->Eta(),0), getEleResCorr(lep->Eta(),0)); 
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,0,0);
    out_met = corrMet;

    corrWeTree->Fill();
    if(q>0) corrWepTree->Fill();
    else    corrWemTree->Fill();
    
    // recoil corrections "up"
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,1,0);
    out_met = corrMet;

    corrUpWeTree->Fill();
    if(q>0) corrUpWepTree->Fill();
    else    corrUpWemTree->Fill();

    // recoil corrections "down"
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,-1,0);
    out_met = corrMet;

    corrDownWeTree->Fill();
    if(q>0) corrDownWepTree->Fill();
    else    corrDownWemTree->Fill();    

    // lepton scale "up"
    lepPt  = gRandom->Gaus(lep->Pt()*getEleScaleCorr(lep->Eta(),1), getEleResCorr(lep->Eta(),0));
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,0,0);
    out_met = corrMet;

    lepScaleUpWeTree->Fill();
    if(q>0) lepScaleUpWepTree->Fill();
    else    lepScaleUpWemTree->Fill();

    // lepton scale "down"
    lepPt  = gRandom->Gaus(lep->Pt()*getEleScaleCorr(lep->Eta(),-1), getEleResCorr(lep->Eta(),0));
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,0,0);
    out_met = corrMet;

    lepScaleDownWeTree->Fill();
    if(q>0) lepScaleDownWepTree->Fill();
    else    lepScaleDownWemTree->Fill();

    // lepton resolution "up"
    lepPt  = gRandom->Gaus(lep->Pt()*getEleScaleCorr(lep->Eta(),0), getEleResCorr(lep->Eta(),1));
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,0,0);
    out_met = corrMet;

    lepResUpWeTree->Fill();
    if(q>0) lepResUpWepTree->Fill();
    else    lepResUpWemTree->Fill();

    // lepton resolution "down"
    lepPt  = gRandom->Gaus(lep->Pt()*getEleScaleCorr(lep->Eta(),0), TMath::Max(getEleResCorr(lep->Eta(),-1),0.0));
    recoilCorr.Correct(corrMet,corrMetPhi,genVPt,genVPhi,lepPt,lepPhi,0,0);
    out_met = corrMet;

    lepResDownWeTree->Fill();
    if(q>0) lepResDownWepTree->Fill();
    else    lepResDownWemTree->Fill();   
    
  }   
  delete infile;
  infile=0, intree=0;   
  
    
  //--------------------------------------------------------------------------------------------------------------
  // Output
  //==============================================================================================================
   
  cout << "*" << endl;
  cout << "* SUMMARY" << endl;
  cout << "*--------------------------------------------------" << endl;  
  
  outFile->Write();
  outFile->Close();
  delete outFile;
    
  cout << endl;
  cout << "  <> Output: " << outfilename << endl;    
  cout << endl;     
  
  gBenchmark->Show("makeTemplatesWe");
}
/**
 * \brief Main function in astro namespace
 */
int	main(int argc, char *argv[]) {
	int	c;
	const char	*darkfilename = NULL;
	const char	*flatfilename = NULL;
	double	minvalue = -1;
	double	maxvalue = -1;
	bool	demosaic = false;
	bool	interpolate = false;

	// parse the command line
	while (EOF != (c = getopt(argc, argv, "dD:F:?hm:M:bi")))
		switch (c) {
		case 'd':
			debuglevel = LOG_DEBUG;
			break;
		case 'D':
			darkfilename = optarg;
			break;
		case 'F':
			flatfilename = optarg;
			break;
		case 'm':
			minvalue = atof(optarg);
			break;
		case 'M':
			maxvalue = atof(optarg);
			break;
		case 'b':
			demosaic = true;
			break;
		case 'i':
			interpolate = true;
			break;
		case '?':
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
			break;
		}

	// two more arguments are required: infile and outfile
	if (2 != argc - optind) {
		std::string	msg("wrong number of arguments");
		debug(LOG_ERR, DEBUG_LOG, 0, "%s", msg.c_str());
		throw std::runtime_error(msg);
	}
	std::string	infilename(argv[optind++]);
	std::string	outfilename(argv[optind]);
	debug(LOG_DEBUG, DEBUG_LOG, 0, "calibrate %s to %s",
		infilename.c_str(), outfilename.c_str());

	// read the infile
	FITSin	infile(infilename);
	ImagePtr	image = infile.read();

	// build the Imager
	Imager	imager;

	// if we have a dark correction, apply it
	ImagePtr	dark;
	if (NULL != darkfilename) {
		debug(LOG_DEBUG, DEBUG_LOG, 0, "dark correct: %s",
			darkfilename);
		FITSin	darkin(darkfilename);
		dark = darkin.read();
		imager.dark(dark);
		imager.darksubtract(true);
	}

	// if we have a flat file, we perform flat correction
	if (NULL != flatfilename) {
		debug(LOG_DEBUG, DEBUG_LOG, 0, "flat correction: %s",
			flatfilename);
		FITSin	flatin(flatfilename);
		ImagePtr	flat = flatin.read();
		imager.flat(flat);
		imager.flatdivide(true);
	}

	// perform bad pixel interpolation
	if (interpolate) {
		imager.interpolate(true);
	}

	// apply imager corrections
	imager(image);

	// if minvalue or maxvalue are set, clamp the image values
	if ((minvalue >= 0) || (maxvalue >= 0)) {
		if (minvalue < 0) {
			minvalue = 0;
		}
		if (maxvalue < 0) {
			maxvalue = std::numeric_limits<double>::infinity();
		}
		Clamper	clamp(minvalue, maxvalue);
		clamp(image);
	}

	// after all the calibrations have been performed, write the output
	// file
	FITSout	outfile(outfilename);

	// if demosaic is requested we do that now
	if (demosaic) {
		ImagePtr	demosaiced = demosaic_bilinear(image);
		outfile.write(demosaiced);
	} else {
		outfile.write(image);
	}

	// that's it
	return EXIT_SUCCESS;
}