createData.C

Go to the documentation of this file.
00001 // plot the variables
00002 #include "TROOT.h"
00003 #include "TMath.h"
00004 #include "TTree.h"
00005 #include "TArrayD.h"
00006 #include "TStyle.h"
00007 #include "TFile.h"
00008 #include "TRandom.h"
00009 #include "Riostream.h"
00010 #include "TCanvas.h"
00011 #include "TMatrixD.h"
00012 #include "TH2F.h"
00013 #include "TLegend.h"
00014 #include "TBranch.h"
00015 #include <vector>
00016 
00017 void plot( TString fname = "data.root", TString var0="var0", TString var1="var1" ) 
00018 {
00019    TFile* dataFile = TFile::Open( fname );
00020 
00021    if (!dataFile) {
00022       cout << "ERROR: cannot open file: " << fname << endl;
00023       return;
00024    }
00025 
00026    TTree *treeS = (TTree*)dataFile->Get("TreeS");
00027    TTree *treeB = (TTree*)dataFile->Get("TreeB");
00028 
00029    TCanvas* c = new TCanvas( "c", "", 0, 0, 550, 550 );
00030 
00031    TStyle *TMVAStyle = gROOT->GetStyle("Plain"); // our style is based on Plain
00032    TMVAStyle->SetOptStat(0);
00033    TMVAStyle->SetPadTopMargin(0.02);
00034    TMVAStyle->SetPadBottomMargin(0.16);
00035    TMVAStyle->SetPadRightMargin(0.03);
00036    TMVAStyle->SetPadLeftMargin(0.15);
00037    TMVAStyle->SetPadGridX(0);
00038    TMVAStyle->SetPadGridY(0);
00039    
00040    TMVAStyle->SetOptTitle(0);
00041    TMVAStyle->SetTitleW(.4);
00042    TMVAStyle->SetTitleH(.10);
00043    TMVAStyle->SetTitleX(.5);
00044    TMVAStyle->SetTitleY(.9);
00045    TMVAStyle->SetMarkerStyle(20);
00046    TMVAStyle->SetMarkerSize(1.6);
00047    TMVAStyle->cd();
00048 
00049 
00050    Float_t xmin = TMath::Min( treeS->GetMinimum( var0 ), treeB->GetMinimum( var0 ) );
00051    Float_t xmax = TMath::Max( treeS->GetMaximum( var0 ), treeB->GetMaximum( var0 ) );
00052    Float_t ymin = TMath::Min( treeS->GetMinimum( var1 ), treeB->GetMinimum( var1 ) );
00053    Float_t ymax = TMath::Max( treeS->GetMaximum( var1 ), treeB->GetMaximum( var1 ) );
00054 
00055    Int_t nbin = 500;
00056    TH2F* frameS = new TH2F( "DataS", "DataS", nbin, xmin, xmax, nbin, ymin, ymax );
00057    TH2F* frameB = new TH2F( "DataB", "DataB", nbin, xmin, xmax, nbin, ymin, ymax );
00058 
00059    // project trees
00060    treeS->Draw( Form("%s:%s>>DataS",var1.Data(),var0.Data()), "", "0" );
00061    treeB->Draw( Form("%s:%s>>DataB",var1.Data(),var0.Data()
00062 ), "", "0" );
00063 
00064    // set style
00065    frameS->SetMarkerSize( 0.1 );
00066    frameS->SetMarkerColor( 4 );
00067 
00068    frameB->SetMarkerSize( 0.1 );
00069    frameB->SetMarkerColor( 2 );
00070 
00071    // legend
00072    frameS->SetTitle( var1+" versus "+var0+" for signal and background" );
00073    frameS->GetXaxis()->SetTitle( var0 );
00074    frameS->GetYaxis()->SetTitle( var1 );
00075 
00076    frameS->SetLabelSize( 0.04, "X" );
00077    frameS->SetLabelSize( 0.04, "Y" );
00078    frameS->SetTitleSize( 0.05, "X" );
00079    frameS->SetTitleSize( 0.05, "Y" );
00080 
00081    // and plot
00082    frameS->Draw();
00083    frameB->Draw( "same" );  
00084 
00085    // Draw legend               
00086    TLegend *legend = new TLegend( 1 - c->GetRightMargin() - 0.32, 1 - c->GetTopMargin() - 0.12, 
00087                                   1 - c->GetRightMargin(), 1 - c->GetTopMargin() );
00088    legend->SetFillStyle( 1 );
00089    legend->AddEntry(frameS,"Signal","p");
00090    legend->AddEntry(frameB,"Background","p");
00091    legend->Draw("same");
00092    legend->SetBorderSize(1);
00093    legend->SetMargin( 0.3 );
00094 
00095 }
00096 
00097 TMatrixD* produceSqrtMat( const TMatrixD& covMat )
00098 {
00099    Double_t sum = 0;
00100    Int_t size = covMat.GetNrows();;
00101    TMatrixD* sqrtMat = new TMatrixD( size, size );
00102 
00103    for (Int_t i=0; i< size; i++) {
00104       
00105       sum = 0;
00106       for (Int_t j=0;j< i; j++) sum += (*sqrtMat)(i,j) * (*sqrtMat)(i,j);
00107 
00108       (*sqrtMat)(i,i) = TMath::Sqrt(TMath::Abs(covMat(i,i) - sum));
00109 
00110       for (Int_t k=i+1 ;k<size; k++) {
00111 
00112          sum = 0;
00113          for (Int_t l=0; l<i; l++) sum += (*sqrtMat)(k,l) * (*sqrtMat)(i,l);
00114 
00115          (*sqrtMat)(k,i) = (covMat(k,i) - sum) / (*sqrtMat)(i,i);
00116 
00117       }
00118    }
00119    return sqrtMat;
00120 }
00121 
00122 void getGaussRnd( TArrayD& v, const TMatrixD& sqrtMat, TRandom& R ) 
00123 {
00124    // generate "size" correlated Gaussian random numbers
00125 
00126    // sanity check
00127    const Int_t size = sqrtMat.GetNrows();
00128    if (size != v.GetSize()) 
00129       cout << "<getGaussRnd> too short input vector: " << size << " " << v.GetSize() << endl;
00130 
00131    Double_t* tmpVec = new Double_t[size];
00132 
00133    for (Int_t i=0; i<size; i++) {
00134       Double_t x, y, z;
00135       y = R.Rndm();
00136       z = R.Rndm();
00137       x = 2*TMath::Pi()*z;
00138       tmpVec[i] = TMath::Sin(x) * TMath::Sqrt(-2.0*TMath::Log(y));
00139    }
00140 
00141    for (Int_t i=0; i<size; i++) {
00142       v[i] = 0;
00143       for (Int_t j=0; j<=i; j++) v[i] += sqrtMat(i,j) * tmpVec[j];
00144    }
00145 
00146    delete tmpVec;
00147 }
00148 
00149 // create the data
00150 void create_lin_Nvar_withFriend(Int_t N = 2000)
00151 {
00152    const Int_t nvar  = 4;
00153    const Int_t nvar2 = 1;
00154    Float_t xvar[nvar];
00155 
00156    // output flie
00157    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00158 
00159    // create signal and background trees
00160    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00161    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00162    for (Int_t ivar=0; ivar<nvar-nvar2; ivar++) {
00163      cout << "Creating branch var" << ivar+1 << " in signal tree" << endl;
00164       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00165       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00166    }
00167    TTree* treeSF = new TTree( "TreeSF", "TreeS", 1 );   
00168    TTree* treeBF = new TTree( "TreeBF", "TreeB", 1 );   
00169    for (Int_t ivar=nvar-nvar2; ivar<nvar; ivar++) {
00170       treeSF->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00171       treeBF->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00172    }
00173 
00174       
00175    TRandom R( 100 );
00176    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
00177    Float_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
00178    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00179    TArrayD* v = new TArrayD( nvar );
00180    Float_t rho[20];
00181    rho[1*2] = 0.4;
00182    rho[1*3] = 0.6;
00183    rho[1*4] = 0.9;
00184    rho[2*3] = 0.7;
00185    rho[2*4] = 0.8;
00186    rho[3*4] = 0.93;
00187 
00188    // create covariance matrix
00189    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00190    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00191    for (Int_t ivar=0; ivar<nvar; ivar++) {
00192       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00193       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00194       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00195          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00196          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00197 
00198          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00199          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00200       }
00201    }
00202 
00203    cout << "signal covariance matrix: " << endl;
00204    covMatS->Print();
00205    cout << "background covariance matrix: " << endl;
00206    covMatB->Print();
00207 
00208    // produce the square-root matrix
00209    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00210    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00211 
00212    // loop over species
00213    for (Int_t itype=0; itype<2; itype++) {
00214 
00215       Float_t*  x;
00216       TMatrixD* m;
00217       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
00218       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
00219 
00220       // event loop
00221       TTree* tree  = (itype==0) ? treeS : treeB;
00222       TTree* treeF = (itype==0) ? treeSF : treeBF;
00223       for (Int_t i=0; i<N; i++) {
00224 
00225          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00226          getGaussRnd( *v, *m, R );
00227 
00228          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
00229          
00230          tree->Fill();
00231          treeF->Fill();
00232       }
00233    }
00234 
00235 //    treeS->AddFriend(treeSF);
00236 //    treeB->AddFriend(treeBF);
00237 
00238    // write trees
00239    treeS->Write();
00240    treeB->Write();
00241    treeSF->Write();
00242    treeBF->Write();
00243 
00244    treeS->Show(0);
00245    treeB->Show(1);
00246 
00247    dataFile->Close();
00248    cout << "created data file: " << dataFile->GetName() << endl;
00249 
00250 
00251 }
00252 
00253 
00254 // create the tree
00255 TTree* makeTree_lin_Nvar( TString treeName, TString treeTitle, Float_t* x, Float_t* dx, const Int_t nvar, Int_t N )
00256 {
00257    Float_t xvar[nvar];
00258 
00259    // create tree
00260    TTree* tree = new TTree(treeName, treeTitle, 1);
00261 
00262    for (Int_t ivar=0; ivar<nvar; ivar++) {
00263       tree->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00264    }
00265       
00266    TRandom R( 100 );
00267    TArrayD* v = new TArrayD( nvar );
00268    Float_t rho[20];
00269    rho[1*2] = 0.4;
00270    rho[1*3] = 0.6;
00271    rho[1*4] = 0.9;
00272    rho[2*3] = 0.7;
00273    rho[2*4] = 0.8;
00274    rho[3*4] = 0.93;
00275 
00276    // create covariance matrix
00277    TMatrixD* covMat = new TMatrixD( nvar, nvar );
00278    for (Int_t ivar=0; ivar<nvar; ivar++) {
00279       (*covMat)(ivar,ivar) = dx[ivar]*dx[ivar];
00280       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00281          (*covMat)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00282          (*covMat)(jvar,ivar) = (*covMat)(ivar,jvar);
00283       }
00284    }
00285    //cout << "covariance matrix: " << endl;
00286    //covMat->Print();
00287 
00288    // produce the square-root matrix
00289    TMatrixD* sqrtMat = produceSqrtMat( *covMat );
00290 
00291    // event loop
00292    for (Int_t i=0; i<N; i++) {
00293 
00294       if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00295       getGaussRnd( *v, *sqrtMat, R );
00296 
00297       for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
00298          
00299       tree->Fill();
00300    }
00301 
00302    // write trees
00303 //   tree->Write();
00304 
00305    tree->Show(0);
00306 
00307    cout << "created tree: " << tree->GetName() << endl;
00308    return tree;
00309 }
00310 
00311 
00312 // create the data
00313 TTree* makeTree_circ(TString treeName, TString treeTitle, Int_t nvar = 2, Int_t N  = 6000, Float_t radius = 1.0, Bool_t distort = false)
00314 {
00315    Int_t Nn = 0;
00316    Float_t xvar[nvar]; //variable array size does not work in interactive mode
00317  
00318    // create signal and background trees
00319    TTree* tree = new TTree( treeName, treeTitle, 1 );   
00320    for (Int_t ivar=0; ivar<nvar; ivar++) {
00321       tree->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00322    }
00323       
00324    TRandom R( 100 );
00325    //Float_t phimin = -30, phimax = 130;
00326    Float_t phimin = -70, phimax = 130;
00327    Float_t phisig = 5;
00328    Float_t rsig = 0.1;
00329    Float_t fnmin = -(radius+4.0*rsig);
00330    Float_t fnmax = +(radius+4.0*rsig);
00331    Float_t dfn = fnmax-fnmin;
00332 
00333    // event loop
00334    for (Int_t i=0; i<N; i++) {
00335       Double_t r1=R.Rndm(),r2=R.Rndm(), r3; 
00336       r3= r1>r2? r1 :r2;
00337       Float_t phi;
00338       if (distort) phi = r3*(phimax - phimin) + phimin;
00339       else  phi = R.Rndm()*(phimax - phimin) + phimin;
00340       phi += R.Gaus()*phisig;
00341       
00342       Float_t r = radius;
00343       r += R.Gaus()*rsig;
00344 
00345       xvar[0] = r*cos(TMath::DegToRad()*phi);
00346       xvar[1] = r*sin(TMath::DegToRad()*phi);
00347 
00348       for( Int_t j = 2; j<nvar; ++j )
00349          xvar[j] = dfn*R.Rndm()+fnmin;
00350          
00351       tree->Fill();
00352    }
00353 
00354    for (Int_t i=0; i<Nn; i++) {
00355 
00356       xvar[0] = dfn*R.Rndm()+fnmin;
00357       xvar[1] = dfn*R.Rndm()+fnmin;
00358 
00359       for( Int_t j = 2; j<nvar; ++j )
00360          xvar[j] = dfn*R.Rndm()+fnmin;
00361          
00362          
00363       tree->Fill();
00364    }
00365 
00366    tree->Show(0);
00367    // write trees
00368    cout << "created tree: " << tree->GetName() << endl;
00369    return tree;
00370 }
00371 
00372 
00373 
00374 // create the data
00375 void create_lin_Nvar_2(Int_t N = 50000)
00376 {
00377    const int nvar = 4;
00378    
00379    // output flie
00380    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00381 
00382 
00383    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
00384    Float_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
00385    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00386 
00387    // create signal and background trees
00388    TTree* treeS = makeTree_lin_Nvar( "TreeS", "Signal tree", xS, dx, nvar, N );
00389    TTree* treeB = makeTree_lin_Nvar( "TreeB", "Background tree", xB, dx, nvar, N );
00390 
00391    treeS->Write();
00392    treeB->Write();
00393 
00394    treeS->Show(0);
00395    treeB->Show(0);
00396 
00397    dataFile->Close();
00398    cout << "created data file: " << dataFile->GetName() << endl;
00399 }
00400 
00401         
00402 
00403 
00404 // create the data
00405 void create_lin_Nvar(Int_t N = 50000)
00406 {
00407    const Int_t nvar = 4;
00408    Float_t xvar[nvar];
00409 
00410    // output flie
00411    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00412 
00413    // create signal and background trees
00414    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00415    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00416    for (Int_t ivar=0; ivar<nvar; ivar++) {
00417       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00418       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00419    }
00420       
00421    TRandom R( 100 );
00422    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
00423    Float_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
00424    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00425    TArrayD* v = new TArrayD( nvar );
00426    Float_t rho[20];
00427    rho[1*2] = 0.4;
00428    rho[1*3] = 0.6;
00429    rho[1*4] = 0.9;
00430    rho[2*3] = 0.7;
00431    rho[2*4] = 0.8;
00432    rho[3*4] = 0.93;
00433 
00434    // create covariance matrix
00435    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00436    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00437    for (Int_t ivar=0; ivar<nvar; ivar++) {
00438       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00439       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00440       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00441          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00442          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00443 
00444          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00445          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00446       }
00447    }
00448    cout << "signal covariance matrix: " << endl;
00449    covMatS->Print();
00450    cout << "background covariance matrix: " << endl;
00451    covMatB->Print();
00452 
00453    // produce the square-root matrix
00454    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00455    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00456 
00457    // loop over species
00458    for (Int_t itype=0; itype<2; itype++) {
00459 
00460       Float_t*  x;
00461       TMatrixD* m;
00462       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
00463       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
00464 
00465       // event loop
00466       TTree* tree = (itype==0) ? treeS : treeB;
00467       for (Int_t i=0; i<N; i++) {
00468 
00469          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00470          getGaussRnd( *v, *m, R );
00471 
00472          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
00473          
00474          tree->Fill();
00475       }
00476    }
00477 
00478    // write trees
00479    treeS->Write();
00480    treeB->Write();
00481 
00482    treeS->Show(0);
00483    treeB->Show(1);
00484 
00485    dataFile->Close();
00486    cout << "created data file: " << dataFile->GetName() << endl;
00487 }
00488 
00489 // create the category data
00490 // type = 1 (offset) or 2 (variable = -99)
00491 void create_lin_Nvar_categories(Int_t N = 10000, Int_t type = 2)  
00492 {
00493    const Int_t nvar = 4;
00494    Float_t xvar[nvar];
00495    Float_t eta;
00496 
00497    // output flie
00498    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00499 
00500    // create signal and background trees
00501    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00502    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00503    for (Int_t ivar=0; ivar<nvar; ivar++) {
00504       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00505       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00506    }
00507 
00508    // add category variable
00509    treeS->Branch( "eta", &eta, "eta/F" );
00510    treeB->Branch( "eta", &eta, "eta/F" );
00511       
00512    TRandom R( 100 );
00513    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
00514    Float_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
00515    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00516    TArrayD* v = new TArrayD( nvar );
00517    Float_t rho[20];
00518    rho[1*2] = 0.0;
00519    rho[1*3] = 0.0;
00520    rho[1*4] = 0.0;
00521    rho[2*3] = 0.0;
00522    rho[2*4] = 0.0;
00523    rho[3*4] = 0.0;
00524    if (type != 1) {
00525       rho[1*2] = 0.6;
00526       rho[1*3] = 0.7;
00527       rho[1*4] = 0.9;
00528       rho[2*3] = 0.8;
00529       rho[2*4] = 0.9;
00530       rho[3*4] = 0.93;
00531    }
00532 
00533    // create covariance matrix
00534    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00535    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00536    for (Int_t ivar=0; ivar<nvar; ivar++) {
00537       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00538       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00539       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00540          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00541          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00542 
00543          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00544          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00545       }
00546    }
00547    cout << "signal covariance matrix: " << endl;
00548    covMatS->Print();
00549    cout << "background covariance matrix: " << endl;
00550    covMatB->Print();
00551 
00552    // produce the square-root matrix
00553    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00554    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00555 
00556    // loop over species
00557    for (Int_t itype=0; itype<2; itype++) {
00558 
00559       Float_t*  x;
00560       TMatrixD* m;
00561       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
00562       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
00563 
00564       // event loop
00565       TTree* tree = (itype==0) ? treeS : treeB;
00566       for (Int_t i=0; i<N; i++) {
00567 
00568          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00569          getGaussRnd( *v, *m, R );
00570 
00571          eta = 2.5*2*(R.Rndm() - 0.5);
00572          Float_t offset = 0;
00573          if (type == 1) offset = TMath::Abs(eta) > 1.3 ? 0.8 : -0.8;
00574          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar] + offset;
00575          if (type != 1 && TMath::Abs(eta) > 1.3) xvar[nvar-1] = -5;
00576 
00577          tree->Fill();
00578       }
00579    }
00580 
00581    // write trees
00582    treeS->Write();
00583    treeB->Write();
00584 
00585    treeS->Show(0);
00586    treeB->Show(1);
00587 
00588    dataFile->Close();
00589    cout << "created data file: " << dataFile->GetName() << endl;
00590 }
00591 
00592 
00593 // create the data
00594 void create_lin_Nvar_weighted(Int_t N = 10000, int WeightedSignal=0, int WeightedBkg=1)
00595 {
00596    const Int_t nvar = 4;
00597    Float_t xvar[nvar];
00598    Float_t weight;
00599 
00600    
00601    cout << endl << endl << endl;
00602    cout << "please use .L createData.C++ if you want to run this MC geneation" <<endl;
00603    cout << "otherwise you will wait for ages!!! " << endl;
00604    cout << endl << endl << endl;
00605 
00606 
00607    // output flie
00608    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00609 
00610    // create signal and background trees
00611    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00612    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00613    for (Int_t ivar=0; ivar<nvar; ivar++) {
00614       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00615       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00616    }
00617    if (WeightedSignal) treeS->Branch( "weight", &weight,"weight/F" );
00618    if (WeightedBkg)    treeB->Branch( "weight", &weight,"weight/F" );
00619       
00620    TRandom R( 100 );
00621    Float_t xS[nvar] = {  0.2,  0.3,  0.4,  0.8 };
00622    Float_t xB[nvar] = { -0.2, -0.3, -0.4, -0.5 };
00623    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00624    TArrayD* v = new TArrayD( nvar );
00625    Float_t rho[20];
00626    rho[1*2] = 0.4;
00627    rho[1*3] = 0.6;
00628    rho[1*4] = 0.9;
00629    rho[2*3] = 0.7;
00630    rho[2*4] = 0.8;
00631    rho[3*4] = 0.93;
00632 
00633    // create covariance matrix
00634    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00635    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00636    for (Int_t ivar=0; ivar<nvar; ivar++) {
00637       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00638       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00639       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00640          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00641          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00642 
00643          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00644          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00645       }
00646    }
00647    cout << "signal covariance matrix: " << endl;
00648    covMatS->Print();
00649    cout << "background covariance matrix: " << endl;
00650    covMatB->Print();
00651 
00652    // produce the square-root matrix
00653    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00654    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00655 
00656    // loop over species
00657    for (Int_t itype=0; itype<2; itype++) {
00658 
00659       Float_t*  x;
00660       TMatrixD* m;
00661       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
00662       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
00663 
00664       // event loop
00665       TTree* tree = (itype==0) ? treeS : treeB;
00666       Int_t i=0;
00667       do {
00668          getGaussRnd( *v, *m, R );
00669 
00670          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
00671          //         for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = R.Uniform()*10.-5.;
00672          
00673          //         weight = 0.5 / (TMath::Gaus( (xvar[nvar-1]-x[nvar-1]), 0, 1.1) );
00674          // weight = TMath::Gaus(0.675,0,1) / (TMath::Gaus( (xvar[nvar-1]-x[nvar-1]), 0, 1.) );
00675          weight = 0.8 / (TMath::Gaus( ((*v)[nvar-1]), 0, 1.09) );
00676          Double_t tmp=R.Uniform()/0.00034;
00677          if (itype==0 && !WeightedSignal) {
00678             weight = 1;
00679             tree->Fill();
00680             i++;
00681          } else if (itype==1 && !WeightedBkg) {
00682             weight = 1;
00683             tree->Fill();
00684             i++;
00685          }
00686          else {
00687             if (tmp < weight){
00688                weight = 1./weight;
00689                tree->Fill();
00690                if (i%10 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00691                i++;
00692             }
00693          }
00694       } while (i<N);
00695    }
00696 
00697    // write trees
00698    treeS->Write();
00699    treeB->Write();
00700 
00701    treeS->Show(0);
00702    treeB->Show(1);
00703 
00704    TH1F *h[4];   
00705    TH1F *hw[4];
00706    for (Int_t  i=0;i<4;i++){
00707       char buffer[5];
00708       sprintf(buffer,"h%d",i);
00709       h[i]= new TH1F(buffer,"",100,-5,5);
00710       sprintf(buffer,"hw%d",i);
00711       hw[i] = new TH1F(buffer,"",100,-5,5);
00712       hw[i]->SetLineColor(3);
00713    }
00714 
00715    for (int ie=0;ie<treeS->GetEntries();ie++){
00716       treeS->GetEntry(ie);
00717       for (Int_t  i=0;i<4;i++){
00718          h[i]->Fill(xvar[i]);
00719          hw[i]->Fill(xvar[i],weight);
00720       }
00721    }
00722 
00723    TCanvas *c = new TCanvas("c","",800,800);
00724    c->Divide(2,2);
00725 
00726    for (Int_t  i=0;i<4;i++){
00727       c->cd(i+1);
00728       h[i]->Draw();
00729       hw[i]->Draw("same");
00730    }
00731 
00732 
00733    //   dataFile->Close();
00734    cout << "created data file: " << dataFile->GetName() << endl;
00735 }
00736 
00737 
00738 
00739 // create the data
00740 void create_lin_Nvar_Arr(Int_t N = 1000)
00741 {
00742    const Int_t nvar = 4;
00743    std::vector<float>* xvar[nvar];
00744 
00745    // output flie
00746    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00747 
00748    // create signal and background trees
00749    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00750    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00751    for (Int_t ivar=0; ivar<nvar; ivar++) {
00752       xvar[ivar] = new std::vector<float>();
00753       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), "vector<float>", &xvar[ivar], 64000, 1 );
00754       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), "vector<float>", &xvar[ivar], 64000, 1 );
00755    }
00756 
00757    TRandom R( 100 );
00758    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
00759    Float_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
00760    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00761    TArrayD* v = new TArrayD( nvar );
00762    Float_t rho[20];
00763    rho[1*2] = 0.4;
00764    rho[1*3] = 0.6;
00765    rho[1*4] = 0.9;
00766    rho[2*3] = 0.7;
00767    rho[2*4] = 0.8;
00768    rho[3*4] = 0.93;
00769 
00770    // create covariance matrix
00771    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00772    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00773    for (Int_t ivar=0; ivar<nvar; ivar++) {
00774       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00775       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00776       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00777          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00778          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00779 
00780          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00781          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00782       }
00783    }
00784    cout << "signal covariance matrix: " << endl;
00785    covMatS->Print();
00786    cout << "background covariance matrix: " << endl;
00787    covMatB->Print();
00788 
00789    // produce the square-root matrix
00790    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00791    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00792 
00793    // loop over species
00794    for (Int_t itype=0; itype<2; itype++) {
00795 
00796       Float_t*  x;
00797       TMatrixD* m;
00798       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
00799       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
00800 
00801       // event loop
00802       TTree* tree = (itype==0) ? treeS : treeB;
00803       for (Int_t i=0; i<N; i++) {
00804 
00805          if (i%100 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00806 
00807          Int_t aSize = (Int_t)(gRandom->Rndm()*10); // size of array varies between events
00808          for (Int_t ivar=0; ivar<nvar; ivar++) {
00809             xvar[ivar]->clear();
00810             xvar[ivar]->reserve(aSize);
00811          }
00812          for(Int_t iA = 0; iA<aSize; iA++) {
00813             //for (Int_t ivar=0; ivar<nvar; ivar++) {
00814                getGaussRnd( *v, *m, R );
00815                for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar]->push_back((*v)[ivar] + x[ivar]);
00816                //}
00817          }
00818          tree->Fill();
00819       }
00820    }
00821 
00822    // write trees
00823    treeS->Write();
00824    treeB->Write();
00825 
00826    treeS->Show(0);
00827    treeB->Show(1);
00828 
00829    dataFile->Close();
00830    cout << "created data file: " << dataFile->GetName() << endl;
00831 
00832    //plot();
00833 }
00834 
00835 
00836 
00837 // create the data
00838 void create_lin_Nvar_double()
00839 {
00840    Int_t N = 10000;
00841    const Int_t nvar = 4;
00842    Double_t xvar[nvar];
00843    Double_t xvarD[nvar];
00844    Float_t  xvarF[nvar];
00845 
00846    // output flie
00847    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00848 
00849    // create signal and background trees
00850    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00851    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00852    for (Int_t ivar=0; ivar<nvar; ivar++) {
00853       if (ivar<2) {
00854          treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvarD[ivar], TString(Form( "var%i/D", ivar+1 )).Data() );
00855          treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvarD[ivar], TString(Form( "var%i/D", ivar+1 )).Data() );
00856       }
00857       else {
00858          treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvarF[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00859          treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvarF[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00860       }
00861    }
00862       
00863    TRandom R( 100 );
00864    Double_t xS[nvar] = {  0.2,  0.3,  0.5,  0.6 };
00865    Double_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
00866    Double_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
00867    TArrayD* v = new TArrayD( nvar );
00868    Double_t rho[20];
00869    rho[1*2] = 0.4;
00870    rho[1*3] = 0.6;
00871    rho[1*4] = 0.9;
00872    rho[2*3] = 0.7;
00873    rho[2*4] = 0.8;
00874    rho[3*4] = 0.93;
00875 
00876    // create covariance matrix
00877    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00878    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00879    for (Int_t ivar=0; ivar<nvar; ivar++) {
00880       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00881       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00882       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00883          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00884          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00885 
00886          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00887          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00888       }
00889    }
00890    cout << "signal covariance matrix: " << endl;
00891    covMatS->Print();
00892    cout << "background covariance matrix: " << endl;
00893    covMatB->Print();
00894 
00895    // produce the square-root matrix
00896    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00897    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00898 
00899    // loop over species
00900    for (Int_t itype=0; itype<2; itype++) {
00901 
00902       Double_t*  x;
00903       TMatrixD* m;
00904       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
00905       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
00906 
00907       // event loop
00908       TTree* tree = (itype==0) ? treeS : treeB;
00909       for (Int_t i=0; i<N; i++) {
00910 
00911          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
00912          getGaussRnd( *v, *m, R );
00913 
00914          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
00915          for (Int_t ivar=0; ivar<nvar; ivar++) {
00916             if (ivar<2) xvarD[ivar] = xvar[ivar];
00917             else        xvarF[ivar] = xvar[ivar];
00918          }
00919          
00920          tree->Fill();
00921       }
00922    }
00923 
00924    // write trees
00925    treeS->Write();
00926    treeB->Write();
00927 
00928    treeS->Show(0);
00929    treeB->Show(1);
00930 
00931    dataFile->Close();
00932    cout << "created data file: " << dataFile->GetName() << endl;
00933 
00934    plot();
00935 }
00936 
00937 // create the data
00938 void create_lin_Nvar_discrete()
00939 {
00940    Int_t N = 10000;
00941    const Int_t nvar = 4;
00942    Float_t xvar[nvar];
00943    Int_t   xvarI[2];
00944 
00945    // output flie
00946    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
00947 
00948    // create signal and background trees
00949    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
00950    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
00951    for (Int_t ivar=0; ivar<nvar-2; ivar++) {
00952       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00953       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
00954    }
00955    for (Int_t ivar=0; ivar<2; ivar++) {
00956       treeS->Branch( TString(Form( "var%i", ivar+nvar-2+1 )).Data(), &xvarI[ivar], TString(Form( "var%i/I", ivar+nvar-2+1 )).Data() );
00957       treeB->Branch( TString(Form( "var%i", ivar+nvar-2+1 )).Data(), &xvarI[ivar], TString(Form( "var%i/I", ivar+nvar-2+1 )).Data() );
00958    }
00959       
00960    TRandom R( 100 );
00961    Float_t xS[nvar] = {  0.2,  0.3,  1,  2 };
00962    Float_t xB[nvar] = { -0.2, -0.3,  0,  0 };
00963    Float_t dx[nvar] = {  1.0,  1.0, 1, 2 };
00964    TArrayD* v = new TArrayD( nvar );
00965    Float_t rho[20];
00966    rho[1*2] = 0.4;
00967    rho[1*3] = 0.6;
00968    rho[1*4] = 0.9;
00969    rho[2*3] = 0.7;
00970    rho[2*4] = 0.8;
00971    rho[3*4] = 0.93;
00972    // no correlations
00973    for (int i=0; i<20; i++) rho[i] = 0;
00974 
00975    // create covariance matrix
00976    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
00977    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
00978    for (Int_t ivar=0; ivar<nvar; ivar++) {
00979       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
00980       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
00981       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
00982          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00983          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
00984 
00985          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
00986          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
00987       }
00988    }
00989    cout << "signal covariance matrix: " << endl;
00990    covMatS->Print();
00991    cout << "background covariance matrix: " << endl;
00992    covMatB->Print();
00993 
00994    // produce the square-root matrix
00995    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
00996    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
00997 
00998    // loop over species
00999    for (Int_t itype=0; itype<2; itype++) {
01000 
01001       Float_t*  x;
01002       TMatrixD* m;
01003       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
01004       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
01005 
01006       // event loop
01007       TTree* tree = (itype==0) ? treeS : treeB;
01008       for (Int_t i=0; i<N; i++) {
01009 
01010          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
01011          getGaussRnd( *v, *m, R );
01012 
01013          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
01014 
01015          xvarI[0] =  TMath::Nint(xvar[nvar-2]);
01016          xvarI[1] =  TMath::Nint(xvar[nvar-1]);
01017          
01018          tree->Fill();
01019       }
01020    }
01021 
01022    // write trees
01023    treeS->Write();
01024    treeB->Write();
01025 
01026    treeS->Show(0);
01027    treeB->Show(1);
01028 
01029    dataFile->Close();
01030    cout << "created data file: " << dataFile->GetName() << endl;
01031 
01032    plot();
01033 }
01034 
01035 // create the data
01036 void create_ManyVars()
01037 {
01038    Int_t N = 20000;
01039    const Int_t nvar = 20;
01040    Float_t xvar[nvar];
01041 
01042    // output flie
01043    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01044 
01045    // create signal and background trees
01046    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01047    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01048    for (Int_t ivar=0; ivar<nvar; ivar++) {
01049       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01050       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01051    }
01052       
01053    Float_t xS[nvar];
01054    Float_t xB[nvar];
01055    Float_t dx[nvar];
01056    for (Int_t ivar=0; ivar<nvar; ivar++) {
01057       xS[ivar] = 0 + ivar*0.05;
01058       xB[ivar] = 0 - ivar*0.05;
01059       dx[ivar] = 1;
01060    }
01061 
01062    xS[0] =   0.2;
01063    xB[0] =  -0.2;
01064    dx[0] =   1.0;
01065    xS[1] =   0.3;
01066    xB[1] =  -0.3;
01067    dx[1] =   1.0;
01068    xS[2] =   0.4;
01069    xB[2] =  -0.4;
01070    dx[2] =  1.0 ;
01071    xS[3] =   0.8 ;
01072    xB[3] =  -0.5 ;
01073    dx[3] =   1.0 ;
01074 //   TArrayD* v = new TArrayD( nvar );
01075    Float_t rho[20];
01076    rho[1*2] = 0.4;
01077    rho[1*3] = 0.6;
01078    rho[1*4] = 0.9;
01079    rho[2*3] = 0.7;
01080    rho[2*4] = 0.8;
01081    rho[3*4] = 0.93;
01082 
01083    TRandom R( 100 );
01084 
01085    // loop over species
01086    for (Int_t itype=0; itype<2; itype++) {
01087 
01088       Float_t* x = (itype == 0) ? xS : xB; 
01089 
01090       // event loop
01091       TTree* tree = (itype == 0) ? treeS : treeB;
01092       for (Int_t i=0; i<N; i++) {
01093 
01094          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
01095          for (Int_t ivar=0; ivar<nvar; ivar++) {
01096             if (ivar == 1500 && itype!=10) xvar[ivar] = 1;
01097             else                           xvar[ivar] = x[ivar] + R.Gaus()*dx[ivar];
01098          }
01099          
01100          tree->Fill();
01101       }
01102    }
01103 
01104    // write trees
01105    treeS->Write();
01106    treeB->Write();
01107 
01108    treeS->Show(0);
01109    treeB->Show(1);
01110 
01111    dataFile->Close();
01112    plot();
01113    cout << "created data file: " << dataFile->GetName() << endl;
01114 }
01115 
01116 // create the data
01117 void create_lin_NvarObsolete()
01118 {
01119    Int_t N = 20000;
01120    const Int_t nvar = 20;
01121    Float_t xvar[nvar];
01122 
01123    // output flie
01124    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01125 
01126    // create signal and background trees
01127    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01128    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01129    for (Int_t ivar=0; ivar<nvar; ivar++) {
01130       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01131       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01132    }
01133       
01134    TRandom R( 100 );
01135    Float_t xS[nvar] = {  0.5,  0.5,  0.0,  0.0,  0.0,  0.0 };
01136    Float_t xB[nvar] = { -0.5, -0.5, -0.0, -0.0, -0.0, -0.0 };
01137    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0, 1.0, 1.0 };
01138    TArrayD* v = new TArrayD( nvar );
01139    Float_t rho[50];
01140    for (Int_t i=0; i<50; i++) rho[i] = 0;
01141    rho[1*2] = 0.3;
01142    rho[1*3] = 0.0;
01143    rho[1*4] = 0.0;
01144    rho[2*3] = 0.0;
01145    rho[2*4] = 0.0;
01146    rho[3*4] = 0.0;
01147 
01148    // create covariance matrix
01149    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
01150    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
01151    for (Int_t ivar=0; ivar<nvar; ivar++) {
01152       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
01153       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
01154       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
01155          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
01156          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
01157 
01158          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
01159          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
01160       }
01161    }
01162    cout << "signal covariance matrix: " << endl;
01163    covMatS->Print();
01164    cout << "background covariance matrix: " << endl;
01165    covMatB->Print();
01166 
01167    // produce the square-root matrix
01168    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
01169    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
01170 
01171    // loop over species
01172    for (Int_t itype=0; itype<2; itype++) {
01173 
01174       Float_t*  x;
01175       TMatrixD* m;
01176       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
01177       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
01178 
01179       // event loop
01180       TTree* tree = (itype==0) ? treeS : treeB;
01181       for (Int_t i=0; i<N; i++) {
01182 
01183          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
01184          getGaussRnd( *v, *m, R );
01185 
01186          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
01187          
01188          tree->Fill();
01189       }
01190    }
01191 
01192    // write trees
01193    treeS->Write();
01194    treeB->Write();
01195 
01196    treeS->Show(0);
01197    treeB->Show(1);
01198 
01199    dataFile->Close();
01200    cout << "created data file: " << dataFile->GetName() << endl;
01201 
01202    plot();
01203 }
01204 
01205 // create the data
01206 void create_lin(Int_t N = 2000)
01207 {
01208    const Int_t nvar = 2;
01209    Double_t xvar[nvar];
01210    Float_t weight;
01211 
01212    // output flie
01213    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01214 
01215    // create signal and background trees
01216    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01217    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01218    for (Int_t ivar=0; ivar<nvar; ivar++) {
01219       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/D", ivar )).Data() );
01220       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/D", ivar )).Data() );
01221    }
01222    treeS->Branch( "weight", &weight, "weight/F" );
01223    treeB->Branch( "weight", &weight, "weight/F" );
01224       
01225    TRandom R( 100 );
01226    Float_t xS[nvar] = {  0.0,  0.0 };
01227    Float_t xB[nvar] = { -0.0, -0.0 };
01228    Float_t dx[nvar] = {  1.0,  1.0 };
01229    TArrayD* v = new TArrayD( 2 );
01230    Float_t rhoS =  0.21;
01231    Float_t rhoB =  0.0;
01232 
01233    // create covariance matrix
01234    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
01235    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
01236    for (Int_t ivar=0; ivar<nvar; ivar++) {
01237       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
01238       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
01239       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
01240          (*covMatS)(ivar,jvar) = rhoS*dx[ivar]*dx[jvar];
01241          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
01242 
01243          (*covMatB)(ivar,jvar) = rhoB*dx[ivar]*dx[jvar];
01244          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
01245       }
01246    }
01247    cout << "signal covariance matrix: " << endl;
01248    covMatS->Print();
01249    cout << "background covariance matrix: " << endl;
01250    covMatB->Print();
01251 
01252    // produce the square-root matrix
01253    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
01254    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
01255 
01256    // loop over species
01257    for (Int_t itype=0; itype<2; itype++) {
01258 
01259       Float_t*  x;
01260       TMatrixD* m;
01261       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
01262       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
01263 
01264       // event loop
01265       TTree* tree = (itype==0) ? treeS : treeB;
01266       for (Int_t i=0; i<N; i++) {
01267 
01268          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
01269          getGaussRnd( *v, *m, R );
01270          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
01271 
01272          // add weights
01273          if (itype == 0) weight = 1.0; // this is the signal weight
01274          else            weight = 2.0; // this is the background weight
01275          
01276          tree->Fill();
01277       }
01278    }
01279 
01280    // write trees
01281    treeS->Write();
01282    treeB->Write();
01283 
01284    treeS->Show(0);
01285    treeB->Show(1);
01286 
01287    dataFile->Close();
01288    cout << "created data file: " << dataFile->GetName() << endl;
01289 
01290    plot();
01291 }
01292 
01293 void create_fullcirc(Int_t nmax  = 20000,  Bool_t distort=false)
01294 {
01295   TFile* dataFile = TFile::Open( "circledata.root", "RECREATE" );
01296    int nvar = 2;
01297    int nsig = 0, nbgd=0;
01298    Float_t weight=1;
01299    Float_t xvar[100];
01300    // create signal and background trees
01301    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );
01302    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );
01303    for (Int_t ivar=0; ivar<nvar; ivar++) {
01304       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
01305       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
01306    }
01307    treeS->Branch("weight", &weight, "weight/F");
01308    treeB->Branch("weight", &weight, "weight/F");
01309 
01310    TRandom R( 100 );
01311    do {
01312       for (Int_t ivar=0; ivar<nvar; ivar++) { xvar[ivar]=2.*R.Rndm()-1.;}
01313       Float_t xout = xvar[0]*xvar[0]+xvar[1]*xvar[1];
01314       if (nsig<10) cout << "xout = " << xout<<endl;
01315       if (xout < 0.3  || (xout >0.3 && xout<0.5 && R.Rndm() > xout)) {
01316          if (distort && xvar[0] < 0 && R.Rndm()>0.1) continue; 
01317          treeS->Fill();
01318          nsig++;
01319       }
01320       else {
01321          if (distort && xvar[0] > 0 && R.Rndm()>0.1) continue; 
01322          treeB->Fill();
01323          nbgd++;
01324       }
01325    } while ( nsig < nmax || nbgd < nmax);
01326 
01327    dataFile->Write();
01328    dataFile->Close();
01329    
01330 } 
01331 
01332 // create the data
01333 void create_circ(Int_t N  = 6000, Bool_t distort = false)
01334 {
01335    Int_t Nn = 0;
01336    const Int_t nvar = 2;
01337    Float_t xvar[nvar];
01338 
01339    // output flie
01340    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01341 
01342    // create signal and background trees
01343    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01344    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01345    for (Int_t ivar=0; ivar<nvar; ivar++) {
01346       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01347       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01348    }
01349 //    TTree *treeB  = treeS->CloneTree();
01350 //    for (Int_t ivar=0; ivar<nvar; ivar++) {
01351 //       treeS->SetBranchAddress( Form( "var%i", ivar ), &xvar[ivar] );
01352 //       treeB->SetBranchAddress( Form( "var%i", ivar ), &xvar[ivar] );
01353 //    }
01354 //    treeB->SetName ( "TreeB" );
01355 //    treeB->SetTitle( "TreeB" );
01356       
01357    TRandom R( 100 );
01358    //Float_t phimin = -30, phimax = 130;
01359    Float_t phimin = -70, phimax = 130;
01360    Float_t phisig = 5;
01361    Float_t rS = 1.1;
01362    Float_t rB = 0.75;
01363    Float_t rsig = 0.1;
01364    Float_t fnmin = -(rS+4.0*rsig);
01365    Float_t fnmax = +(rS+4.0*rsig);
01366    Float_t dfn = fnmax-fnmin;
01367    // loop over species
01368    for (Int_t itype=0; itype<2; itype++) {
01369 
01370       // event loop
01371       TTree* tree = (itype==0) ? treeS : treeB;
01372       for (Int_t i=0; i<N; i++) {
01373          Double_t r1=R.Rndm(),r2=R.Rndm(), r3; 
01374          if (itype==0) r3= r1>r2? r1 :r2;
01375          else r3= r2;
01376          Float_t phi;
01377          if (distort) phi = r3*(phimax - phimin) + phimin;
01378          else  phi = R.Rndm()*(phimax - phimin) + phimin;
01379          phi += R.Gaus()*phisig;
01380       
01381          Float_t r = (itype==0) ? rS : rB;
01382          r += R.Gaus()*rsig;
01383 
01384          xvar[0] = r*cos(TMath::DegToRad()*phi);
01385          xvar[1] = r*sin(TMath::DegToRad()*phi);
01386          
01387          tree->Fill();
01388       }
01389 
01390       for (Int_t i=0; i<Nn; i++) {
01391 
01392          xvar[0] = dfn*R.Rndm()+fnmin;
01393          xvar[1] = dfn*R.Rndm()+fnmin;
01394          
01395          tree->Fill();
01396       }
01397    }
01398 
01399    // write trees
01400    treeS->Write();
01401    treeB->Write();
01402 
01403    treeS->Show(0);
01404    treeB->Show(1);
01405 
01406    dataFile->Close();
01407    cout << "created data file: " << dataFile->GetName() << endl;
01408 
01409    plot();
01410 }
01411 
01412 
01413 void create_schachbrett(Int_t nEvents = 20000) {
01414 
01415    const Int_t nvar = 2;
01416    Float_t xvar[nvar];
01417 
01418    // output flie
01419    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01420 
01421    // create signal and background trees
01422    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01423    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01424    for (Int_t ivar=0; ivar<nvar; ivar++) {
01425       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01426       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01427    }
01428 
01429    Int_t   nSeed   = 12345;
01430    TRandom *m_rand = new TRandom(nSeed);
01431    Double_t sigma=0.3;
01432    Double_t meanX;
01433    Double_t meanY;
01434    Int_t xtype=1, ytype=1;
01435    Int_t iev=0;
01436    Int_t m_nDim = 2; // actually the boundary, there is a "bump" for every interger value
01437                      // between in the Inteval [-m_nDim,m_nDim]
01438    while (iev < nEvents){
01439       xtype=1;
01440       for (Int_t i=-m_nDim; i <=  m_nDim; i++){
01441          ytype  =  1;
01442          for (Int_t j=-m_nDim; j <=  m_nDim; j++){
01443             meanX=Double_t(i);
01444             meanY=Double_t(j);
01445             xvar[0]=m_rand->Gaus(meanY,sigma);
01446             xvar[1]=m_rand->Gaus(meanX,sigma);
01447             Int_t type   = xtype*ytype;
01448             TTree* tree = (type==1) ? treeS : treeB;
01449             tree->Fill();
01450             iev++;
01451             ytype *= -1;
01452          }
01453          xtype *= -1;
01454       }
01455    }
01456 
01457 
01458    // write trees
01459    treeS->Write();
01460    treeB->Write();
01461 
01462    treeS->Show(0);
01463    treeB->Show(1);
01464 
01465    dataFile->Close();
01466    cout << "created data file: " << dataFile->GetName() << endl;
01467 
01468    plot();
01469 
01470 }
01471 
01472 
01473 void create_schachbrett_5D(Int_t nEvents = 200000) {
01474    const Int_t nvar = 5;
01475    Float_t xvar[nvar];
01476 
01477    // output flie
01478    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01479 
01480    // create signal and background trees
01481    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01482    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01483    for (Int_t ivar=0; ivar<nvar; ivar++) {
01484       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01485       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01486    }
01487 
01488    Int_t   nSeed   = 12345;
01489    TRandom *m_rand = new TRandom(nSeed);
01490    Double_t sigma=0.3;
01491    Int_t itype[nvar];
01492    Int_t iev=0;
01493    Int_t m_nDim = 2; // actually the boundary, there is a "bump" for every interger value
01494                      // between in the Inteval [-m_nDim,m_nDim]
01495 
01496    int idx[nvar];
01497    while (iev < nEvents){
01498       itype[0]=1;
01499       for (idx[0]=-m_nDim; idx[0] <=  m_nDim; idx[0]++){
01500          itype[1]=1;
01501          for (idx[1]=-m_nDim; idx[1] <=  m_nDim; idx[1]++){
01502             itype[2]=1;
01503             for (idx[2]=-m_nDim; idx[2] <=  m_nDim; idx[2]++){
01504                itype[3]=1;
01505                for (idx[3]=-m_nDim; idx[3] <=  m_nDim; idx[3]++){
01506                   itype[4]=1;
01507                   for (idx[4]=-m_nDim; idx[4] <=  m_nDim; idx[4]++){
01508                      Int_t type   = itype[0]; 
01509                      for (Int_t i=0;i<nvar;i++){
01510                         xvar[i]=m_rand->Gaus(Double_t(idx[i]),sigma);
01511                         if (i>0) type *= itype[i];
01512                      }
01513                      TTree* tree = (type==1) ? treeS : treeB;
01514                      tree->Fill();
01515                      iev++;
01516                      itype[4] *= -1;
01517                   }
01518                   itype[3] *= -1;
01519                }
01520                itype[2] *= -1;
01521             }
01522             itype[1] *= -1;
01523          }
01524          itype[0] *= -1;
01525       }
01526    }
01527             
01528    // write trees
01529    treeS->Write();
01530    treeB->Write();
01531 
01532    treeS->Show(0);
01533    treeB->Show(1);
01534 
01535    dataFile->Close();
01536    cout << "created data file: " << dataFile->GetName() << endl;
01537 
01538    plot();
01539 
01540 }
01541 
01542 
01543 void create_schachbrett_4D(Int_t nEvents = 200000) {
01544 
01545    const Int_t nvar = 4;
01546    Float_t xvar[nvar];
01547 
01548    // output flie
01549    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01550 
01551    // create signal and background trees
01552    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01553    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01554    for (Int_t ivar=0; ivar<nvar; ivar++) {
01555       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01556       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01557    }
01558 
01559    Int_t   nSeed   = 12345;
01560    TRandom *m_rand = new TRandom(nSeed);
01561    Double_t sigma=0.3;
01562    Int_t itype[nvar];
01563    Int_t iev=0;
01564    Int_t m_nDim = 2; // actually the boundary, there is a "bump" for every interger value
01565                      // between in the Inteval [-m_nDim,m_nDim]
01566 
01567    int idx[nvar];
01568    while (iev < nEvents){
01569       itype[0]=1;
01570       for (idx[0]=-m_nDim; idx[0] <=  m_nDim; idx[0]++){
01571          itype[1]=1;
01572          for (idx[1]=-m_nDim; idx[1] <=  m_nDim; idx[1]++){
01573             itype[2]=1;
01574             for (idx[2]=-m_nDim; idx[2] <=  m_nDim; idx[2]++){
01575                itype[3]=1;
01576                for (idx[3]=-m_nDim; idx[3] <=  m_nDim; idx[3]++){
01577                   Int_t type   = itype[0]; 
01578                   for (Int_t i=0;i<nvar;i++){
01579                      xvar[i]=m_rand->Gaus(Double_t(idx[i]),sigma);
01580                      if (i>0) type *= itype[i];
01581                   }
01582                   TTree* tree = (type==1) ? treeS : treeB;
01583                   tree->Fill();
01584                   iev++;
01585                   itype[3] *= -1;
01586                }
01587                itype[2] *= -1;
01588             }
01589             itype[1] *= -1;
01590          }
01591          itype[0] *= -1;
01592       }
01593    }
01594 
01595    // write trees
01596    treeS->Write();
01597    treeB->Write();
01598 
01599    treeS->Show(0);
01600    treeB->Show(1);
01601 
01602    dataFile->Close();
01603    cout << "created data file: " << dataFile->GetName() << endl;
01604 
01605    plot();
01606 
01607 }
01608 
01609 
01610 void create_schachbrett_3D(Int_t nEvents = 20000) {
01611 
01612    const Int_t nvar = 3;
01613    Float_t xvar[nvar];
01614 
01615    // output flie
01616    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01617 
01618    // create signal and background trees
01619    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01620    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01621    for (Int_t ivar=0; ivar<nvar; ivar++) {
01622       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01623       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01624    }
01625 
01626    Int_t   nSeed   = 12345;
01627    TRandom *m_rand = new TRandom(nSeed);
01628    Double_t sigma=0.3;
01629    Int_t itype[nvar];
01630    Int_t iev=0;
01631    Int_t m_nDim = 2; // actually the boundary, there is a "bump" for every interger value
01632                      // between in the Inteval [-m_nDim,m_nDim]
01633 
01634    int idx[nvar];
01635    while (iev < nEvents){
01636       itype[0]=1;
01637       for (idx[0]=-m_nDim; idx[0] <=  m_nDim; idx[0]++){
01638          itype[1]=1;
01639          for (idx[1]=-m_nDim; idx[1] <=  m_nDim; idx[1]++){
01640             itype[2]=1;
01641             for (idx[2]=-m_nDim; idx[2] <=  m_nDim; idx[2]++){
01642                Int_t type   = itype[0]; 
01643                for (Int_t i=0;i<nvar;i++){
01644                   xvar[i]=m_rand->Gaus(Double_t(idx[i]),sigma);
01645                   if (i>0) type *= itype[i];
01646                }
01647                TTree* tree = (type==1) ? treeS : treeB;
01648                tree->Fill();
01649                iev++;
01650                itype[2] *= -1;
01651             }
01652             itype[1] *= -1;
01653          }
01654          itype[0] *= -1;
01655       }
01656    }
01657 
01658    // write trees
01659    treeS->Write();
01660    treeB->Write();
01661 
01662    treeS->Show(0);
01663    treeB->Show(1);
01664 
01665    dataFile->Close();
01666    cout << "created data file: " << dataFile->GetName() << endl;
01667 
01668    plot();
01669 
01670 }
01671 
01672 
01673 void create_schachbrett_2D(Int_t nEvents = 100000, Int_t nbumps=2) {
01674 
01675    const Int_t nvar = 2;
01676    Float_t xvar[nvar];
01677 
01678    // output flie
01679    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01680 
01681    // create signal and background trees
01682    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01683    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01684    for (Int_t ivar=0; ivar<nvar; ivar++) {
01685       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01686       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01687    }
01688 
01689    Int_t   nSeed   = 345;
01690    TRandom *m_rand = new TRandom(nSeed);
01691    Double_t sigma=0.35;
01692    Int_t itype[nvar];
01693    Int_t iev=0;
01694    Int_t m_nDim = nbumps; // actually the boundary, there is a "bump" for every interger value
01695                      // between in the Inteval [-m_nDim,m_nDim]
01696 
01697    int idx[nvar];
01698    while (iev < nEvents){
01699       itype[0]=1;
01700       for (idx[0]=-m_nDim; idx[0] <=  m_nDim; idx[0]++){
01701          itype[1]=1;
01702          for (idx[1]=-m_nDim; idx[1] <=  m_nDim; idx[1]++){
01703             Int_t type   = itype[0]; 
01704             for (Int_t i=0;i<nvar;i++){
01705                xvar[i]=m_rand->Gaus(Double_t(idx[i]),sigma);
01706                if (i>0) type *= itype[i];
01707             }
01708             TTree* tree = (type==1) ? treeS : treeB;
01709             tree->Fill();
01710             iev++;
01711             itype[1] *= -1;
01712          }
01713          itype[0] *= -1;
01714       }
01715    }
01716    
01717    // write trees
01718    treeS->Write();
01719    treeB->Write();
01720 
01721    treeS->Show(0);
01722    treeB->Show(1);
01723 
01724    dataFile->Close();
01725    cout << "created data file: " << dataFile->GetName() << endl;
01726 
01727    plot();
01728 
01729 }
01730 
01731 
01732 
01733 void create_3Bumps(Int_t nEvents = 5000) {
01734    // signal is clustered around (1,0) and (-1,0) where one is two times(1,0) 
01735    // bkg                        (0,0)
01736    
01737 
01738 
01739    const Int_t nvar = 2;
01740    Float_t xvar[nvar];
01741 
01742    // output flie
01743    TString filename = "data_3Bumps.root";
01744    TFile* dataFile = TFile::Open( filename, "RECREATE" );
01745 
01746    // create signal and background trees
01747    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01748    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01749    for (Int_t ivar=0; ivar<nvar; ivar++) {
01750       treeS->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01751       treeB->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar )).Data() );
01752    }
01753 
01754    Int_t   nSeed   = 12345;
01755    TRandom *m_rand = new TRandom(nSeed);
01756    Double_t sigma=0.2;
01757    Int_t type;
01758    Int_t iev=0;
01759    Double_t Centers[nvar][6] = {{-1,0,0,0,1,1},{0,0,0,0,0,0}}; // 
01760 
01761 
01762    while (iev < nEvents){
01763       for (int idx=0; idx<6; idx++){
01764          if (idx==1 || idx==2 || idx==3) type = 0;
01765          else type=1;
01766          for (Int_t ivar=0;ivar<nvar;ivar++){
01767             xvar[ivar]=m_rand->Gaus(Centers[ivar][idx],sigma);
01768          }
01769          TTree* tree = (type==1) ? treeS : treeB;
01770          tree->Fill();
01771          iev++;
01772       }
01773    }
01774    
01775    // write trees
01776    treeS->Write();
01777    treeB->Write();
01778 
01779    treeS->Show(0);
01780    treeB->Show(1);
01781 
01782    dataFile->Close();
01783    cout << "created data file: " << dataFile->GetName() << endl;
01784 
01785    plot(filename);
01786 
01787 }
01788 
01789 void createOnionData(Int_t nmax = 50000){
01790    // output file
01791    TFile* dataFile = TFile::Open( "oniondata.root", "RECREATE" );
01792    int nvar = 4;
01793    int nsig = 0, nbgd=0;
01794    Float_t xvar[100];
01795    // create signal and background trees
01796    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );
01797    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );
01798    for (Int_t ivar=0; ivar<nvar; ivar++) {
01799       treeS->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
01800       treeB->Branch( TString(Form( "var%i", ivar+1 )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar+1 )).Data() );
01801    }
01802    
01803    TRandom R( 100 );
01804    do {
01805       for (Int_t ivar=0; ivar<nvar; ivar++) { xvar[ivar]=R.Rndm();}
01806       Float_t xout = sin(2.*acos(-1.)*(xvar[0]*xvar[1]*xvar[2]*xvar[3]+xvar[0]*xvar[1]));
01807       if (nsig<100) cout << "xout = " << xout<<endl;
01808       Int_t i = (Int_t) ((1.+xout)*4.99);
01809       if (i%2 == 0 && nsig < nmax) {
01810          treeS->Fill();
01811          nsig++;
01812       }
01813       if (i%2 != 0 && nbgd < nmax){
01814          treeB->Fill();
01815          nbgd++;
01816       }
01817    } while ( nsig < nmax || nbgd < nmax);
01818 
01819    dataFile->Write();
01820    dataFile->Close();
01821 }
01822 
01823 void create_multiclassdata(Int_t nmax  = 20000)
01824 {
01825    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01826    int ncls = 3;
01827    int nvar = 4;
01828    int ndat = 0;
01829    Int_t cls;
01830    Float_t thecls;
01831    Float_t weight=1;
01832    Float_t xcls[100];
01833    Float_t xmean[3][4] = {
01834       { 0.   ,  0.3,  0.5, 0.9 }, 
01835       { -0.2 , -0.3,  0.5, 0.4 }, 
01836       { 0.2  ,  0.1, -0.1, 0.7 }} ;
01837 
01838    Float_t xvar[100];
01839    // create tree using class flag stored in int variable cls
01840    TTree* treeR = new TTree( "TreeR", "TreeR", 1 );
01841    for (Int_t ivar=0; ivar<nvar; ivar++) {
01842       treeR->Branch( TString(Form( "var%i", ivar )).Data(), &xvar[ivar], TString(Form( "var%i/F", ivar)).Data() );
01843    }
01844    for (Int_t icls=0; icls<ncls; icls++) {
01845       treeR->Branch(TString(Form( "cls%i", icls )).Data(), &xcls[icls], TString(Form( "cls%i/F", icls)).Data() );
01846    }
01847 
01848    treeR->Branch("cls", &thecls, "cls/F");
01849    treeR->Branch("weight", &weight, "weight/F");
01850    
01851    TRandom R( 100 );
01852    do {
01853       for (Int_t icls=0; icls<ncls; icls++) xcls[icls]=0.;
01854       cls = R.Integer(ncls);
01855       thecls = cls;
01856       xcls[cls]=1.;
01857       for (Int_t ivar=0; ivar<nvar; ivar++) { 
01858          xvar[ivar]=R.Gaus(xmean[cls][ivar],1.);
01859       }
01860       
01861       if (ndat<30) cout << "cls=" << cls <<" xvar = " << xvar[0]<<" " <<xvar[1]<<" " << xvar[2]<<" " <<xvar[3]<<endl;
01862       
01863       treeR->Fill();
01864       ndat++;
01865    } while ( ndat < nmax );
01866 
01867    dataFile->Write();
01868    dataFile->Close();
01869    
01870 } 
01871 
01872 
01873 
01874 
01875 
01876 
01877 // create the data
01878 void create_array_with_different_lengths(Int_t N = 100)
01879 {
01880    const Int_t nvar = 4;
01881    Int_t nvarCurrent = 4;
01882    Float_t xvar[nvar];
01883 
01884    // output flie
01885    TFile* dataFile = TFile::Open( "data.root", "RECREATE" );
01886 
01887    // create signal and background trees
01888    TTree* treeS = new TTree( "TreeS", "TreeS", 1 );   
01889    TTree* treeB = new TTree( "TreeB", "TreeB", 1 );   
01890    treeS->Branch( "arrSize", &nvarCurrent, "arrSize/I" );
01891    treeS->Branch( "arr", xvar, "arr[arrSize]/F" );
01892    treeB->Branch( "arrSize", &nvarCurrent, "arrSize/I" );
01893    treeB->Branch( "arr", xvar, "arr[arrSize]/F" );
01894       
01895    TRandom R( 100 );
01896    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
01897    Float_t xB[nvar] = { -0.2, -0.3, -0.5, -0.6 };
01898    Float_t dx[nvar] = {  1.0,  1.0, 1.0, 1.0 };
01899    TArrayD* v = new TArrayD( nvar );
01900    Float_t rho[20];
01901    rho[1*2] = 0.4;
01902    rho[1*3] = 0.6;
01903    rho[1*4] = 0.9;
01904    rho[2*3] = 0.7;
01905    rho[2*4] = 0.8;
01906    rho[3*4] = 0.93;
01907 
01908    // create covariance matrix
01909    TMatrixD* covMatS = new TMatrixD( nvar, nvar );
01910    TMatrixD* covMatB = new TMatrixD( nvar, nvar );
01911    for (Int_t ivar=0; ivar<nvar; ivar++) {
01912       (*covMatS)(ivar,ivar) = dx[ivar]*dx[ivar];
01913       (*covMatB)(ivar,ivar) = dx[ivar]*dx[ivar];
01914       for (Int_t jvar=ivar+1; jvar<nvar; jvar++) {
01915          (*covMatS)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
01916          (*covMatS)(jvar,ivar) = (*covMatS)(ivar,jvar);
01917 
01918          (*covMatB)(ivar,jvar) = rho[(ivar+1)*(jvar+1)]*dx[ivar]*dx[jvar];
01919          (*covMatB)(jvar,ivar) = (*covMatB)(ivar,jvar);
01920       }
01921    }
01922    cout << "signal covariance matrix: " << endl;
01923    covMatS->Print();
01924    cout << "background covariance matrix: " << endl;
01925    covMatB->Print();
01926 
01927    // produce the square-root matrix
01928    TMatrixD* sqrtMatS = produceSqrtMat( *covMatS );
01929    TMatrixD* sqrtMatB = produceSqrtMat( *covMatB );
01930 
01931    // loop over species
01932    for (Int_t itype=0; itype<2; itype++) {
01933 
01934       Float_t*  x;
01935       TMatrixD* m;
01936       if (itype == 0) { x = xS; m = sqrtMatS; cout << "- produce signal" << endl; }
01937       else            { x = xB; m = sqrtMatB; cout << "- produce background" << endl; }
01938 
01939       // event loop
01940       TTree* tree = (itype==0) ? treeS : treeB;
01941       for (Int_t i=0; i<N; i++) {
01942 
01943          if (i%1000 == 0) cout << "... event: " << i << " (" << N << ")" << endl;
01944          getGaussRnd( *v, *m, R );
01945 
01946          for (Int_t ivar=0; ivar<nvar; ivar++) xvar[ivar] = (*v)[ivar] + x[ivar];
01947          
01948 
01949          nvarCurrent = (i%4)+1;
01950 
01951          tree->Fill();
01952       }
01953    }
01954 
01955    // write trees
01956    treeS->Write();
01957    treeB->Write();
01958 
01959    treeS->Show(0);
01960    treeB->Show(1);
01961 
01962    dataFile->Close();
01963    cout << "created data file: " << dataFile->GetName() << endl;
01964 }
01965 
01966 
01967 
01968 // create the data
01969 void create_MultipleBackground(Int_t N = 50000)
01970 {
01971    const int nvar = 4;
01972    
01973    // output flie
01974    TFile* dataFile = TFile::Open( "tmva_example_multiple_background.root", "RECREATE" );
01975 
01976 
01977    Float_t xS[nvar] = {  0.2,  0.3,  0.5,  0.9 };
01978    Float_t xB0[nvar] = { -0.2, -0.3, -0.5, -0.6 };
01979    Float_t xB1[nvar] = { -0.2, 0.3, 0.5, -0.6 };
01980    Float_t dx0[nvar] = {  1.0,  1.0, 1.0, 1.0 };
01981    Float_t dx1[nvar] = {  -1.0,  -1.0, -1.0, -1.0 };
01982 
01983    // create signal and background trees
01984    TTree* treeS = makeTree_lin_Nvar( "TreeS", "Signal tree", xS, dx0, nvar, N );
01985    TTree* treeB0 = makeTree_lin_Nvar( "TreeB0", "Background 0", xB0, dx0, nvar, N );
01986    TTree* treeB1 = makeTree_lin_Nvar( "TreeB1", "Background 1", xB1, dx1, nvar, N );
01987    TTree* treeB2 = makeTree_circ( "TreeB2", "Background 2", nvar, N, 1.5, true);
01988 
01989    treeS->Write();
01990    treeB0->Write();
01991    treeB1->Write();
01992    treeB2->Write();
01993 
01994    //treeS->Show(0);
01995    //treeB0->Show(0);
01996    //treeB1->Show(0);
01997    //treeB2->Show(0);
01998 
01999    dataFile->Close();
02000    cout << "created data file: " << dataFile->GetName() << endl;
02001 }

Generated on Tue Jul 5 15:25:42 2011 for ROOT_528-00b_version by  doxygen 1.5.1