/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/root/process_example.cpp

Go to the documentation of this file.
00001 /* @version $Revision: 1566 $ * @modifiedby $Author: jacquem $ * @lastmodified $Date: 2012-03-08 14:00:10 +0100 (Thu, 08 Mar 2012) $ */
00002 #include "tools/Log.hh"
00003 #include "tools/Toolbox.hh"
00004 
00005 #include "root/MTRun.hh"
00006 #include "root/MTChannel.hh"
00007 #include "root/MTEvent.hh"
00008 #include "root/MTTrack.hh"
00009 #include "MicroException.hh"
00010 
00011 #include "mysql/Mysql.hh"
00012 
00013 
00014 #include "mysql/Mysql.hh"
00015 
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include "TKey.h"
00019 #include "TBranch.h"
00020 #include "TRandom.h"
00021 
00022 
00023 #include <sstream>
00024 #include <sys/types.h>
00025 #include <unistd.h>
00026 #include <stdio.h>
00027 
00028 
00029 using namespace std;
00030 
00031 /*
00032  Modifying a existing TTree is often not possible because the tree is in a read-only
00033  file and you do not have permission to save the modified tree .
00034  Even if you do have the permission, you risk losing the
00035  original tree with an unsuccessful attempt to save  the modification.
00036 */
00037 //---------------------------------------------
00038 //
00039 //---------------------------------------------
00040 int main(int argc, char**argv){
00041 
00042    if ( argc != 2  ) {
00043        FILE_LOG(logERROR)  << "usage:" << endl;
00044        FILE_LOG(logERROR)  << ":process_example fileToProcess" << endl;
00045        exit(1);
00046    }
00047   string inputfile = argv[1] ;
00048 
00049   // build temporaly fill path and name
00050   pid_t pid = getpid(); 
00051   std::ostringstream oss;
00052   oss << "/tmp/" << pid << ".root";
00053   string outputfile = oss.str();
00054 
00055   // Open Root TFile in read only mode
00056   TFile infile(inputfile.c_str(),"READ");
00057 
00058   // Open Root TFile in write mode
00059   TFile oufile(outputfile.c_str(),"RECREATE");
00060   // Change current directory to "this" directory
00061   // Mandatory to use updateTree->GetCurrentFile() later
00062   oufile.cd();
00063 
00064 
00065 
00066   // Loop over original TTrees
00067   TIter nextkey(infile.GetListOfKeys());
00068   TKey *key;
00069   MTEvent *updateEvt = new MTEvent();
00070 
00071   while (key = (TKey*)nextkey()) 
00072   {
00073     TTree *tree = (TTree*)key->ReadObj();
00074 
00075   // Create nwew TTree which will contain processed MTRun and MTEvent
00076   string treeName = tree->GetName();
00077   treeName = treeName + "_preprocessed" ;
00078 
00079   TTree *updateTree = new TTree(treeName.c_str(),"MicroMegas event");
00080   updateTree->Branch("MTEvent",&updateEvt,16000,2);
00081 
00082     MTRun * run  = (MTRun*)tree->GetUserInfo()->FindObject("MTRun");
00083 
00084     // Set Processed flag to MTRun
00085     run->SetProcessed(true);
00086 
00087     // Save MTRun infiormation to new processed TTree
00088     updateTree->GetUserInfo()->Add(run);
00089     MTEvent *evt =  new MTEvent();
00090     TBranch *branch= tree->GetBranch("MTEvent");
00091     branch->SetAddress(&evt);
00092 
00093 
00094     MTChannel* channel =NULL;
00095     int nbEntries = tree->GetEntries();
00096     TRandom random;
00097     //for ( int evtNum = 0; evtNum < nbEntries ; evtNum++)
00098     for ( int evtNum = 0; evtNum < 10 ; evtNum++)
00099     {
00100       tree->GetEntry(evtNum);
00101 
00102       // Copy original MTEvent to new processed MTEvent
00103       *updateEvt = *evt;
00104 
00105       // Processed MTEvents
00106       Float_t  ax =0.;
00107       Float_t  bx =0.;
00108       Float_t  ay =0.;
00109       Float_t  by=0.;
00110       ax = random.Rndm();
00111       bx = random.Rndm();
00112       ay = random.Rndm();
00113       by = random.Rndm();
00114 
00115       MTTrack *track = new MTTrack(true, // fromPad
00116                                   false, // fromStrip
00117                                   true , // straight 
00118                                   false, // singleHit
00119                                   ax,  // ax 
00120                                   bx,   // bx 
00121                                   ay,  // ay
00122                                   by); // by
00123 
00124       updateEvt->SetTimeInfo(false);
00125       updateEvt->SetSquareEvt(true);
00126       updateEvt->SetTrack(track);
00127 
00128       // Write MTEvent into Branch
00129       cout  << "Write Event[" << evtNum << "] \t\t\t\t\t\t\t\t\t\t\t\r" << flush;
00130       updateTree->Fill();
00131       delete track;
00132 
00133     }
00134 
00135     // Write Root Object to TFile
00136     TFile *curFile =updateTree->GetCurrentFile();
00137     updateTree->Write("", TObject::kOverwrite);
00138     updateTree->SetDirectory(0);
00139   
00140     // free heap memory
00141     delete evt;
00142   }
00143   
00144 
00145   infile.Close();
00146   oufile.Close();
00147 
00148 
00149   //delete updateEvt;
00150   // Replace original root file by the new processed one
00151   rename(outputfile.c_str(),inputfile.c_str());
00152   
00153   cout << endl << "Done." << endl;
00154 
00155 
00156   // Set run processed information into database
00157   Mysql mysql;
00158   mysql.updateRunProcessInfo(10211, // runId
00159                              12.2, // offsetX
00160                              45.3, // offsetY 
00161                              123.1, // pressure
00162                              37.4, // temperature
00163                              45 );// deltaTMax 
00164 
00165   mysql.addRunChamberProcessInfo(10211,
00166                                  10,  // chamberId,
00167                                  1.2, // xmean, 
00168                                  2.3, // ymean,
00169                                  99.3,// inefficiency, 
00170                                  15.5,// nHitMean,
00171                                  8) ;// deltaTMax;
00172 
00173   
00174     ui32 runId = 10211;
00175     ui16 chamberId = 10;
00176   
00177     cout <<  "OffsetX: " <<  mysql.getRunOffsetX(  runId) << endl;
00178     cout <<  "OffsetY: " << mysql.getRunOffsetY(  runId) << endl;
00179     cout <<  "Pressure: " <<  mysql.getRunPressure(  runId) << endl;
00180     cout <<  "Temperature: " <<  mysql.getRunTemperature(  runId) << endl;
00181     cout <<  "DeltaTMax: " <<  mysql.getRunDeltaTMax(  runId) << endl;
00182 
00183     cout <<  "XMean: " <<  mysql.getRunChamberXMean( runId,  chamberId) << endl;
00184     cout <<  "YMean: " <<  mysql.getRunChamberYMean( runId,  chamberId) << endl;
00185     cout <<  "Inefficiency: " <<  mysql.getRunChamberInefficiency( runId,  chamberId) << endl;
00186     cout <<  "NHitMean: " <<  mysql.getRunChamberNHitMean( runId,  chamberId) << endl;
00187     cout <<  "DeltaTMax: " <<  mysql.getRunChamberDeltaTMax( runId,  chamberId) << endl;
00188 
00189 
00190 return 0;
00191 }

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7