#include "tools/Log.hh"
#include "tools/Toolbox.hh"
#include "root/MTRun.hh"
#include "root/MTChannel.hh"
#include "root/MTEvent.hh"
#include "root/MTTrack.hh"
#include "MicroException.hh"
#include "mysql/Mysql.hh"
#include "TFile.h"
#include "TTree.h"
#include "TKey.h"
#include "TBranch.h"
#include "TRandom.h"
#include <sstream>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
Include dependency graph for setRunId.cpp:
Go to the source code of this file.
Functions | |
int | main (int argc, char **argv) |
int main | ( | int | argc, | |
char ** | argv | |||
) |
Definition at line 40 of file setRunId.cpp.
00040 { 00041 00042 if ( argc != 2 ) { 00043 FILE_LOG(logERROR) << "usage:" << endl; 00044 FILE_LOG(logERROR) << "setRunId 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 00078 TTree *updateTree = new TTree(treeName.c_str(),"MicroMegas event"); 00079 updateTree->Branch("MTEvent",&updateEvt,16000,2); 00080 00081 MTRun * run = (MTRun*)tree->GetUserInfo()->FindObject("MTRun"); 00082 00083 // Set Processed flag to MTRun 00084 ui32 runId = run->GetRunId(); 00085 ui32 sqlrunId = UNDEFINED_ID; 00086 if ( runId != 0 && runId != UNDEFINED_ID ) 00087 { 00088 cout << "Run id alread set[" << runId << "]" << endl; 00089 } 00090 else 00091 { 00092 size_t last_slash = inputfile.find_last_of("/") +1 ; 00093 string name = inputfile.substr(last_slash); 00094 string path = inputfile.substr(0,last_slash-1); 00095 cout << "search file FromRawFile: path[" << path << "] , name[" << name <<"]" << endl; 00096 00097 Mysql mysql; 00098 try { 00099 sqlrunId = mysql.getRunIdFromRawFile(path,name); 00100 } 00101 catch (MicroException &e) {; } 00102 00103 if ( sqlrunId == UNDEFINED_ID) 00104 { 00105 // cout << "search file RebuildFile: path[" << path << "] , name[" << name <<"]" << endl; 00106 try { 00107 sqlrunId = mysql.getRunIdFromRebuildFile(path,name); 00108 } 00109 catch (MicroException &e) {; } 00110 if ( sqlrunId == UNDEFINED_ID ) 00111 { 00112 // cout << "search file FromMergeFile: path[" << path << "] , name[" << name <<"]" << endl; 00113 try { 00114 sqlrunId = mysql.getRunIdFromMergeFile(path,name); 00115 } 00116 catch (MicroException &e) {; } 00117 } 00118 } 00119 // cout << "after reseacrh RunId[" << sqlrunId << "] [0x" << hex << sqlrunId << "]" << endl; 00120 } 00121 00122 if ( sqlrunId == UNDEFINED_ID ) 00123 { 00124 cout << "No run correspond to this fuile in database !" << endl; 00125 exit(-1); 00126 } 00127 00128 // Save MTRun infiormation to new processed TTree 00129 run->SetRunId(sqlrunId); 00130 cout << "set RunId[" << dec << sqlrunId << endl; 00131 updateTree->GetUserInfo()->Add(run); 00132 MTEvent *evt = new MTEvent(); 00133 TBranch *branch= tree->GetBranch("MTEvent"); 00134 branch->SetAddress(&evt); 00135 00136 int nbEntries = tree->GetEntries(); 00137 for ( int evtNum = 0; evtNum < 10 ; evtNum++) 00138 { 00139 tree->GetEntry(evtNum); 00140 updateTree->Fill(); 00141 } 00142 00143 // Write Root Object to TFile 00144 TFile *curFile =updateTree->GetCurrentFile(); 00145 updateTree->Write("", TObject::kOverwrite); 00146 updateTree->SetDirectory(0); 00147 00148 // free heap memory 00149 delete evt; 00150 } 00151 00152 infile.Close(); 00153 oufile.Close(); 00154 00155 00156 // Replace original root file by the new processed one 00157 string command = "cp -f "; 00158 command = command + outputfile + " " + inputfile ; 00159 int result = system(command.c_str()); 00160 if ( result == -1 ) { cout << strerror(errno) << endl; } 00161 command = "rm -f " + outputfile; 00162 result = system(command.c_str()); 00163 if ( result == -1 ) { cout << strerror(errno) << endl; } 00164 00165 00166 cout << endl << "Done." << endl; 00167 00168 00169 return 0; 00170 }