Example #1
0
void NtupleChecker(){
  TString path = "/uscms_data/d2/uplegger/CMSSW/CMSSW_3_8_0_pre7/src/RecoVertex/BeamSpotProducer/test/scripts/Ntuples/";
  TSystemDirectory sourceDir("fileDir",path); 
  TList* fileList = sourceDir.GetListOfFiles(); 
  TIter next(fileList);
  TSystemFile* fileName;
  int fileNumber = 1;
  int maxFiles = 1000;
  BeamSpotTreeData aData;
  while ((fileName = (TSystemFile*)next()) && fileNumber <= maxFiles){
    if(TString(fileName->GetName()) == "." || TString(fileName->GetName()) == ".."  ){
      continue;
    }
    TTree* aTree = 0;
    TFile file(path+fileName->GetName(),"READ");//STARTUP
    cout << "Opening file: " << path+fileName->GetName() << endl;
    file.cd();
//    aTree = (TTree*)file.Get("PrimaryVertices");
    aTree = (TTree*)file.Get("BeamSpotTree");
    cout << (100*fileNumber)/(fileList->GetSize()-2) << "% of files done." << endl;
    ++fileNumber;
    if(aTree == 0){
      cout << "Can't find the tree" << endl;
      continue;
    }
    aData.setBranchAddress(aTree);
    for(unsigned int entry=0; entry<aTree->GetEntries(); entry++){
      aTree->GetEntry(entry);
      cout << aData.getRun() << endl;
    }
    
  }
}
Example #2
0
void create_file(const char *dirname, TString filename, TString histoname, TString Prefix){
  
  TSystemDirectory dir(dirname, dirname);
  TList *fileslist = dir.GetListOfFiles();
  if (fileslist) {
    TSystemFile *systemfile;
    TString fname;
    TIter next(fileslist);
    while ((systemfile=(TSystemFile*)next())) {
      fname = systemfile->GetName();
      //cout<<fname<<endl;
      if (!systemfile->IsDirectory() && fname.Contains(filename+".root")) {
	cout<<Prefix<<endl;
	TFile* file  = new TFile(dirname+fname,"READ");
	TH1F* h1 = (TH1F*)file->Get(histoname);
	//if(!fname.Contains("Bpb_TW")) h1->Scale(0.25);
	//h1->Scale(15);
	TFile* file2  = new TFile("/nfs/dust/cms/user/multh/RunII_76X_v1/Limit/Tstar_comb.root","UPDATE");
	TString histname = h1->GetName();
	h1->SetName(Prefix+"__"+filename);
	h1->Write();
	file->Close();
	file2->Close();
      }
    }
  }
}
Example #3
0
void KVSimDir::AnalyseDirectory()
{
   // Read contents of directory given to ctor.
   // Each ROOT file will be analysed by AnalyseFile().

   Info("AnalyseDirectory", "Analysing %s...", GetDirectory());
   fSimData.Clear();
   fFiltData.Clear();
   //loop over files in current directory
   TSystemDirectory thisDir(".", GetDirectory());
   TList* fileList = thisDir.GetListOfFiles();
   TIter nextFile(fileList);
   TSystemFile* aFile = 0;
   while ((aFile = (TSystemFile*)nextFile())) {

      if (aFile->IsDirectory()) continue;

      KVString fileName = aFile->GetName();
      if (!fileName.EndsWith(".root"))  continue; /* skip non-ROOT files */

      AnalyseFile(fileName);

   }
   delete fileList;
}
Example #4
0
void runTree(char* dir="hist_pAu2", char* filter="16142010", unsigned int neventsIn = 0){  
  // If nEvents is negative, reset to the maximum possible value for an Int_t
  if( neventsIn <= 0 ) neventsIn = 1<<31-1;

  TSystemDirectory DIR(dir, dir);
  TList *files = DIR.GetListOfFiles();
  TChain* trees = new TChain();
  int nfile=0;
  if (files) {
      TSystemFile *file;
      TString fname;
      TIter next(files);
      while ((file=(TSystemFile*)next())) {
	  fname = file->GetName();
	  if (!file->IsDirectory() && fname.BeginsWith(filter) && fname.EndsWith("tree.root")) {
	      cout << Form("Adding %s/%s to TChain",dir,fname.Data())<<endl;
	      trees->AddFile(Form("%s/%s/dipi0",dir,fname.Data()));
	      nfile++;
	  }
      }
  }
  cout << Form("%d files added",nfile) << endl;

  // load the shared libraries
  std::cout << "***** Loading libraries *****" << endl;
  LoadLibs();

  // Create the analysis chain
  analysisChain = new StChain("dipi0Chain");
  
  //cout << "Constructing StFmsDbMaker" << endl;
  //StFmsDbMaker* fmsDbMkr = new StFmsDbMaker( "fmsDb" );
  //fmsDbMkrName = fgtDbMkr->GetName();

  gSystem->Load("StFmsDiPi0");
  StFmsDiPi0* dipi0=new StFmsDiPi0;
  TString filenameDiPi0(Form("%s/%s.dipi0.root",dir,filter));
  cout << "DiPi0 outfile name = " << filenameDiPi0.Data()<<endl;
  dipi0->setFileName(filenameDiPi0.Data());
  dipi0->setReadTree(trees);

  analysisChain->Init();

  Int_t ierr  = kStOK;  // err flag
  Int_t nevents = 0;    // cumulative number of events in
  for( ; nevents < neventsIn && !ierr; ++nevents ){
    if(nevents%10000==0) cout <<"event: "<< nevents <<endl;
    analysisChain->Clear();
    ierr = analysisChain->Make();
  } 
  analysisChain->Finish(); 

  analysisChain->Delete();
  return;
};
Example #5
0
void runSelector(TString runNumber = "30496", TString myPath = "/sciclone/data10/jrstevens01/RunPeriod-2017-01/analysis/ver08/tree_pi0pi0pimpip__B3/merged/") 

{
  // Load DSelector library
  gROOT->ProcessLine(".x $(ROOT_ANALYSIS_HOME)/scripts/Load_DSelector.C");
  int Proof_Nthreads = 8;

  // process signal 
  TString sampleDir = myPath;
  //sampleDir += Form("0%s/", runNumber.Data());
  cout<<"running selector on files in: "<<sampleDir.Data()<<endl;
  
  TChain *chain = new TChain("pi0pi0pimpip__B3_Tree");
  TSystemDirectory dir(sampleDir, sampleDir);
  TList *files = dir.GetListOfFiles();
  int ifile = 0;
  if(files) {
	  TSystemFile *file;
	  TString fileName;
	  TIter next(files);
	  
	  // loop over files
	  while ((file=(TSystemFile*)next())) {
		  fileName = file->GetName();
		  if(fileName.Contains(runNumber)) {
			  cout<<fileName.Data()<<endl;
			  
			  // check if file corrupted
			  TFile f(sampleDir+fileName);
			  if(f.TestBit(TFile::kRecovered)) {
				  cout<<"file corrupted -> skipping"<<endl;
				  continue;
			  }
			  if(f.IsZombie()) {
				  cout<<"file is a Zombie -> skipping"<<endl;
				  continue;
			  }
			  
			  // add file to chain
			  chain->Add(sampleDir+fileName);
			  ifile++;
		  }
	  }

	  cout<<"total entries in TChain = "<<chain->GetEntries()<<" from "<<ifile<<" files"<<endl;
	  DPROOFLiteManager::Process_Chain(chain, "DSelector_pomegapi.C+", Proof_Nthreads, Form("hist_pomegapi_%s.acc.root", runNumber.Data()));
  }

  return;
}
void get_filenames(vector<TString> &names, const char *dirname=".", const char *ext=".root", const char *prefix = "uhh2")
{
  TSystemDirectory dir(dirname, dirname);
  TList *files = dir.GetListOfFiles();
  if (files) {
    TSystemFile *file;
    TString fname;
    TIter next(files);
    while ((file=(TSystemFile*)next())) {
      fname = file->GetName();
      if (!file->IsDirectory() && fname.EndsWith(ext) && fname.BeginsWith(prefix)) {
	names.push_back(TString(fname.Data()));
      }
    }
  }
}
void BrowseHistograms(const char* histname) {

    fHistName=histname;

  TSystemDirectory dir(".",".");
  TList *files = dir.GetListOfFiles();
  if (!files) {
    cerr << "Error: No files found in " << fFileDir << endl;
    return;
  }
  files->Sort();


  histograms = new TList();
  saved = new TList();

  c = new TCanvas("canvas","Browse Histograms");

  TSystemFile *file;
  TIter next(files);
  Int_t n=0;
  TString fname;
  while ((file=(TSystemFile*)next())) {
    fname = file->GetName();
    if (!file->IsDirectory() && fname.EndsWith(".root")) {

      histograms->Add(new TObjString(fname));
      n++;
      //if(n>10)
      //	break;
    }
  }

  DrawCurrHistogram();

  TControlBar *bar = new TControlBar("vertical", "Control", 10, 10);
  bar->AddButton("Next","ProcessClick(1);", "Show next run");
  bar->AddButton("Next And Save","NextAndSave();", "Show previous run");
  bar->AddButton("Next And Forget","NextAndForget();", "Show previous run");
  bar->AddButton("Prev","ProcessClick(-1);", "Show previous run");
  bar->AddButton("Print saved","PrintSaved();", "Show previous run");
  bar->SetButtonWidth(90);
  bar->Show();
}
Example #8
0
vector<TString> list_files(const char *dirname, const char *exp=".*HiForestAOD.*\\.root")
{
  vector<TString> names;
  TSystemDirectory dir(dirname, dirname);
  TList *files = dir.GetListOfFiles();
  if (files) {
    TSystemFile *file;
    TString fname;
    TIter next(files);
    while ((file=(TSystemFile*)next())) {
      fname = file->GetName();
      if (!file->IsDirectory() && fname.Contains(TRegexp(exp))) {
        names.push_back(TString(dirname)+"/"+fname);
      }
    }
  }
  if (names.size()==0) return {dirname};

  return names;
}
Example #9
0
void addfiles(TChain *ch, const TString dirname=".", const TString ext=".root")
{
	bool verbose(false);
	TSystemDirectory dir(dirname, dirname);
	TList *files = dir.GetListOfFiles();
	if (files) {
		if (verbose) std::cout << "Found files" << std::endl;
		TSystemFile *file;
		TString fname;
		TIter next(files);
		while ((file=(TSystemFile*)next())) {
			fname = file->GetName();
			if (verbose) std::cout << "found fname " << fname << std::endl;
			if (!file->IsDirectory() && fname.BeginsWith(ext)) {
				if (verbose) std::cout << "adding fname " << fname << std::endl;
				ch->Add(fname);
			}
		}
	}
}
Example #10
0
// Returns list of directorites or files in folder
vector<TString> dirlist(const TString &folder,
                        const TString &inname,
                        const TString &tag) {
    TString pwd(gSystem->pwd());
    vector<TString> v_dirs;
    TSystemDirectory dir(folder, folder);
    TList *files = dir.GetListOfFiles();
    if (files) {
        TSystemFile *file;
        TString fname;
        TIter next(files);
        while ((file=static_cast<TSystemFile*>(next()))) {
            fname = file->GetName();
            if (inname=="dir") {
                if ((file->IsDirectory() && !fname.Contains(".") && fname.Contains(tag))) v_dirs.push_back(fname);
            } else  if(fname.Contains(inname)) v_dirs.push_back(fname);
        }
    } // if(files)
    gSystem->cd(pwd); // The TSystemDirectory object seems to change current folder
    return v_dirs;
}
Example #11
0
void add_periods(const char *newname, const char *perioddir)
{
	int i, j, K, N, L;
	char str[1024];
	TList *keys;
	TFile *fIn;
	TFile *f0;
	TSystemFile *fSys;
	char prefix[128];
	char *ptr;
	TNamed *obj;
	TH1D *h;
	TH1D *hist[MAXHIST];
	
	TSystemDirectory *dir = new TSystemDirectory("MyDir", perioddir);
	TList *files = dir->GetListOfFiles();
	if (!files) {
		printf("%s - nothing to do(files)\n", perioddir);
		delete dir;
		return;
	}
	N = files->GetEntries() - 2;
	if (N <= 1) {
		printf("%s - nothing to do\n", perioddir);
		delete dir;
		return;
	}
	
	TFile *fNew = new TFile(newname, "RECREATE");
	if (!fNew->IsOpen()) {
	    delete dir;
	    delete files;
	    return;
	}
	
	fSys = (TSystemFile *) files->At(2);
	if (!fSys) {
		printf("Can not open the first file\n");
		delete dir;
		delete files;
		return;
	}
	sprintf(str, "%s/%s", perioddir, fSys->GetName());
	f0 = new TFile(str);
	if (!f0->IsOpen()) {
		printf("Can not open the first file\n");
		delete dir;
		delete files;
		return;
	}
	keys = f0->GetListOfKeys();
	K = keys->GetEntries();
	if (K <= 0) {
		printf("Nothing to do: K=0\n");
		delete dir;
		delete files;
		return;
	}
	L = 0;
	for (j=0; j<K; j++) {
		obj = (TNamed *) keys->At(j);
		if (!obj) continue;
		obj = (TNamed *) f0->Get(obj->GetName());
		if (!obj) continue;
		if (strcmp(obj->ClassName(), "TH1D")) continue;
		hist[L] = (TH1D *)obj;
		L++;
	}
	if (!L) {
		printf("Nothing to do: L=0\n");
		f0->Close();
		delete dir;
		delete files;
		return;
	}
	
	for (i=1; i<N; i++) {
		fSys = (TSystemFile *) files->At(i+2);	// skip . and ..
		if (!fSys) continue;
		sprintf(str, "%s/%s", perioddir, fSys->GetName());
		fIn = new TFile(str);
		if (!fIn->IsOpen()) continue;
		for (j=0; j<L; j++) {
			h = (TH1D *) fIn->Get(hist[j]->GetName());
			if (!h) continue;
			hist[j]->Add(h);
		}
//		printf("%s\n", fSys->GetName());
		fIn->Close();
	}

	fNew->cd();
	for (j=0; j<L; j++) hist[j]->Write();
	fNew->Close();
	
	f0->Close();
	delete files;
	delete dir;
}
int eff(){


        cout << "Starting Efficency Script" << endl;

        cout << "Usage:  eff( module_name_string , starting_hr_file_string )" << endl;
        cout << "for defaults enter \"hr\" for starting hr file designator. " << endl;

        char chpath[256];
        getcwd(chpath, 255);
        std::string path = chpath;
        std::string mod("paxxx");//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
        //if( newmod != "" )  mod = newmod;
        // <<<<<< change folder/module name to run in 
        //std::string mod("yhc691015sn3p35");
        
        std::string dataPath =  path + "/" + mod + "data";
        //std::string measurementFolder =  mod + "data";
        //std::string configPath = path + "/" + mod;
        std::string configPath = path;
        std::string HighRateSaveFileName( "Results_Hr" );
        std::string HighRateFileName( "hr" );//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
        //if( fileDesg != "" ) HighRateFileName = fileDesg;
        int namelength = HighRateFileName.length();

	// <<<<<<<<<<<<<<<<<<<  change Highrate File name to use
    	//  assumes something like hr08ma_pa225_082715.root
    	//  10 or 08 or 06 or 04 or 02 required after hr
    	//      --   looks for a root file  with "HighRateFileName" followed by 10 or 08 or ect....
    	//      --   so will parse hr10****.root and hr08**********.root ect...  with above settings
    	std::string moduleName = mod;

	std::string maskFileName("defaultMaskFile.dat");

    //phrun files here
    
    std::string phLowName("dc05_mn325_0503.root");
    std::string phHighName("dc15_mn325_0503.root");
    
    
    //
    
	const bool FIDUCIAL_ONLY = true; // don't change
	const bool VERBOSE = true;

	int nTrigPerPixel = 50; // will be read from testParameters.dat
	int nPixels = 4160;
	int nTrig = nTrigPerPixel * nPixels;
	float pixelArea = 0.01 * 0.015; // cm^2
	float triggerDuration = 25e-9; //s

	const int nRocs = 16;
	const int nDCol = 25;
	
	double worstDCol[nRocs];
	for( int i = 0; i<nRocs; i++) worstDCol[i] = -1;
	double worstDColEff[nRocs];
        for( int i = 0; i<nRocs; i++) worstDColEff[i] = 10;

	double bestDCol[nRocs];
        for( int i = 0; i<nRocs; i++) bestDCol[i] = -1;
        double bestDColEff[nRocs];
        for( int i = 0; i<nRocs; i++) bestDColEff[i] = 10;

	double lowestdceff = 10;
        int lowestdc = -1;
        int lowestroc = 25;

        double highdceff = -10;
        int highdc = -1;
        int highroc = 25;

	std::string directoryList = mod;

	std::string outFileName = dataPath + "/" + HighRateFileName + "Efficiency.log";
        std::ofstream log(outFileName.c_str());


	cout << "search for HREfficiency folders in elComandante folder structure" << endl;
    	log << "High Rate Efficency Log File Module: "<< mod << endl << endl;
	TSystemDirectory dir(dataPath.c_str(), dataPath.c_str());
    	TList *files = dir.GetListOfFiles();
    	std::vector<std::string> fileList;
    	if (files) {
      		TSystemFile *file;
      		TString fname;
      		TIter next(files);
      		while (file=(TSystemFile*)next()) {
        		fname = file->GetName();
			std::cout << fname << endl;
         		std::string filename = fname.Data();
			if (filename.substr(0,namelength) == HighRateFileName ) {
				if( filename.substr(( filename.length() - 4 ), 4)  == "root" ){
         				fileList.push_back(filename);
					std::cout << "---Added to fileList" << endl;
				}
         		}
      		}
    	}


        std::vector<double> ylist;
        ylist.push_back(0.05);
        ylist.push_back(0.10);
        ylist.push_back(0.15);
       // ylist.push_back(0.15);
       // ylist.push_back(0.15);

	std::cout << " Declaring vectors" << endl;

    std::vector< std::vector< std::pair< int,int > > > maskedPixels;
	std::vector< std::vector< double > > efficiencies;
	std::vector< std::vector< double > > efficiencyErrors;
	std::vector< std::vector< double > > rates;
	std::vector< std::vector< double > > rateErrors;
	std::vector< double > hitslow;
	std::vector< double > hitshigh;
	std::vector< double > efflow;
	std::vector< double > effhigh;
        std::vector< double > DCUni;
        std::vector< double > DCUniNum;
//      std::vector< std::vector< std::vector< double > > > byAmpEfficiencies;
//      std::vector< std::vector< std::vector< double > > > byAmpEfficiencyErrors;
//      std::vector< std::vector< std::vector< double > > > byAmpRates;
//      std::vector< std::vector< std::vector< double > > > byAmpRateErrors;
	std::vector< std::vector< std::vector< double > > > dcolHits;
	std::vector< std::vector< std::vector< double > > > dcolHitErrors;
	std::vector< std::vector< std::vector< double > > > dcolRates;
        std::vector< std::vector< std::vector< double > > > dcolRateErrors;
        std::vector< std::vector< std::vector< double > > > dcolEff;
        std::vector< std::vector< std::vector< double > > > dcolEffErrors;

	std::vector< std::vector< double > > lineList;
	std::vector<std::vector<double>> dclineList;	

	std::vector< std::vector< double > > bigempty; 
	std::vector< double > empty;

	std::cout << " Line Lists" << endl;
	for( int i=0; i <4; i++ ){
		lineList.push_back(empty);
		dclineList.push_back(empty);
	}

	for( int i = 0; i<201;i++){
		lineList[1].push_back(.98);
		lineList[0].push_back(i);
	}

	for( int i = 0; i<= 100; i++ ){
		lineList[3].push_back(i/100);
		lineList[2].push_back(120.0);
	} 

	for( int i = 0; i <= 450; i++){
		dclineList[1].push_back(1.5);
                dclineList[0].push_back(i);
		dclineList[3].push_back(0.6);
                dclineList[2].push_back(i);
	}


//	std::cout << "byamp inits" << endl;

/*	for (int i=0;i<=5;i++){
		byAmpEfficiencies.push_back(bigempty);
                byAmpEfficiencyErrors.push_back(bigempty);
                byAmpRates.push_back(bigempty);
                byAmpRateErrors.push_back(bigempty);
	}
*/
//	std::cout << "initilizing 2 tier" << endl;

/*	for( int i=0; i<=5; i++){
		for( int j=0;j<=nRocs;j++){
                        byAmpEfficiencies[i].push_back(empty);
                        byAmpEfficiencyErrors[i].push_back(empty);
                        byAmpRates[i].push_back(empty);
                        byAmpRateErrors[i].push_back(empty);
                }
	}
*/	
        //std::cout << "initilizing vectors" << endl;

	for (int i=0;i<=nRocs;i++) {
		efficiencies.push_back(empty);
		efficiencyErrors.push_back(empty);
		rates.push_back(empty);
		rateErrors.push_back(empty);
		dcolHits.push_back(bigempty);
         	dcolHitErrors.push_back(bigempty);
	        dcolRates.push_back(bigempty);
                dcolRateErrors.push_back(bigempty);
                dcolEff.push_back(bigempty);
                dcolEffErrors.push_back(bigempty);
	 }

	int dc98count[nRocs][nDCol];
	int dc95count[nRocs][nDCol];
	int totdc98 = 0;
	int totdc95 = 0;

        int dc08count[nRocs][nDCol];
        int dc12count[nRocs][nDCol];
        int totdc08 = 0;
        int totdc12 = 0;
	
	int dcbothcount[nRocs][nDCol];
	int totdcboth = 0;


        for( int i=0; i<nRocs; i++){
                for( int j=0; j<nDCol; j++){
                        dc98count[i][j] = 0;
                        dc95count[i][j] = 0;
                        dc08count[i][j] = 0;
                        dc12count[i][j] = 0;
			dcbothcount[i][j] = 0;
                }
        }

	for( int i=0; i<=nRocs; i++){
		for( int j=0; j<=nDCol; j++){
			dcolHits[i].push_back(empty);
			dcolHitErrors[i].push_back(empty);
	                dcolRates[i].push_back(empty);
	                dcolRateErrors[i].push_back(empty);
                	dcolEff[i].push_back(empty);
                	dcolEffErrors[i].push_back(empty);
		}
	}


	std::cout << "loop over all commander_HREfficiency.root root files" << endl;
	for (int i=0;i<1;++i) {
		chdir(path.c_str());
		std::cout << "looking in directory <" << directoryList << ">" << std::endl;
		std::cout << "From " << path << endl;
		std::string parmFile = directoryList + "/testParameters.dat";
		cout << "For " << parmFile << endl;
		std::ifstream testParameters(parmFile.c_str());
		std::string line2;
		cout << "Getting line" << endl;
	
		while (getline(testParameters, line2)) {
			cout << line2 << endl;
			if (line2.find("HighRate") != std::string::npos) {
				while (getline(testParameters, line2)) {
					if (line2.size() < 1) break;
					size_t pos = line2.find("Ntrig");
					std::cout << line2 << " " << pos << endl;
					if (pos != std::string::npos) {
						nTrigPerPixel = atoi(line2.substr(pos+6).c_str());
						nTrig = nTrigPerPixel * nPixels;
						std::cout << ">" << line2 << "< pos:" << pos << std::endl;
						std::cout << "number of triggers per pixel: " << nTrigPerPixel << std::endl;
					}
				}

			}
		}
		testParameters.close();


		// read masked pixels
	    	maskedPixels.clear();
		for (int j=0;j<nRocs;j++) {
			std::vector< std::pair<int,int> > rocMaskedPixels;
			maskedPixels.push_back(rocMaskedPixels);
		}
		std::ifstream maskFile;
		char maskFilePath[256];
		sprintf(maskFilePath, "%s/%s/%s", path.c_str(), directoryList.c_str(), maskFileName.c_str());
		maskFile.open(maskFilePath, std::ifstream::in);
		if (!maskFile) {
			std::cout << "ERROR: mask file <" << maskFilePath << "> can't be opened!"<<std::endl;
		}
		std::string line;
		std::vector< std::string > tokens;
		while(getline(maskFile, line)) {
			if (line[0] != '#') {
				std::stringstream ss(line); 
	    			std::string buf;
	    			tokens.clear();
				while (ss >> buf) {
					tokens.push_back(buf);
				}
				std::cout << "tok0 <" << tokens[0] << "> ";
				if (tokens[0] == "pix" && tokens.size() >= 4) {
					int roc = atoi(tokens[1].c_str());
					int col = atoi(tokens[2].c_str());
					int row = atoi(tokens[3].c_str());
					std::cout << "mask pixel " << roc << " " << col << " " << row << std::endl;
					maskedPixels[roc].push_back(std::make_pair(col, row));
				}
			}
		}
		maskFile.close();
	}
Example #13
0
TChain* CreateESDChain(const char* aDataDir = "ESDfiles.txt", Int_t aRuns = 20, Int_t offset = 0, Bool_t addFileName = kFALSE, Bool_t addFriend = kFALSE, const char* check = 0)
{
  // creates chain of files in a given directory or file containing a list.
  // In case of directory the structure is expected as:
  // <aDataDir>/<dir0>/AliESDs.root
  // <aDataDir>/<dir1>/AliESDs.root
  // ...
  //
  // if addFileName is true the list only needs to contain the directories that contain the AliESDs.root files
  // if addFriend is true a file AliESDfriends.root is expected in the same directory and added to the chain as friend
  // if check is != 0 the files that work are written back into the textfile with the name check

  if (!aDataDir)
    return 0;

  Long_t id, size, flags, modtime;
  if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
  {
    printf("%s not found.\n", aDataDir);
    return 0;
  }

  TChain* chain = new TChain("esdTree");
  TChain* chainFriend = 0;
  
  if (addFriend)
    chainFriend = new TChain("esdFriendTree");

  if (flags & 2)
  {
    TString execDir(gSystem->pwd());
    TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
    TList* dirList            = baseDir->GetListOfFiles();
    Int_t nDirs               = dirList->GetEntries();
    gSystem->cd(execDir);

    Int_t count = 0;

    for (Int_t iDir=0; iDir<nDirs; ++iDir)
    {
      TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
      if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
        continue;

      if (offset > 0)
      {
        --offset;
        continue;
      }

      if (count++ == aRuns)
        break;

      TString presentDirName(aDataDir);
      presentDirName += "/";
      presentDirName += presentDir->GetName();

      chain->Add(presentDirName + "/AliESDs.root/esdTree");
    }
  }
  else
  {
    // Open the input stream
    ifstream in;
    in.open(aDataDir);

    ofstream outfile;
    if (check)
      outfile.open(check);

    Int_t count = 0;

    // Read the input list of files and add them to the chain
    TString line;
    while (in.good())
    {
      in >> line;

      if (line.Length() == 0)
        continue;

      if (offset > 0)
      {
        offset--;
        continue;
      }

      if (count++ == aRuns)
        break;

      TString esdFile(line);

      if (addFileName)
        esdFile += "/AliESDs.root";
        
      TString esdFileFriend(esdFile);
      esdFileFriend.ReplaceAll("AliESDs.root", "AliESDfriends.root");
        
      if (check)
      {
        TFile* file = TFile::Open(esdFile);
        if (!file)
          continue;
        file->Close();
        
        if (chainFriend)
        {
          TFile* file = TFile::Open(esdFileFriend);
          if (!file)
            continue;
          file->Close();
        }
        
        outfile << line.Data() << endl;
        printf("%s\n", line.Data());
      }        
        
        // add esd file
      chain->Add(esdFile);

        // add file
      if (chainFriend)
        chainFriend->Add(esdFileFriend);
    }

    in.close();
    
    if (check)
      outfile.close();
  }
  
  if (chainFriend)
    chain->AddFriend(chainFriend);

  return chain;
}
Example #14
0
//void TMVAClassification( TString myMethodList = "" )
void tmvaClassifier( TString myMethodList = "", TString inputDir="~/work/ewkzp2j_5311/ll/", bool minimalTrain=false, bool useQG=false)
{   
  gSystem->ExpandPathName(inputDir);
  TString pf("base_weights");
  if(!minimalTrain){
    if(useQG) pf="full_weights";
    else      pf="weights";
  }
  TMVA::gConfig().GetIONames().fWeightFileDir = inputDir + pf;
  
  // The explicit loading of the shared libTMVA is done in TMVAlogon.C, defined in .rootrc
  // if you use your private .rootrc, or run from a different directory, please copy the
  // corresponding lines from .rootrc
  
  // methods to be processed can be given as an argument; use format:
  //
  // mylinux~> root -l TMVAClassification.C\(\"myMethod1,myMethod2,myMethod3\"\)
  //
  // if you like to use a method via the plugin mechanism, we recommend using
  //
  // mylinux~> root -l TMVAClassification.C\(\"P_myMethod\"\)
  // (an example is given for using the BDT as plugin (see below),
  // but of course the real application is when you write your own
  // method based)
  
  //---------------------------------------------------------------
  // This loads the library
  TMVA::Tools::Instance();
  
  // Default MVA methods to be trained + tested
  std::map<std::string,int> Use;
  
  // --- Cut optimisation
  Use["Cuts"]            = 0;
  Use["CutsD"]           = 0;
  Use["CutsPCA"]         = 0;
  Use["CutsGA"]          = 0;
  Use["CutsSA"]          = 0;
  // 
  // --- 1-dimensional likelihood ("naive Bayes estimator")
  Use["Likelihood"]      = 0;
  Use["LikelihoodD"]     = 1; // the "D" extension indicates decorrelated input variables (see option strings)
  Use["LikelihoodPCA"]   = 0; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
  Use["LikelihoodKDE"]   = 0;
  Use["LikelihoodMIX"]   = 0;
  //
  // --- Mutidimensional likelihood and Nearest-Neighbour methods
  Use["PDERS"]           = 0;
  Use["PDERSD"]          = 0;
  Use["PDERSPCA"]        = 0;
  Use["PDEFoam"]         = 0;
  Use["PDEFoamBoost"]    = 0; // uses generalised MVA method boosting
  Use["KNN"]             = 0; // k-nearest neighbour method
  //
  // --- Linear Discriminant Analysis
  Use["LD"]              = 0; // Linear Discriminant identical to Fisher
  Use["Fisher"]          = 1;
  Use["FisherCat"]       = 0;//added by loic
  Use["FisherG"]         = 0;
  Use["BoostedFisher"]   = 0; // uses generalised MVA method boosting
  Use["HMatrix"]         = 0;
  //
  // --- Function Discriminant analysis
  Use["FDA_GA"]          = 0; // minimisation of user-defined function using Genetics Algorithm
  Use["FDA_SA"]          = 0;
  Use["FDA_MC"]          = 0;
  Use["FDA_MT"]          = 0;
  Use["FDA_GAMT"]        = 0;
  Use["FDA_MCMT"]        = 0;
  //
  // --- Neural Networks (all are feed-forward Multilayer Perceptrons)
  Use["MLP"]             = 0; // Recommended ANN
  Use["MLPBFGS"]         = 0; // Recommended ANN with optional training method
  Use["MLPBNN"]          = 0; // Recommended ANN with BFGS training method and bayesian regulator
  Use["CFMlpANN"]        = 0; // Depreciated ANN from ALEPH
  Use["TMlpANN"]         = 0; // ROOT's own ANN
  //
  // --- Support Vector Machine 
  Use["SVM"]             = 0;
  // 
  // --- Boosted Decision Trees
  Use["BDT"]             = 0; // uses Adaptive Boost
  Use["BDTG"]            = 0; // uses Gradient Boost
  Use["BDTB"]            = 0; // uses Bagging
  Use["BDTD"]            = 1; // decorrelation + Adaptive Boost
  Use["BDTF"]            = 0; // allow usage of fisher discriminant for node splitting 
  // 
  // --- Friedman's RuleFit method, ie, an optimised series of cuts ("rules")
  Use["RuleFit"]         = 0;
  // ---------------------------------------------------------------

  std::cout << std::endl;
  std::cout << "==> Start TMVAClassification" << std::endl;

  // Select methods (don't look at this code - not of interest)
  if (myMethodList != "") {
    for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

    std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
    for (UInt_t i=0; i<mlist.size(); i++) {
      std::string regMethod(mlist[i]);

      if (Use.find(regMethod) == Use.end()) {
	std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
	for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
	std::cout << std::endl;
	return;
      }
      Use[regMethod] = 1;
    }
  }

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

  // --- Here the preparation phase begins

  // Create a ROOT output file where TMVA will store ntuples, histograms, etc.
  TString outfileName( "TMVA.root" );
  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );

  // Create the factory object. Later you can choose the methods
  // whose performance you'd like to investigate. The factory is 
  // the only TMVA object you have to interact with
  //
  // The first argument is the base of the name of all the
  // weightfiles in the directory weight/
  //
  // The second argument is the output file for the training results
  // All TMVA output can be suppressed by removing the "!" (not) in
  // front of the "Silent" argument in the option string
  TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile,
					      "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );

  // You can add so-called "Spectator variables", which are not used in the MVA training,
  // but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the
  // input variables, the response values of all trained MVAs, and the spectator variables
  //   factory->AddSpectator( "spec1 := var1*2",  "Spectator 1", "units", 'F' );
  // factory->AddSpectator( "spec2 := var1*3",  "Spectator 2", "units", 'F' );

  // Read training and test data
  // (it is also possible to use ASCII format as input -> see TMVA Users Guide)
  //   TString fname = "./tmva_class_example.root";
   
  //if (gSystem->AccessPathName( fname ))  // file does not exist in local directory
  //   gSystem->Exec("wget http://root.cern.ch/files/tmva_class_example.root");
   
  
  //   std::cout << "--- TMVAClassification       : Using input file: " << input->GetName() << std::endl;
   
  // --- Register the training and test trees
  TChain *signal     = new TChain("ewkzp2j");
  TChain *background = new TChain("ewkzp2j");
  TSystemDirectory dir(inputDir,inputDir);
  TList *files = dir.GetListOfFiles();
  if (files) {
    TSystemFile *file;
    TString fname;
    TIter next(files);
    while ((file=(TSystemFile*)next())) {
      fname = file->GetName();
      if(!fname.EndsWith("_summary.root")) continue;
      if(fname.Contains("Data")) continue;
      if(!fname.Contains("DY")) continue;
      bool isSignal(false);
      if(fname.Contains("JJ")) { signal->Add(fname); isSignal=true; }
      else if(fname.Contains("50toInf") && fname.Contains("DY")) background->Add(fname);
      cout << fname << " added as " << (isSignal ? "signal" : "background") << endl;
    }
  }else{
    cout << "[Error] no files found in " << inputDir << endl;
  }
  cout << "Signal has " << signal->GetEntries() << " raw events" << endl
       << "Background has " << background->GetEntries() << " raw events"<< endl;

  // global event weights per tree
  Double_t signalWeight     = 1.0;
  Double_t backgroundWeight = 1.0;
  factory->AddSignalTree    ( signal,     signalWeight     );
  factory->AddBackgroundTree( background, backgroundWeight );
  // event-per-event weights per tree
  factory->SetBackgroundWeightExpression( "weight/cnorm" );
  factory->SetSignalWeightExpression( "weight/cnorm" );

  //define variables for the training
  if(minimalTrain)
    {
      factory->AddVariable( "mjj",     "M_{jj}"              "GeV", 'F' );
      factory->AddVariable( "detajj",  "#Delta#eta_{jj}",     "",    'F' );
      factory->AddVariable( "spt",     "#Delta_{rel}",        "GeV", 'F' );
    }
  else
    {
      factory->AddVariable( "mjj",     "M_{jj}"              "GeV",  'F' );
      factory->AddVariable( "detajj",  "#Delta#eta_{jj}",     "",    'F' );
      factory->AddVariable( "setajj",  "#Sigma#eta_{j}",      "",    'F' );
      factory->AddVariable( "eta1",    "#eta(1)",             "",    'F' );
      factory->AddVariable( "eta2",    "#eta(2)",             "",    'F' );
      factory->AddVariable( "pt1",     "p_{T}(1)",            "GeV", 'F' );
      factory->AddVariable( "pt2",     "p_{T}(2)",            "GeV", 'F' );
      factory->AddVariable( "spt",     "#Delta_{rel}",        "GeV", 'F' );
      if(useQG) factory->AddVariable( "qg1",   "q/g(1)",      "",    'F' );
      if(useQG) factory->AddVariable( "qg2",   "q/g(2)",      "",    'F' );
    }
  

  // Apply additional cuts on the signal and background samples (can be different)
  TCut mycuts = ""; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
  TCut mycutb = ""; // for example: TCut mycutb = "abs(var1)<0.5";
  factory->PrepareTrainingAndTestTree( mycuts, mycutb,
				       "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );

  // ---- Book MVA methods
  //
  // Please lookup the various method configuration options in the corresponding cxx files, eg:
  // src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
  // it is possible to preset ranges in the option string in which the cut optimisation should be done:
  // "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable

  // Cut optimisation
  if (Use["Cuts"])
    factory->BookMethod( TMVA::Types::kCuts, "Cuts",
			 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );

  if (Use["CutsD"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsD",
			 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" );

  if (Use["CutsPCA"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsPCA",
			 "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=PCA" );

  if (Use["CutsGA"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsGA",
			 "H:!V:FitMethod=GA:CutRangeMin[0]=-10:CutRangeMax[0]=10:VarProp[1]=FMax:EffSel:Steps=30:Cycles=3:PopSize=400:SC_steps=10:SC_rate=5:SC_factor=0.95" );

  if (Use["CutsSA"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsSA",
			 "!H:!V:FitMethod=SA:EffSel:MaxCalls=150000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );

  // Likelihood ("naive Bayes estimator")
  if (Use["Likelihood"])
    factory->BookMethod( TMVA::Types::kLikelihood, "Likelihood",
			 "H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" );

  // Decorrelated likelihood
  if (Use["LikelihoodD"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodD",
			 "!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" );

  // PCA-transformed likelihood
  if (Use["LikelihoodPCA"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodPCA",
			 "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA" ); 

  // Use a kernel density estimator to approximate the PDFs
  if (Use["LikelihoodKDE"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodKDE",
			 "!H:!V:!TransformOutput:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" ); 

  // Use a variable-dependent mix of splines and kernel density estimator
  if (Use["LikelihoodMIX"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodMIX",
			 "!H:!V:!TransformOutput:PDFInterpolSig[0]=KDE:PDFInterpolBkg[0]=KDE:PDFInterpolSig[1]=KDE:PDFInterpolBkg[1]=KDE:PDFInterpolSig[2]=Spline2:PDFInterpolBkg[2]=Spline2:PDFInterpolSig[3]=Spline2:PDFInterpolBkg[3]=Spline2:KDEtype=Gauss:KDEiter=Nonadaptive:KDEborder=None:NAvEvtPerBin=50" ); 

  // Test the multi-dimensional probability density estimator
  // here are the options strings for the MinMax and RMS methods, respectively:
  //      "!H:!V:VolumeRangeMode=MinMax:DeltaFrac=0.2:KernelEstimator=Gauss:GaussSigma=0.3" );
  //      "!H:!V:VolumeRangeMode=RMS:DeltaFrac=3:KernelEstimator=Gauss:GaussSigma=0.3" );
  if (Use["PDERS"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERS",
			 "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" );

  if (Use["PDERSD"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERSD",
			 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=Decorrelate" );

  if (Use["PDERSPCA"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERSPCA",
			 "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=PCA" );

  // Multi-dimensional likelihood estimator using self-adapting phase-space binning
  if (Use["PDEFoam"])
    factory->BookMethod( TMVA::Types::kPDEFoam, "PDEFoam",
			 "!H:!V:SigBgSeparate=F:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" );

  if (Use["PDEFoamBoost"])
    factory->BookMethod( TMVA::Types::kPDEFoam, "PDEFoamBoost",
			 "!H:!V:Boost_Num=30:Boost_Transform=linear:SigBgSeparate=F:MaxDepth=4:UseYesNoCell=T:DTLogic=MisClassificationError:FillFoamWithOrigWeights=F:TailCut=0:nActiveCells=500:nBin=20:Nmin=400:Kernel=None:Compress=T" );

  // K-Nearest Neighbour classifier (KNN)
  if (Use["KNN"])
    factory->BookMethod( TMVA::Types::kKNN, "KNN",
			 "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" );

  // H-Matrix (chi2-squared) method
  if (Use["HMatrix"])
    factory->BookMethod( TMVA::Types::kHMatrix, "HMatrix", "!H:!V:VarTransform=None" );

  // Linear discriminant (same as Fisher discriminant)
  if (Use["LD"])
    factory->BookMethod( TMVA::Types::kLD, "LD", "H:!V:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );

  // Fisher discriminant (same as LD)
  if (Use["Fisher"])
    factory->BookMethod( TMVA::Types::kFisher, "Fisher", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
   
  if (Use["FisherCat"]){
    TMVA::MethodBase* fiCat = factory->BookMethod( TMVA::Types::kCategory, "FisherCat","" );
    TMVA::MethodCategory* mcategory = dynamic_cast<TMVA::MethodCategory*>(fiCat);
    mcategory->AddMethod( "mjj<250", "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat1", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
    mcategory->AddMethod( "mjj>=250&&mjj<350" , "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat0000", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
    mcategory->AddMethod( "mjj>=350&&mjj<450" , "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat0350", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
    mcategory->AddMethod( "mjj>=450&&mjj<550" , "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat0450", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
    mcategory->AddMethod( "mjj>=550&&mjj<750" , "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat0550", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
    mcategory->AddMethod( "mjj>=750&&mjj<1000", "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat0750", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
    mcategory->AddMethod( "mjj>=1000"         , "mjj:detajj:spt:", TMVA::Types::kFisher, "Fisher_Cat1000", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
  }


  // Fisher with Gauss-transformed input variables
  if (Use["FisherG"])
    factory->BookMethod( TMVA::Types::kFisher, "FisherG", "H:!V:VarTransform=Gauss" );

  // Composite classifier: ensemble (tree) of boosted Fisher classifiers
  if (Use["BoostedFisher"])
    factory->BookMethod( TMVA::Types::kFisher, "BoostedFisher", 
			 "H:!V:Boost_Num=20:Boost_Transform=log:Boost_Type=AdaBoost:Boost_AdaBoostBeta=0.2:!Boost_DetailedMonitoring" );

  // Function discrimination analysis (FDA) -- test of various fitters - the recommended one is Minuit (or GA or SA)
  if (Use["FDA_MC"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_MC",
			 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:SampleSize=100000:Sigma=0.1" );

  if (Use["FDA_GA"]) // can also use Simulated Annealing (SA) algorithm (see Cuts_SA options])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_GA",
			 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=300:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" );

  if (Use["FDA_SA"]) // can also use Simulated Annealing (SA) algorithm (see Cuts_SA options])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_SA",
			 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=SA:MaxCalls=15000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );

  if (Use["FDA_MT"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_MT",
			 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=2:UseImprove:UseMinos:SetBatch" );

  if (Use["FDA_GAMT"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_GAMT",
			 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:Cycles=1:PopSize=5:Steps=5:Trim" );

  if (Use["FDA_MCMT"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_MCMT",
			 "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:SampleSize=20" );

  // TMVA ANN: MLP (recommended ANN) -- all ANNs in TMVA are Multilayer Perceptrons
  if (Use["MLP"])
    factory->BookMethod( TMVA::Types::kMLP, "MLP", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:!UseRegulator" );

  if (Use["MLPBFGS"])
    factory->BookMethod( TMVA::Types::kMLP, "MLPBFGS", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" );

  if (Use["MLPBNN"])
    factory->BookMethod( TMVA::Types::kMLP, "MLPBNN", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" ); // BFGS training with bayesian regulators

  // CF(Clermont-Ferrand)ANN
  if (Use["CFMlpANN"])
    factory->BookMethod( TMVA::Types::kCFMlpANN, "CFMlpANN", "!H:!V:NCycles=2000:HiddenLayers=N+1,N"  ); // n_cycles:#nodes:#nodes:...  

  // Tmlp(Root)ANN
  if (Use["TMlpANN"])
    factory->BookMethod( TMVA::Types::kTMlpANN, "TMlpANN", "!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3"  ); // n_cycles:#nodes:#nodes:...

  // Support Vector Machine
  if (Use["SVM"])
    factory->BookMethod( TMVA::Types::kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm" );

  // Boosted Decision Trees
  if (Use["BDTG"]) // Gradient Boost
    factory->BookMethod( TMVA::Types::kBDT, "BDTG",
			 "!H:!V:NTrees=1000:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.10:UseBaggedGrad:GradBaggingFraction=0.5:nCuts=20:MaxDepth=2" );

  if (Use["BDT"])  // Adaptive Boost
    factory->BookMethod( TMVA::Types::kBDT, "BDT",
			 "!H:!V:NTrees=850:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20" );

  if (Use["BDTB"]) // Bagging
    factory->BookMethod( TMVA::Types::kBDT, "BDTB",
			 "!H:!V:NTrees=400:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20" );

  if (Use["BDTD"]) // Decorrelation + Adaptive Boost
    factory->BookMethod( TMVA::Types::kBDT, "BDTD",
			 "!H:!V:NTrees=400:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=25:PruneMethod=CostComplexity:PruneStrength=25.0:VarTransform=Decorrelate");
  //"!H:!V:NTrees=400:MinNodeSize=5%:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:VarTransform=Decorrelate" );

  if (Use["BDTF"])  // Allow Using Fisher discriminant in node splitting for (strong) linearly correlated variables
    factory->BookMethod( TMVA::Types::kBDT, "BDTMitFisher",
			 "!H:!V:NTrees=50:MinNodeSize=2.5%:UseFisherCuts:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20" );

  // RuleFit -- TMVA implementation of Friedman's method
  if (Use["RuleFit"])
    factory->BookMethod( TMVA::Types::kRuleFit, "RuleFit",
			 "H:!V:RuleFitModule=RFTMVA:Model=ModRuleLinear:MinImp=0.001:RuleMinDist=0.001:NTrees=20:fEventsMin=0.01:fEventsMax=0.5:GDTau=-1.0:GDTauPrec=0.01:GDStep=0.01:GDNSteps=10000:GDErrScale=1.02" );

  // For an example of the category classifier usage, see: TMVAClassificationCategory




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

  // ---- Now you can optimize the setting (configuration) of the MVAs using the set of training events

  // factory->OptimizeAllMethods("SigEffAt001","Scan");
  // factory->OptimizeAllMethods("ROCIntegral","FitGA");

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

  // ---- Now you can tell the factory to train, test, and evaluate the MVAs

  // Train MVAs using the set of training events
  factory->TrainAllMethods();

  // ---- Evaluate all MVAs using the set of test events
  factory->TestAllMethods();

  // ----- Evaluate and compare performance of all configured MVAs
  factory->EvaluateAllMethods();

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

  // Save the output
  outputFile->Close();

  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
  std::cout << "==> TMVAClassification is done!" << std::endl;
  std::cout << " ==> Weights are stored in " << TMVA::gConfig().GetIONames().fWeightFileDir << std::endl;
  delete factory;



  // Launch the GUI for the root macros
  //   if (!gROOT->IsBatch()) TMVAGui( outfileName );
}
Example #15
0
void TMVAClassification( TString myMethodList = "" ) 
{
  // The explicit loading of the shared libTMVA is done in TMVAlogon.C, defined in .rootrc
  // if you use your private .rootrc, or run from a different directory, please copy the 
  // corresponding lines from .rootrc

  // methods to be processed can be given as an argument; use format:
  //
  // mylinux~> root -l TMVAClassification.C\(\"myMethod1,myMethod2,myMethod3\"\)
  //
  // if you like to use a method via the plugin mechanism, we recommend using
  // 
  // mylinux~> root -l TMVAClassification.C\(\"P_myMethod\"\)
  // (an example is given for using the BDT as plugin (see below),
  // but of course the real application is when you write your own
  // method based)

  // this loads the library
  TMVA::Tools::Instance();

  //---------------------------------------------------------------
  // default MVA methods to be trained + tested
  std::map<std::string,int> Use;

  Use["Cuts"]            = 1;
  Use["CutsD"]           = 1;
  Use["CutsPCA"]         = 1;
  Use["CutsGA"]          = 1;
  Use["CutsSA"]          = 1;
  // ---
  Use["Likelihood"]      = 1;
  Use["LikelihoodD"]     = 1; // the "D" extension indicates decorrelated input variables (see option strings)
  Use["LikelihoodPCA"]   = 1; // the "PCA" extension indicates PCA-transformed input variables (see option strings)
  Use["LikelihoodKDE"]   = 1;
  Use["LikelihoodMIX"]   = 1;
  // ---
  Use["PDERS"]           = 1;
  Use["PDERSD"]          = 1;
  Use["PDERSPCA"]        = 1;
  Use["PDERSkNN"]        = 1; // depreciated until further notice
  Use["PDEFoam"]         = 1;
  // --
  Use["KNN"]             = 1;
  // ---
  Use["HMatrix"]         = 1;
  Use["Fisher"]          = 1;
  Use["FisherG"]         = 1;
  Use["BoostedFisher"]   = 1;
  Use["LD"]              = 1;
  // ---
  Use["FDA_GA"]          = 1;
  Use["FDA_SA"]          = 1;
  Use["FDA_MC"]          = 1;
  Use["FDA_MT"]          = 1;
  Use["FDA_GAMT"]        = 1;
  Use["FDA_MCMT"]        = 1;
  // ---
  Use["MLP"]             = 1; // this is the recommended ANN
  Use["MLPBFGS"]         = 1; // recommended ANN with optional training method
  Use["CFMlpANN"]        = 1; // *** missing
  Use["TMlpANN"]         = 1; 
  // ---
  Use["SVM"]             = 1;
  // ---
  Use["BDT"]             = 1;
  Use["BDTD"]            = 0;
  Use["BDTG"]            = 1;
  Use["BDTB"]            = 0;
  // ---
  Use["RuleFit"]         = 1;
  // ---
  Use["Plugin"]          = 0;
  // ---------------------------------------------------------------

  std::cout << std::endl;
  std::cout << "==> Start TMVAClassification" << std::endl;

  if (myMethodList != "") {
    for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;

    std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
    for (UInt_t i=0; i<mlist.size(); i++) {
      std::string regMethod(mlist[i]);

      if (Use.find(regMethod) == Use.end()) {
        std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
        for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
        std::cout << std::endl;
        return;
      }
      Use[regMethod] = 1;
    }
  }

  // Create a new root output file.
  TString outfileName = "TMVA";
  int toAppNum = 1;
  char name[1024];

  TSystemDirectory dir("",".");
  TList *files = dir.GetListOfFiles();
  vector<string> vfname;
  if (files) {
     TIter next(files);
     TSystemFile *file;
     TString fname;
     
     while ((file=(TSystemFile*)next())) {
        fname = file->GetName();
        if (!file->IsDirectory() && fname.EndsWith(".root") && fname.BeginsWith("TMVA")) {
           vfname.push_back(string(fname));
        }
      }
     delete files;
     if (vfname.size()>0) {
        std::sort(vfname.begin(),vfname.end());
        TString num = TString(vfname[vfname.size()-1]);
        num.ReplaceAll(".root","");
        num.ReplaceAll("TMVA","");
        toAppNum = num.Atoi()+1;
     }
  }
  sprintf(name,"%d",toAppNum);
  outfileName = outfileName + name + ".root";
  //TString outfileName( "TMVA.root" );
  TFile* outputFile = TFile::Open( outfileName, "RECREATE" );

  // Create the factory object. Later you can choose the methods
  // whose performance you'd like to investigate. The factory will
  // then run the performance analysis for you.
  //
  // The first argument is the base of the name of all the
  // weightfiles in the directory weight/ 
  //
  // The second argument is the output file for the training results
  // All TMVA output can be suppressed by removing the "!" (not) in 
  // front of the "Silent" argument in the option string
  TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile, 
                                              "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D" );

  // If you wish to modify default settings 
  // (please check "src/Config.h" to see all available global options)
  //    (TMVA::gConfig().GetVariablePlotting()).fTimesRMS = 8.0;
  //    (TMVA::gConfig().GetIONames()).fWeightFileDir = "myWeightDirectory";


  // Define the input variables that shall be used for the MVA training
  factory->AddVariable("tEventProbSig := tEventProb[0]+tEventProb[1]+tEventProb[2]","F",0,0);
  factory->AddVariable("tEventProbBkg := tEventProb","F",0,0);
  
  // You can add so-called "Spectator variables", which are not used in the MVA training, 
  // but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the 
  // input variables, the response values of all trained MVAs, and the spectator variables
  factory->AddSpectator("run := m_run", "I");
  factory->AddSpectator("event := m_event", "I");


  // read training and test data
  TFile *input1 = TFile::Open( "/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microWW_EPDv01.root" );
  TFile *input2 = TFile::Open( "/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microWZ_EPDv01.root" );
  TFile *input3 = TFile::Open( "/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microWJets_EPDv01.root" );
  TFile *input4 = TFile::Open( "/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microZJets_EPDv01.root" );
  TFile *input5 = TFile::Open( "/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microTTbar_EPDv01.root" );
  TFile *input6 = TFile::Open( "/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microHWWMH150_EPDv01.root" );

 
  std::cout << "--- TMVAClassification : Using input file: " << input1->GetName() << std::endl;
  std::cout << "--- TMVAClassification : Using input file: " << input2->GetName() << std::endl;
  std::cout << "--- TMVAClassification : Using input file: " << input3->GetName() << std::endl;
  std::cout << "--- TMVAClassification : Using input file: " << input4->GetName() << std::endl;
  std::cout << "--- TMVAClassification : Using input file: " << input5->GetName() << std::endl;
  std::cout << "--- TMVAClassification : Using input file: " << input6->GetName() << std::endl;
   
  //TTree *signal     = (TTree*)input1->Get("WJet");
  //TTree *background = (TTree*)input3->Get("WJet");
  TChain *signal = new TChain("METree");
  signal->Add("/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microHWWMH150_EPDv01.root");
  TChain *background = new TChain("METree"); 
  background->Add("/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microWW_EPDv01.root");
  background->Add("/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microWZ_EPDv01.root");
  background->Add("/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microWJets_EPDv01.root");
  background->Add("/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microZJets_EPDv01.root");
  background->Add("/uscms/home/ilyao/nobackup/Spring12ME7TeV/MEResults/microNtuples/microTTbar_EPDv01.root");

  // global event weights per tree (see below for setting event-wise weights)
  Double_t signalWeight     = 1.0;
  Double_t backgroundWeight = 1.0;

  // ====== register trees ====================================================
  //
  // the following method is the prefered one:
  // you can add an arbitrary number of signal or background trees
  factory->AddSignalTree    ( signal,     signalWeight     );
  factory->AddBackgroundTree( background, backgroundWeight );

  // To give different trees for training and testing, do as follows:
  //    factory->AddSignalTree( signalTrainingTree, signalTrainWeight, "Training" );
  //    factory->AddSignalTree( signalTestTree,     signalTestWeight,  "Test" );

  // Use the following code instead of the above two or four lines to add signal and background 
  // training and test events "by hand"
  // NOTE that in this case one should not give expressions (such as "var1+var2") in the input 
  //      variable definition, but simply compute the expression before adding the event
  // 
  //    // --- begin ----------------------------------------------------------
  //    std::vector<Double_t> vars( 4 ); // vector has size of number of input variables
  //    Float_t  treevars[4];
  //    for (Int_t ivar=0; ivar<4; ivar++) signal->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
  //    for (Int_t i=0; i<signal->GetEntries(); i++) {
  //       signal->GetEntry(i);
  //       for (Int_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
  //       // add training and test events; here: first half is training, second is testing
  //       // note that the weight can also be event-wise	
  //       if (i < signal->GetEntries()/2) factory->AddSignalTrainingEvent( vars, signalWeight ); 
  //       else                            factory->AddSignalTestEvent    ( vars, signalWeight ); 
  //    }
  //
  //    for (Int_t ivar=0; ivar<4; ivar++) background->SetBranchAddress( Form( "var%i", ivar+1 ), &(treevars[ivar]) );
  //    for (Int_t i=0; i<background->GetEntries(); i++) {
  //       background->GetEntry(i); 
  //       for (Int_t ivar=0; ivar<4; ivar++) vars[ivar] = treevars[ivar];
  //       // add training and test events; here: first half is training, second is testing
  //       // note that the weight can also be event-wise	
  //       if (i < background->GetEntries()/2) factory->AddBackgroundTrainingEvent( vars, backgroundWeight ); 
  //       else                                factory->AddBackgroundTestEvent    ( vars, backgroundWeight ); 
  //    }
  //    // --- end ------------------------------------------------------------
  //
  // ====== end of register trees ==============================================

   
  // This would set individual event weights (the variables defined in the 
  // expression need to exist in the original TTree)
  //    for signal    : factory->SetSignalWeightExpression("weight1*weight2");
  //    for background: factory->SetBackgroundWeightExpression("weight1*weight2");
  // factory->SetBackgroundWeightExpression("weight");

  // Apply additional cuts on the signal and background samples (can be different)
  //   TCut mycuts = "abs(eta)>1.5"; // for example: TCut mycuts = "abs(var1)<0.5 && abs(var2-0.5)<1";
  //   TCut mycutb = "abs(eta)>1.5"; // for example: TCut mycutb = "abs(var1)<0.5";

  //Acceptance/Base cuts
  //TCut goodW("W_electron_et>30. && TMath::Abs(W_electron_eta)<2.5 && event_met_pfmet>25.");
  TCut goodW("W_muon_pt>25. && TMath::Abs(W_muon_eta)<2.5 && event_met_pfmet>25.");
  TCut twojets("numPFCorJets==2");
  TCut jetPt("JetPFCor_Pt[0]>30. && JetPFCor_Pt[1]>30.");
  TCut jetEta("TMath::Abs(JetPFCor_Eta[0])<2.5 && TMath::Abs(JetPFCor_Eta[1])<2.5");
  TCut deltaR1("TMath::Sqrt(TMath::Power(TMath::Abs(TMath::Abs(TMath::Abs(JetPFCor_Phi[0]-W_muon_phi)-TMath::Pi())-TMath::Pi()),2)+TMath::Power(JetPFCor_Eta[0]-W_muon_eta,2))>0.5");
  TCut deltaR2("TMath::Sqrt(TMath::Power(TMath::Abs(TMath::Abs(TMath::Abs(JetPFCor_Phi[1]-W_muon_phi)-TMath::Pi())-TMath::Pi()),2)+TMath::Power(JetPFCor_Eta[1]-W_muon_eta,2))>0.5");
  TCut noBJets("numPFCorJetBTags==0");
  TCut null("");

  //TCut mycuts (goodW && twojets && jetPt && jetEta && deltaR1 && deltaR2 && noBJets);
  TCut mycuts (null);

  // tell the factory to use all remaining events in the trees after training for testing:
  factory->PrepareTrainingAndTestTree( mycuts, mycuts,
                                       "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );

  // If no numbers of events are given, half of the events in the tree are used for training, and 
  // the other half for testing:
  //    factory->PrepareTrainingAndTestTree( mycut, "SplitMode=random:!V" );  
  // To also specify the number of testing events, use:
  //    factory->PrepareTrainingAndTestTree( mycut, 
  //                                         "NSigTrain=3000:NBkgTrain=3000:NSigTest=3000:NBkgTest=3000:SplitMode=Random:!V" );  

  // ---- Book MVA methods
  //
  // please lookup the various method configuration options in the corresponding cxx files, eg:
  // src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
  // it is possible to preset ranges in the option string in which the cut optimisation should be done:
  // "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable

  // Cut optimisation
  if (Use["Cuts"])
    factory->BookMethod( TMVA::Types::kCuts, "Cuts", 
                         "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart" );

  if (Use["CutsD"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsD", 
                         "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" );

  if (Use["CutsPCA"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsPCA", 
                         "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=PCA" );

  if (Use["CutsGA"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsGA",
                         "H:!V:FitMethod=GA:CutRangeMin[0]=-10:CutRangeMax[0]=10:VarProp[1]=FMax:EffSel:Steps=30:Cycles=3:PopSize=400:SC_steps=10:SC_rate=5:SC_factor=0.95" );
   
  if (Use["CutsSA"])
    factory->BookMethod( TMVA::Types::kCuts, "CutsSA",
                         "!H:!V:FitMethod=SA:EffSel:MaxCalls=150000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );
   
  // Likelihood
  if (Use["Likelihood"])
     factory->BookMethod( TMVA::Types::kLikelihood, "Likelihood", 
                          "H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" ); 

  // test the decorrelated likelihood
  if (Use["LikelihoodD"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodD", 
                         "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" ); 

  if (Use["LikelihoodPCA"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodPCA", 
                         "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA" ); 
 
  // test the new kernel density estimator
  if (Use["LikelihoodKDE"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodKDE", 
                         "!H:!V:!TransformOutput:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" ); 

  // test the mixed splines and kernel density estimator (depending on which variable)
  if (Use["LikelihoodMIX"])
    factory->BookMethod( TMVA::Types::kLikelihood, "LikelihoodMIX", 
                         "!H:!V:!TransformOutput:PDFInterpolSig[0]=KDE:PDFInterpolBkg[0]=KDE:PDFInterpolSig[1]=KDE:PDFInterpolBkg[1]=KDE:PDFInterpolSig[2]=Spline2:PDFInterpolBkg[2]=Spline2:PDFInterpolSig[3]=Spline2:PDFInterpolBkg[3]=Spline2:KDEtype=Gauss:KDEiter=Nonadaptive:KDEborder=None:NAvEvtPerBin=50" ); 

  // test the multi-dimensional probability density estimator
  // here are the options strings for the MinMax and RMS methods, respectively:
  //      "!H:!V:VolumeRangeMode=MinMax:DeltaFrac=0.2:KernelEstimator=Gauss:GaussSigma=0.3" );   
  //      "!H:!V:VolumeRangeMode=RMS:DeltaFrac=3:KernelEstimator=Gauss:GaussSigma=0.3" );   
  if (Use["PDERS"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERS", 
                         "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" );

  if (Use["PDERSkNN"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERSkNN", 
                         "!H:!V:VolumeRangeMode=kNN:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" );

  if (Use["PDERSD"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERSD", 
                         "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=Decorrelate" );

  if (Use["PDERSPCA"])
    factory->BookMethod( TMVA::Types::kPDERS, "PDERSPCA", 
                         "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=PCA" );
  /*
  // Multi-dimensional likelihood estimator using self-adapting phase-space binning
  if (Use["PDEFoam"])
    factory->BookMethod( TMVA::Types::kPDEFoam, "PDEFoam", 
                         "H:!V:SigBgSeparate=F:TailCut=0.001:VolFrac=0.0333:nActiveCells=500:nSampl=2000:nBin=5:CutNmin=T:Nmin=100:Kernel=None:Compress=T" );
  */
  // K-Nearest Neighbour classifier (KNN)
  if (Use["KNN"])
    factory->BookMethod( TMVA::Types::kKNN, "KNN", 
                         "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" );
  // H-Matrix (chi2-squared) method
  if (Use["HMatrix"])
    factory->BookMethod( TMVA::Types::kHMatrix, "HMatrix", "!H:!V" ); 

  // Fisher discriminant   
  if (Use["Fisher"])
    factory->BookMethod( TMVA::Types::kFisher, "Fisher", "H:!V:Fisher:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=60:NsmoothMVAPdf=10" );

  // Fisher with Gauss-transformed input variables
  if (Use["FisherG"])
    factory->BookMethod( TMVA::Types::kFisher, "FisherG", "H:!V:VarTransform=Gauss" );

  // Composite classifier: ensemble (tree) of boosted Fisher classifiers
  if (Use["BoostedFisher"])
    factory->BookMethod( TMVA::Types::kFisher, "BoostedFisher", "H:!V:Boost_Num=20:Boost_Transform=log:Boost_Type=AdaBoost:Boost_AdaBoostBeta=0.2");
  /*
  // Linear discriminant (same as Fisher)
  if (Use["LD"])
    factory->BookMethod( TMVA::Types::kLD, "LD", "H:!V:VarTransform=None" );
  */
  // Function discrimination analysis (FDA) -- test of various fitters - the recommended one is Minuit (or GA or SA)
  if (Use["FDA_MC"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_MC",
                         "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:SampleSize=100000:Sigma=0.1" );
   
  if (Use["FDA_GA"]) // can also use Simulated Annealing (SA) algorithm (see Cuts_SA options])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_GA",
                         "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=300:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" );

  if (Use["FDA_SA"]) // can also use Simulated Annealing (SA) algorithm (see Cuts_SA options])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_SA",
                         "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=SA:MaxCalls=15000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );

  if (Use["FDA_MT"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_MT",
                         "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=2:UseImprove:UseMinos:SetBatch" );

  if (Use["FDA_GAMT"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_GAMT",
                         "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:Cycles=1:PopSize=5:Steps=5:Trim" );

  if (Use["FDA_MCMT"])
    factory->BookMethod( TMVA::Types::kFDA, "FDA_MCMT",
                         "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:SampleSize=20" );

  // TMVA ANN: MLP (recommended ANN) -- all ANNs in TMVA are Multilayer Perceptrons
  if (Use["MLP"])
    factory->BookMethod( TMVA::Types::kMLP, "MLP", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=500:HiddenLayers=N+5:TestRate=10:EpochMonitoring" );

  if (Use["MLPBFGS"])
    factory->BookMethod( TMVA::Types::kMLP, "MLPBFGS", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=500:HiddenLayers=N+5:TestRate=10:TrainingMethod=BFGS:!EpochMonitoring" );


  // CF(Clermont-Ferrand)ANN
  if (Use["CFMlpANN"])
    factory->BookMethod( TMVA::Types::kCFMlpANN, "CFMlpANN", "!H:!V:NCycles=2000:HiddenLayers=N+1,N"  ); // n_cycles:#nodes:#nodes:...  
  
  // Tmlp(Root)ANN
  if (Use["TMlpANN"])
    factory->BookMethod( TMVA::Types::kTMlpANN, "TMlpANN", "!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3"  ); // n_cycles:#nodes:#nodes:...
  
  // Support Vector Machine
  if (Use["SVM"])
    factory->BookMethod( TMVA::Types::kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm" );
   
  // Boosted Decision Trees
  if (Use["BDTG"]) // Gradient Boost
    factory->BookMethod( TMVA::Types::kBDT, "BDTG", 
                         "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedGrad:GradBaggingFraction=0.6:SeparationType=GiniIndex:nCuts=20:NNodesMax=5" );

  if (Use["BDT"])  // Adaptive Boost
    factory->BookMethod( TMVA::Types::kBDT, "BDT", 
                         "!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );
   
  if (Use["BDTB"]) // Bagging
    factory->BookMethod( TMVA::Types::kBDT, "BDTB", 
                         "!H:!V:NTrees=400:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" );

  if (Use["BDTD"]) // Decorrelation + Adaptive Boost
    factory->BookMethod( TMVA::Types::kBDT, "BDTD", 
                         "!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning:VarTransform=Decorrelate" );
   
  // RuleFit -- TMVA implementation of Friedman's method
  if (Use["RuleFit"])
    factory->BookMethod( TMVA::Types::kRuleFit, "RuleFit",
                         "H:!V:RuleFitModule=RFTMVA:Model=ModRuleLinear:MinImp=0.001:RuleMinDist=0.001:NTrees=20:fEventsMin=0.01:fEventsMax=0.5:GDTau=-1.0:GDTauPrec=0.01:GDStep=0.01:GDNSteps=10000:GDErrScale=1.02" );
   
  // For an example of the category classifier, see: TMVAClassificationCategory

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

  // As an example how to use the ROOT plugin mechanism, book BDT via
  // plugin mechanism
  if (Use["Plugin"]) {
    //
    // first the plugin has to be defined, which can happen either through the following line in the local or global .rootrc:
    //
    // # plugin handler          plugin name(regexp) class to be instanciated library        constructor format
    // Plugin.TMVA@@MethodBase:  ^BDT                TMVA::MethodBDT          TMVA.1         "MethodBDT(TString,TString,DataSet&,TString)"
    // 
    // or by telling the global plugin manager directly
    gPluginMgr->AddHandler("TMVA@@MethodBase", "BDT", "TMVA::MethodBDT", "TMVA.1", "MethodBDT(TString,TString,DataSet&,TString)");
    factory->BookMethod( TMVA::Types::kPlugins, "BDT",
                         "!H:!V:NTrees=400:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=CostComplexity:PruneStrength=50" );
  }

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

  // ---- Now you can tell the factory to train, test, and evaluate the MVAs

  // Train MVAs using the set of training events
  factory->TrainAllMethods();

  // ---- Evaluate all MVAs using the set of test events
  factory->TestAllMethods();

  // ----- Evaluate and compare performance of all configured MVAs
  factory->EvaluateAllMethods();    

  // --------------------------------------------------------------
   
  // Save the output
  outputFile->Close();

  std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
  std::cout << "==> TMVAClassification is done!" << std::endl;      

  delete factory;

  // Launch the GUI for the root macros
  if (!gROOT->IsBatch()) TMVAGui( outfileName );
}
Example #16
0
int efficiency(char* path0) {
	char the_path[256];
    getcwd(the_path, 255);

    std::string path(path0);


  	unsigned found = path.find_last_of("/\\", path.size()-2);

    std::string dataPath(path.substr(0,found));
    std::string measurementFolder(path.substr(found+1));

	std::stringstream ss1(measurementFolder);
    std::string moduleName;
    std::getline(ss1, moduleName, '_');

	std::string rootFileName("commander_HREfficiency.root");
	std::string maskFileName("defaultMaskFile.dat");

	const bool FIDUCIAL_ONLY = true;
	const bool VERBOSE = true;

	int nTrigPerPixel = 50;
	int nPixels = 4160;
	int nTrig = nTrigPerPixel * nPixels;
	float pixelArea = 0.01 * 0.015; // cm^2
	float triggerDuration = 25e-9; //s

	int nRocs = 16;
	
	//search for XrarSpectrum folders in elComandante folder structure
    TSystemDirectory dir(path.c_str(), path.c_str());
    TList *files = dir.GetListOfFiles();
    std::vector<std::string> directoryList;
    if (files) {
      TSystemFile *file;
      TString fname;
      TIter next(files);
      while (file=(TSystemFile*)next()) {
         fname = file->GetName();
         if (file->IsDirectory()) {
         	std::string dirname(fname.Data());
         	if (dirname.find("HREfficiency") != std::string::npos) {
         		directoryList.push_back(dirname);
         	}
         }
      }
    }

    std::vector< std::vector< std::pair< int,int > > > maskedPixels;

    //sort!
	std::sort(directoryList.begin(), directoryList.end());

	std::vector< std::vector< double > > efficiencies;
	std::vector< std::vector< double > > efficiencyErrors;
	std::vector< std::vector< double > > rates;
	std::vector< std::vector< double > > rateErrors;


	for (int i=0;i<nRocs;i++) {
		std::vector< double > empty;
		efficiencies.push_back(empty);
		efficiencyErrors.push_back(empty);
		rates.push_back(empty);
		rateErrors.push_back(empty);
	}

	// loop over all commander_HREfficiency.root root files
	for (int i=0;i<directoryList.size();++i) {
		chdir(path.c_str());
		std::cout << "looking in directory <" << directoryList[i] << ">" << std::endl;

		std::ifstream testParameters(Form("%s/testParameters.dat", directoryList[i].c_str()));
		std::string line2;
		while (getline(testParameters, line2)) {
			std::transform(line2.begin(), line2.end(), line2.begin(), ::tolower);
			if (line2.find("highrate") != std::string::npos) {
				while (getline(testParameters, line2)) {
					if (line2.size() < 1) break;
					std::transform(line2.begin(), line2.end(), line2.begin(), ::tolower);
					size_t pos = line2.find("ntrig");
					if (pos != std::string::npos) {
						nTrigPerPixel = atoi(line2.substr(pos+6).c_str());
						nTrig = nTrigPerPixel * nPixels;
						std::cout << ">" << line2 << "< pos:" << pos << std::endl;
						std::cout << "number of triggers per pixel: " << nTrigPerPixel << std::endl;
					}
				}

			}
		}
		testParameters.close();


		// read masked pixels
	    maskedPixels.clear();
		for (int j=0;j<nRocs;j++) {
			std::vector< std::pair<int,int> > rocMaskedPixels;
			maskedPixels.push_back(rocMaskedPixels);
		}
		std::ifstream maskFile;
		char maskFilePath[256];
		sprintf(maskFilePath, "%s/%s/%s", path.c_str(), directoryList[i].c_str(), maskFileName.c_str());
		maskFile.open(maskFilePath, std::ifstream::in);
		if (!maskFile) {
			std::cout << "ERROR: mask file <" << maskFilePath << "> can't be opened!"<<std::endl;
		}
		std::string line;
		std::vector< std::string > tokens;
		while(getline(maskFile, line)) {
			if (line[0] != '#') {
				std::stringstream ss(line); 
	    		std::string buf;
	    		tokens.clear();
				while (ss >> buf) {
					tokens.push_back(buf);
				}
				std::cout << "tok0 <" << tokens[0] << "> ";
				if (tokens[0] == "pix" && tokens.size() >= 4) {
					int roc = atoi(tokens[1].c_str());
					int col = atoi(tokens[2].c_str());
					int row = atoi(tokens[3].c_str());
					std::cout << "mask pixel " << roc << " " << col << " " << row << std::endl;
					maskedPixels[roc].push_back(make_pair(col, row));
				}
			}
		}
		maskFile.close();

		chdir(directoryList[i].c_str());
		
		TFile f(rootFileName.c_str());
		if (f.IsZombie()) {
			std::cout << "could not read: " << rootFileName << " .";
			exit(0);
		}
		std::cout << "list keys:" << std::endl;
		TIter next(f.GetListOfKeys());
		bool highRateFound = false;


		while (TKey *obj = (TKey*)next()) {
			if (strcmp(obj->GetTitle(),"HighRate") == 0) highRateFound = true;
			if (VERBOSE) {
				std::cout << obj->GetTitle() << std::endl;
			}
		}
		if (highRateFound) {
			std::cout << "highRate test found, reading data..." << std::endl;
			TH2D* xraymap;
			TH2D* calmap;
			char calmapName[256];
			char xraymapName[256];
			std::ofstream output;

			for (int iRoc=0;iRoc<nRocs;iRoc++) {

				std::cout << "ROC" << iRoc << std::endl;
				sprintf(xraymapName, "HighRate/highRate_xraymap_C%d_V0;1", iRoc);
				f.GetObject(xraymapName, xraymap);
				if (xraymap == 0) {
					std::cout << "ERROR: x-ray hitmap not found!" << std::endl;
				}
				int nBinsX = xraymap->GetXaxis()->GetNbins();
				int nBinsY = xraymap->GetYaxis()->GetNbins();

				sprintf(calmapName, "HighRate/highRate_C%d_V0;1", iRoc);
				f.GetObject(calmapName, calmap);
				if (calmap == 0) {
					sprintf(calmapName, "HighRate/highRate_calmap_C%d_V0;1", iRoc);
					f.GetObject(calmapName, calmap);
					if (calmap == 0) {
						std::cout << "ERROR: calibration hitmap not found!" << std::endl;
					}
				}

				//std::cout << nBinsX << "x" << nBinsY << std::endl;
				float rate, efficiency;

				for (int doubleColumn = 1; doubleColumn < 25; doubleColumn++) {
					//std::cout << "reading dc " << doubleColumn << std::endl;

					std::vector<double> hits;
					std::vector<double> xray_hits;

					for (int y = 0; y < 160; y++) {

						bool masked = false;

						for (int iMaskedPixels=0; iMaskedPixels < maskedPixels[iRoc].size(); iMaskedPixels++) {
							if (maskedPixels[iRoc][iMaskedPixels].first == doubleColumn * 2 + (int)(y / 80) && maskedPixels[iRoc][iMaskedPixels].second == (y % 80)) {
								masked = true;
								break;
							}
						}

						if ((!FIDUCIAL_ONLY || ((y % 80) > 0 && (y % 80) < 79)) && !masked) {
							//std::cout << " get " << (doubleColumn * 2 + (int)(y / 80) + 1) << " / " <<  ((y % 80) + 1) << std::endl;

							hits.push_back( calmap->GetBinContent(doubleColumn * 2 + (int)(y / 80) + 1, (y % 80) + 1) );
							xray_hits.push_back( xraymap->GetBinContent(doubleColumn * 2 + (int)(y / 80) + 1, (y % 80) + 1) );

						}
					}
					//std::cout << "calculating rates and efficiencies" << std::endl;

					int nPixelsDC = hits.size();
					if (nPixelsDC < 1) nPixelsDC = 1;
					double rate = TMath::Mean(nPixelsDC, &xray_hits[0]) / (nTrig * triggerDuration * pixelArea) * 1.0e-6;
					double efficiency = TMath::Mean(nPixelsDC, &hits[0]) / nTrigPerPixel;
					double rateError = TMath::RMS(nPixelsDC, &xray_hits[0]) / std::sqrt(nPixelsDC) / (nTrig * triggerDuration * pixelArea) * 1.0e-6;
					double efficiencyError = TMath::RMS(nPixelsDC, &hits[0]) / std::sqrt(nPixelsDC) / nTrigPerPixel;

					
						efficiencies[iRoc].push_back(efficiency);
						efficiencyErrors[iRoc].push_back(efficiencyError);
						rates[iRoc].push_back(rate);
						rateErrors[iRoc].push_back(rateError);
					

					if (VERBOSE) {
						std::cout << "dc " << doubleColumn << " nPixelsDC: " << nPixelsDC << " rate: " << rate << " " << efficiency << std::endl;
					}
				}
			}



	  		//output.open (Form("%s/output_%d.txt",the_path,i), std::ofstream::out);
			//output.close();

		} else {
			std::cout << "high rate test not found";
			return 1;
		}
	}
void PValueCorrection::readFiles(TString name, int id, bool isPlugin){

	// read coverage test files
	name = "root/"+name;
	TSystemDirectory dir(name,name);
	TList *files = dir.GetListOfFiles();
	TChain *fChain = new TChain("tree");
	int foundFiles=0;
	if (files){
		TSystemFile *file;
		TString fname;
		TIter next(files);
		while ((file=(TSystemFile*)next())) {
			fname = file->GetName();
			if (!file->IsDirectory() && fname.Contains(Form("id%d",id)) && fname.EndsWith(".root")) {
				//cout << fname << endl;
				fChain->Add(name+"/"+fname);
				foundFiles++;
			}
		}
	}

	cout << "PValueCorrector::readFiles() -- found " << foundFiles << " files in " << dir.GetName() << " with total of " << fChain->GetEntries() << " entries" << endl;
	cout << "PValueCorrector::readFiles() -- will apply on the fly correction of type ";
	isPlugin ? cout << " plugin"<< endl : cout << " prob" << endl;

	// fill histogram from tree
	h_pvalue_before = new TH1F("h_pvalue_before","p-value",50,0.,1.);
	h_pvalue_after = new TH1F("h_pvalue_after","p-value",50,0.,1.);


	// set up root tree for reading
  float tSol = 0.0;
  float tChi2free = 0.0;
  float tChi2scan = 0.0;
  float tSolGen = 0.0;
  float tPvalue = 0.0;
  fChain->SetBranchAddress("sol",      &tSol);
  fChain->SetBranchAddress("chi2free", &tChi2free);
  fChain->SetBranchAddress("chi2scan", &tChi2scan);
  fChain->SetBranchAddress("solGen",   &tSolGen);
  fChain->SetBranchAddress("pvalue",   &tPvalue);

	// initialize loop variables
  Long64_t nentries = fChain->GetEntries();
  Long64_t nfailed = 0;
  float n68 =0.;
  float n95 =0.;
  float n99 =0.;

	for (Long64_t i = 0; i < nentries; i++)
	{
		fChain->GetEntry(i);
    // apply cuts (we'll count the failed ones later)
    if ( ! (tChi2free > -1e10 && tChi2scan > -1e10
         && tChi2scan-tChi2free>0
         && tSol != 0.0 ///< exclude some pathological jobs from when tSol wasn't set yet
         && tChi2free < 500 && tChi2scan < 500
      )){
			nfailed++;
      continue;
    }
		float pvalue = -999.;
		if (isPlugin) {
			pvalue = tPvalue;
		}
		else {
			pvalue = TMath::Prob(tChi2scan-tChi2free,1);
		}

		if ( pvalue > TMath::Prob(1,1) ) n68++;
		if ( pvalue > TMath::Prob(4,1) ) n95++;
		if ( pvalue > TMath::Prob(9,1) ) n99++;

		h_pvalue_before->Fill(pvalue);
	}

	fitHist(h_pvalue_before);
	if (verbose) printCoverage(n68,n95,n99,float(nentries-nfailed),"Before Correction");

	n68=0.;
	n95=0.;
	n99=0.;
	for (Long64_t i = 0; i < nentries; i++)
	{
		fChain->GetEntry(i);
    // apply cuts (we'll count the failed ones later)
    if ( ! (tChi2free > -1e10 && tChi2scan > -1e10
         && tChi2scan-tChi2free>0
         && tSol != 0.0 ///< exclude some pathological jobs from when tSol wasn't set yet
         && tChi2free < 500 && tChi2scan < 500
      )){
      continue;
    }
		float pvalue = -999.;
		if (isPlugin) {
			pvalue = transform(tPvalue);
		}
		else {
			pvalue = transform(TMath::Prob(tChi2scan-tChi2free,1));
		}

		if ( pvalue > TMath::Prob(1,1) ) n68++;
		if ( pvalue > TMath::Prob(4,1) ) n95++;
		if ( pvalue > TMath::Prob(9,1) ) n99++;

		h_pvalue_after->Fill(pvalue);
	}

	if (verbose) printCoverage(n68,n95,n99,float(nentries-nfailed),"After Correction");
	checkParams();
}
void recupThePlots(){
//	TDirectory *theDr = (TDirectory*) myFile->Get("eleIDdir");///denom_pt/fit_eff_plots");
	//theDr->ls();
	cout << "coucou" << theOutFileName << endl;
	
	TFile *myOutFile = new TFile(theOutFileName,"RECREATE");
	
	TSystemDirectory dir(thePath, thePath);
	TSystemFile *file;
	TString fname;
	TIter next(dir.GetListOfFiles());
	while (((file=(TSystemFile*)next()))) {
		fname = file->GetName();
		if ((fname.BeginsWith("TnP"))&&fname.Contains("data")) {
                        cout <<  "--------------------"<< "\n";
                        cout << fname << "\n";
			TFile *myFile = new TFile(fname);
			TIter nextkey(myFile->GetListOfKeys());                   
			TKey *key;
			while ((key = (TKey*)nextkey())) {
                                // cout << key << "\n";
				TString theTypeClasse = key->GetClassName();
				TString theNomClasse = key->GetTitle();
                                   cout << "theTypeClasse:  "<< theTypeClasse << " ,  " << theNomClasse << "\n";
				if ( theTypeClasse == "TDirectoryFile" ){
                                        //    cout << "we are here 1" << "\n";
					TDirectory *theDr = (TDirectory*) myFile->Get(theNomClasse);
					TIter nextkey2(theDr->GetListOfKeys());
					TKey *key2;
					while ((key2 = (TKey*)nextkey2())) {
						TString theTypeClasse2 = key2->GetClassName();
						TString theNomClasse2 = key2->GetTitle();	
						if ( theTypeClasse == "TDirectoryFile" || theTypeClasse == "TGraphAsymmErrors" ){
							TDirectory *theDr2 = (TDirectory*) myFile->Get(theNomClasse+"/"+theNomClasse2+"/fit_eff_plots");
							TIter nextkey3(theDr2->GetListOfKeys());
							TKey *key3;
							while ((key3 = (TKey*)nextkey3())) {
								TString theTypeClasse3 = key3->GetClassName();
								TString theNomClasse3 = key3->GetName();	
				 				cout << "type = " << theTypeClasse3 << " nom = " << theNomClasse3 << endl;
						
								TCanvas *theCanvas = (TCanvas*) myFile->Get(theNomClasse+"/"+theNomClasse2+"/fit_eff_plots/"+theNomClasse3);
								TIter nextObject(theCanvas->GetListOfPrimitives());
								TObject *obj;
								while ((obj = (TObject*)nextObject())) {
									if (obj->InheritsFrom("TGraphAsymmErrors")) {
					        				cout << "histo: " << obj->GetName() << endl;
										myOutFile->cd();
										obj->Write(theNomClasse2+"_"+theNomClasse3);
										myFile->cd();
									}
									if (obj->InheritsFrom("TH2F")) {
										cout << "the TH2F = " << obj->GetName() << endl;
										myOutFile->cd();
										obj->Write(theNomClasse2+"_"+theNomClasse3);
										myFile->cd();
									}
								}
							}
						}

					}
			
				}
			}
			delete myFile;
		}
	
	}
	myOutFile->Close();

}
TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
{
  // creates chain of files in a given directory or file containing a list.
  // In case of directory the structure is expected as:
  // <aDataDir>/<dir0>/AliAOD.root
  // <aDataDir>/<dir1>/AliAOD.root
  // ...

  if (!aDataDir)
    return 0;

  Long_t id, size, flags, modtime;
  if (gSystem->GetPathInfo(aDataDir, &id, &size, &flags, &modtime))
  {
    printf("%s not found.\n", aDataDir);
    return 0;
  }

  TChain* chain = new TChain("aodTree");
  TChain* chaingAlice = 0;

  if (flags & 2)
  {
    TString execDir(gSystem->pwd());
    TSystemDirectory* baseDir = new TSystemDirectory(".", aDataDir);
    TList* dirList            = baseDir->GetListOfFiles();
    Int_t nDirs               = dirList->GetEntries();
    gSystem->cd(execDir);

    Int_t count = 0;

    for (Int_t iDir=0; iDir<nDirs; ++iDir)
    {
      TSystemFile* presentDir = (TSystemFile*) dirList->At(iDir);
      if (!presentDir || !presentDir->IsDirectory() || strcmp(presentDir->GetName(), ".") == 0 || strcmp(presentDir->GetName(), "..") == 0)
        continue;

      if (offset > 0)
      {
        --offset;
        continue;
      }

      if (count++ == aRuns)
        break;

      TString presentDirName(aDataDir);
      presentDirName += "/";
      presentDirName += presentDir->GetName();
      chain->Add(presentDirName + "/AliAOD.root/aodTree");
      // cerr<<presentDirName<<endl;
    }

  }
  else
  {
    // Open the input stream
    ifstream in;
    in.open(aDataDir);

    Int_t count = 0;

    // Read the input list of files and add them to the chain
    TString aodfile;
    while(in.good())
    {
      in >> aodfile;
      if (!aodfile.Contains("root")) continue; // protection

      if (offset > 0)
      {
        --offset;
        continue;
      }

      if (count++ == aRuns)
        break;

      // add aod file
      chain->Add(aodfile);
    }

    in.close();
  }

  return chain;

} // end of TChain* CreateAODChain(const char* aDataDir, Int_t aRuns, Int_t offset)
Example #20
0
prepDataFiles(){
//	TDirectory *theDr = (TDirectory*) myFile->Get("eleIDdir");///denom_pt/fit_eff_plots");
	//theDr->ls();
	int myIndex;	
	
	TSystemDirectory dir(thePath, thePath);
	TSystemFile *file;
	TString fname;
	TIter next(dir.GetListOfFiles());
	while ((file=(TSystemFile*)next())) {
		fname = file->GetName();
		if (fname.BeginsWith("TnP")&& fname.Contains("mc")) {
	
			ofstream myfile;

			TFile *myFile = new TFile(fname);
			TIter nextkey(myFile->GetListOfKeys());
			TKey *key;
			while (key = (TKey*)nextkey()) {
				TString theTypeClasse = key->GetClassName();
				TString theNomClasse = key->GetTitle();
				if ( theTypeClasse == "TDirectoryFile"){
					TDirectory *theDr = (TDirectory*) myFile->Get(theNomClasse);
					TIter nextkey2(theDr->GetListOfKeys());
					TKey *key2;
					while (key2 = (TKey*)nextkey2()) {
						TString theTypeClasse2 = key2->GetClassName();
						TString theNomClasse2 = key2->GetTitle();	
						myfile.open (theNomClasse2+".info");
						if ( theTypeClasse == "TDirectoryFile"){
							cout << "avant " << endl;
							TDirectory *theDr2 = (TDirectory*) myFile->Get(theNomClasse+"/"+theNomClasse2);
							cout << "apres " << endl;
							TIter nextkey3(theDr2->GetListOfKeys());
							TKey *key3;
							while (key3 = (TKey*)nextkey3()) {
								TString theTypeClasse3 = key3->GetClassName();
								TString theNomClasse3 = key3->GetTitle();	
								if ((theNomClasse3.Contains("FromMC"))) {

									TString localClasse3 = theNomClasse3;
									localClasse3.ReplaceAll("__","%");
									cout << "apres " << localClasse3 << endl;
									TObjArray* listBin = localClasse3.Tokenize('%');
									TString first = ((TObjString*)listBin->At(0))->GetString();
									TString second = ((TObjString*)listBin->At(2))->GetString();
									myfile << first;
									myfile << " " << second << " ";
									cout << "coucou la on va récupérer le rooFitResult " << endl;

									RooFitResult *theResults = (RooFitResult*) myFile->Get(theNomClasse+"/"+theNomClasse2+"/"+theNomClasse3+"/fitresults");
									theResults->Print();
									RooArgList theParam = theResults->floatParsFinal();
									int taille = theParam.getSize();
									for (int m = 0 ; m < taille ; m++){
										cout << "m=" << m << endl;
									RooAbsArg *theArg = (RooAbsArg*) theParam.at(m);
									RooAbsReal *theReal = (RooAbsReal*) theArg;
										myfile << theReal->getVal() << " " ;
									}		
															
									myfile << "\n";

								}
							}
						}
						myfile.close();

					}
			
				}
			}
			delete myFile;
		}
	
	}

}
recupTheZpeak(){
//	TDirectory *theDr = (TDirectory*) myFile->Get("eleIDdir");///denom_pt/fit_eff_plots");
	//theDr->ls();
	int myIndex;	
	
	TSystemDirectory dir(thePath, thePath);
	TSystemFile *file;
	TString fname;
	TIter next(dir.GetListOfFiles());
	while ((file=(TSystemFile*)next())) {
		fname = file->GetName();
		if (fname.BeginsWith("TnP")) {
			if (fname.Contains("data")) kind = "data";
			else kind = "mc";
			bool pass = false;
			for (int p = 0 ; p< nbOfIds ; p++){
				if (fname.Contains(thePlotToDraw[p])) {
					pass = true;				 
					theIndex = p;
					break;
				}
			}
			if (!(pass)) continue;
			TFile *myFile = new TFile(fname);
			TIter nextkey(myFile->GetListOfKeys());
			TKey *key;
			while (key = (TKey*)nextkey()) {
				TString theTypeClasse = key->GetClassName();
				TString theNomClasse = key->GetTitle();
				if ( theTypeClasse == "TDirectoryFile"){
					TDirectory *theDr = (TDirectory*) myFile->Get(theNomClasse);
					TIter nextkey2(theDr->GetListOfKeys());
					TKey *key2;
					while (key2 = (TKey*)nextkey2()) {
						TString theTypeClasse2 = key2->GetClassName();
						TString theNomClasse2 = key2->GetTitle();	
						if ( theTypeClasse == "TDirectoryFile"){
							TDirectory *theDr2 = (TDirectory*) myFile->Get(theNomClasse+"/"+theNomClasse2);
							TIter nextkey3(theDr2->GetListOfKeys());
							TKey *key3;
							while (key3 = (TKey*)nextkey3()) {
								TString theTypeClasse3 = key3->GetClassName();
								TString theNomClasse3 = key3->GetTitle();	
								if (((theNomClasse3.Contains("vpvPlusExpo"))||(theNomClasse3.Contains("higgsSF")))&&(!(theNomClasse3.Contains("all")))) {
									TCanvas *theCanvas = (TCanvas*) myFile->Get(theNomClasse+"/"+theNomClasse2+"/"+theNomClasse3+"/fit_canvas");
									theCanvas->Print(outputDir+"/"+kind+"_"+theName[theIndex]+"__"+theNomClasse3+".png");
									
								}
							}
						}

					}
			
				}
			}
			delete myFile;
		}
	
	}

}