threadsh2.C

Go to the documentation of this file.
00001 //+ Example of a simple script creating 2 threads each with one canvas.
00002 // This script can only be executed via ACliC .x threadsh2.C++.
00003 // The canvases are saved in a animated gif file.
00004 
00005 #include "TROOT.h"
00006 #include "TCanvas.h"
00007 #include "TFrame.h"
00008 #include "TH1F.h"
00009 #include "TRandom.h"
00010 #include "TThread.h"
00011 #include "TMethodCall.h"
00012 
00013 TH1F    *hpx, *total, *hmain, *s1, *s2;
00014 TThread *thread1, *thread2, *threadj;
00015 Bool_t   finished;
00016 
00017 void *handle1(void *)
00018 {
00019    int nfills = 10000;
00020    int upd = 500;
00021 
00022    TCanvas *c0 = new TCanvas("c0","Dynamic Filling Example", 100, 30, 400, 300);
00023    c0->SetFillColor(42);
00024    c0->GetFrame()->SetFillColor(21);
00025    c0->GetFrame()->SetBorderSize(6);
00026    c0->GetFrame()->SetBorderMode(-1);
00027 
00028    TThread::Lock();
00029    hpx = new TH1F("hpx", "This is the px distribution", 100, -4, 4);
00030    hpx->SetFillColor(48);
00031    TThread::UnLock();
00032    Float_t px, py, pz;
00033    gRandom->SetSeed();
00034    for (Int_t i = 0; i < nfills; i++) {
00035       gRandom->Rannor(px, py);
00036       pz = px*px + py*py;
00037       hpx->Fill(px);
00038       if (i && (i%upd) == 0) {
00039          if (i == upd) {
00040             c0->cd();
00041             hpx->Draw();
00042          }
00043          c0->Modified();
00044          c0->Update();
00045          gSystem->Sleep(10);
00046          TMethodCall c(c0->IsA(), "Print", "");
00047          void *arr[4];
00048          arr[1] = &c;
00049          arr[2] = (void *)c0;
00050          arr[3] = (void*)"\"hpxanim.gif+50\"";
00051          (*gThreadXAR)("METH", 4, arr, NULL);
00052       }
00053    }
00054    c0->Modified();
00055    c0->Update();
00056    TMethodCall c(c0->IsA(), "Print", "");
00057    void *arr[4];
00058    arr[1] = &c;
00059    arr[2] = (void *)c0;
00060    arr[3] = (void*)"\"hpxanim.gif++\"";
00061    (*gThreadXAR)("METH", 4, arr, NULL);
00062    return 0;
00063 }
00064 
00065 void *handle2(void *)
00066 {
00067    int nfills = 10000;
00068    int upd = 500;
00069 
00070    TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example", 515, 30, 400, 300);
00071    c1->SetGrid();
00072 
00073    TThread::Lock();
00074    total  = new TH1F("total","This is the total distribution",100,-4,4);
00075    hmain  = new TH1F("hmain","Main contributor",100,-4,4);
00076    s1     = new TH1F("s1","This is the first signal",100,-4,4);
00077    s2     = new TH1F("s2","This is the second signal",100,-4,4);
00078    total->Sumw2();   // this makes sure that the sum of squares of weights will be stored
00079    total->SetMarkerStyle(21);
00080    total->SetMarkerSize(0.7);
00081    hmain->SetFillColor(16);
00082    s1->SetFillColor(42);
00083    s2->SetFillColor(46);
00084    TThread::UnLock();
00085    Float_t xs1, xs2, xmain;
00086    gRandom->SetSeed();
00087    for (Int_t i = 0; i < nfills; i++) {
00088       xmain = gRandom->Gaus(-1,1.5);
00089       xs1   = gRandom->Gaus(-0.5,0.5);
00090       xs2   = gRandom->Landau(1,0.15);
00091       hmain->Fill(xmain);
00092       s1->Fill(xs1,0.3);
00093       s2->Fill(xs2,0.2);
00094       total->Fill(xmain);
00095       total->Fill(xs1,0.3);
00096       total->Fill(xs2,0.2);
00097       if (i && (i%upd) == 0) {
00098          if (i == upd) {
00099             c1->cd();
00100             total->Draw("e1p");
00101             hmain->Draw("same");
00102             s1->Draw("same");
00103             s2->Draw("same");
00104          }
00105          c1->Modified();
00106          c1->Update();
00107          gSystem->Sleep(10);
00108          TMethodCall c(c1->IsA(), "Print", "");
00109          void *arr[4];
00110          arr[1] = &c;
00111          arr[2] = (void *)c1;
00112          arr[3] = (void*)"\"hsumanim.gif+50\"";
00113          (*gThreadXAR)("METH", 4, arr, NULL);
00114       }
00115    }
00116    total->Draw("sameaxis"); // to redraw axis hidden by the fill area
00117    c1->Modified();
00118    c1->Update();
00119    // make infinite animation by adding "++" to the file name
00120    TMethodCall c(c1->IsA(), "Print", "");
00121    void *arr[4];
00122    arr[1] = &c;
00123    arr[2] = (void *)c1;
00124    arr[3] = (void*)"\"hsumanim.gif++\"";
00125    (*gThreadXAR)("METH", 4, arr, NULL);
00126    return 0;
00127 }
00128 
00129 void *joiner(void *)
00130 {
00131    thread1->Join();
00132    thread2->Join();
00133 
00134    finished = kTRUE;
00135 
00136    return 0;
00137 }
00138 
00139 void threadsh2()
00140 {
00141 #ifdef __CINT__
00142    printf("This script can only be executed via ACliC: .x threadsh2.C++\n");
00143    return;
00144 #endif
00145 
00146    finished = kFALSE;
00147    //gDebug = 1;
00148    gSystem->Unlink("hpxanim.gif");
00149    gSystem->Unlink("hsumanim.gif");
00150 
00151    printf("Starting Thread 0\n");
00152    thread1 = new TThread("t0", handle1, (void*) 0);
00153    thread1->Run();
00154    printf("Starting Thread 1\n");
00155    thread2 = new TThread("t1", handle2, (void*) 1);
00156    thread2->Run();
00157    printf("Starting Joiner Thread \n");
00158    threadj = new TThread("t4", joiner, (void*) 3);
00159    threadj->Run();
00160 
00161    TThread::Ps();
00162 
00163    while (!finished) {
00164       gSystem->Sleep(100);
00165       gSystem->ProcessEvents();
00166    }
00167 
00168    threadj->Join();
00169    TThread::Ps();
00170 
00171    delete thread1;
00172    delete thread2;
00173    delete threadj;
00174 }

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