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

Go to the documentation of this file.
00001 #include <TRandom.h>
00002 #include <TTree.h>
00003 #include <TFile.h>
00004 #include <TStyle.h>
00005 #include <TKey.h>
00006 
00007 #include <Riostream.h>
00008 #include <sstream>
00009 #include <string>
00010 #include "mysql/Mysql.hh"
00011 #include "MicroException.hh"
00012 #include <iostream>
00013 #include <fstream>
00014 #include "root/CaloHit.hh"
00015 
00016 //#include <TROOT.h>
00017 //#include <TRint.h>
00018 #include "TH2I.h"
00019 //#include "TNtuple.h"
00020 
00021 
00022 #include "root/CaloRun.hh"
00023 #include "root/CaloEvent.hh"
00024 #include "root/MTRun.hh"
00025 #include "root/MTEvent.hh"
00026 #include "root/MTChannel.hh"
00027 #include "root/MTTrack.hh"
00028 
00029 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,int deltaTMin, int deltaTMax);
00030 void buildCaloEvent(MTEvent& evt, CaloEvent&, int deltaTMin, int deltaTMax, float offset_x, float offset_y);
00031 
00032 void usage(void)
00033 {
00034     cout  << "usage: buildCaloHits -t timestampMin timestampMax outputfileName [deltaTMin deltaTMax]" << endl;
00035     cout  << "     : buildCaloHits -f filename outputfileName [deltaTMin deltaTMax]" << endl;
00036     cout << "1312577280 1312590660" << endl;
00037 }
00038 
00039 int main(int argc, char** argv) {
00040 
00041   if ( argc < 4 || argc > 7 )
00042   {
00043     usage();
00044     return -1;
00045   }
00046 
00047 
00048   int deltaTMin = -1;
00049   int deltaTMax = -1;
00050 
00051 
00052 
00053 
00054 
00055   // Get inputFile name in database by timestamp limit
00056   if ( strcmp (argv[1] ,"-t" )== 0 )
00057   {
00058     if ( argc < 5 ) { usage(); return -1; } 
00059 
00060     if ( argc == 7 ) 
00061     {
00062       deltaTMin = atoi ( argv[5] );
00063       deltaTMax = atoi ( argv[6] );
00064     }
00065     
00066     string outputFileName = argv[4];
00067     // RECREATE Root file to clear its contains if it alread exist
00068     TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00069     caloFile.Write();
00070     caloFile.Close();
00071 
00072     Mysql sql;
00073     string start = argv[2];
00074     string end = argv[3];
00075     
00076     try 
00077     {
00078       // Get list of TFile to filter from Database
00079       const vector<string>& ids = sql.getMergedFilesByTimestamp(start,end);
00080       vector<string>::const_iterator cii;
00081 
00082       // Loop over input TFile
00083       for(cii=ids.begin(); cii!=ids.end(); cii++)
00084       {
00085 
00086         string inputFileName = *cii;
00087         selectAndCopyEvent(inputFileName,outputFileName,deltaTMin,deltaTMax);
00088       }
00089       delete &ids;
00090     }
00091     catch (MicroException e) { }
00092   }
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100   // Get input file name in a input file
00101   else if ( strcmp (argv[1] ,"-f") == 0 )
00102   {
00103      if ( argc < 4 ) { usage(); return -1; } 
00104      string outputFileName = argv[3];
00105 
00106     if ( argc == 6 ) 
00107     {
00108       deltaTMin = atoi ( argv[4] );
00109       deltaTMax = atoi ( argv[5] );
00110     }
00111 
00112     
00113 
00114 
00115 
00116     // RECREATE Root file to clear its contains if it alread exist
00117     TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00118     caloFile.Write();
00119     caloFile.Close();
00120 
00121     ifstream myReadFile;
00122     myReadFile.open(argv[2]);
00123     string inputRootFile;
00124     if (myReadFile.is_open()) {
00125       while (!myReadFile.eof()) {
00126         getline(myReadFile,inputRootFile);
00127         if ( inputRootFile.size() > 0 ) 
00128         {
00129           cout<<"Read ROOT file : "<<inputRootFile<<endl;
00130           selectAndCopyEvent(inputRootFile,outputFileName,deltaTMin,deltaTMax);
00131         }
00132       }
00133     }
00134     myReadFile.close();
00135     return 0;
00136   }
00137   else 
00138   {
00139     usage();
00140     return -1;
00141   }
00142   return 0;
00143 }
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 /*-----------------------------------------------------------------------------------------------*/
00155 
00156 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,int deltaTMin, int deltaTMax)
00157 {
00158 
00159  
00160   TFile readFile(inputFileName.c_str(),"READONLY");
00161   TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00162 
00163   ui16 runId = 0;
00164 
00165 
00166   TIter nextkey(readFile.GetListOfKeys());
00167   TKey *key = NULL;
00168   CaloRun run;
00169 
00170   key= (TKey*)nextkey();
00171   
00172 
00173   //Get tree to read
00174   TTree *readTree = (TTree*)key->ReadObj();
00175   MTEvent *readEvt =  new MTEvent();
00176   TBranch *branch= readTree->GetBranch("MTEvent");
00177   branch->SetAddress(&readEvt);
00178 
00179 
00180   //Get RunId
00181   MTRun * readRun  = (MTRun*)readTree->GetUserInfo()->FindObject("MTRun");
00182   int runid = readRun->GetRunId();
00183   if (runid==0)
00184     {
00185       //cout<<"RunId not known, please enter: ";
00186       //cin>>runid;
00187       //cout<<endl;
00188       //cout<<endl;
00189       runid = 10227;
00190     }
00191 
00192   //Get telescope/prototype offset from database
00193   Mysql mysql;
00194   float offset_x = mysql.getRunOffsetX(runid);
00195   float offset_y = mysql.getRunOffsetY(runid);
00196 
00197   cout<<"OFFSETS = "<<offset_x<<" "<<offset_y<<endl;
00198 
00199 
00200 
00201   // Create a new TTree which will contain  filtered events
00202   stringstream label;
00203   label<< inputFileName << "_" << runId ; 
00204   TTree tree(label.str().c_str(),"Calo tree");
00205   cout << "work on :" << label.str() << endl;
00206   tree.SetMaxTreeSize(4000000000); // 4 Mb
00207 
00208   CaloEvent *evt = new CaloEvent();
00209   // create a branch with run 
00210   tree.Branch("CaloEvent",&evt,16000,2);
00211   run.SetRunId(runId++);
00212 
00213 
00214   // Loop over event in current input TTree
00215   int nbEntries = readTree->GetEntries();
00216   ui16 evtNum = 0;
00217   cout<<"Tree entries = "<<nbEntries<<endl;
00218 
00219   MTTrack * trk = NULL;
00220 
00221 
00222   //for ( evtNum = 0; evtNum < 100 ; evtNum++)
00223   for ( evtNum = 0; evtNum < nbEntries ; evtNum++)
00224     {
00225       cout << "Done for entry " << evtNum + 1 << " / " << nbEntries << "\r" << flush;
00226 
00227       // Get input Event as readEvt
00228       readTree->GetEntry(evtNum);
00229 
00230       bool valid_time = readEvt->GetTimeInfo();
00231 cout << "readEvt->GetTimeInfo["<< readEvt->GetTimeInfo() << "]"  << endl;
00232       bool square_m2 = readEvt->GetSquareEvt();
00233 
00234       if (valid_time && !square_m2)
00235         {
00236 
00237           trk = readEvt->GetTrack();
00238 cout << "readEvt->GetTrack()[" << readEvt->GetTrack() << "]" << endl;
00239 
00240           if (trk != NULL)
00241             {
00242               buildCaloEvent(*readEvt,*evt,deltaTMin,deltaTMax,offset_x,offset_y);
00243               tree.Fill();
00244               evt->Clear();
00245             }
00246         }
00247     }
00248 
00249 
00250   tree.GetUserInfo()->Add(&run); 
00251   TFile *curFile =tree.GetCurrentFile() ;
00252   //curFile->Write("", TObject::kOverwrite);
00253   tree.Write();
00254 
00255   delete readEvt;
00256   delete evt;
00257   
00258 
00259   caloFile.Close();
00260   readFile.Close();
00261   // delete run;
00262 }
00263 
00264 
00265 
00266 
00267 
00268 
00269 // Build Tree with position, time and digit of M2 prototype hits
00270 // + cut on the dt distribution between 5 and 10
00271 
00272 void buildCaloEvent(MTEvent& evt, CaloEvent& caloEvent,int deltaTMin, int deltaTMax, float offset_x, float offset_y)
00273   {
00274 
00275     /*m2 prototype time cut*/
00276     int dt_min = 5;
00277     int dt_max = 10;
00278 
00279     if (deltaTMin != -1) { deltaTMin = dt_min; }
00280     if (deltaTMax != -1) { deltaTMax = dt_max; }
00281 
00282 
00283     //Distance from first telescope chamber to m2 prototype
00284     float dist_z = 156.;
00285 
00286 
00287     //Get track
00288     MTTrack * trk = evt.GetTrack();
00289     
00290     int trk_quality = 0;
00291 
00292     if (trk->GetFromPad())
00293       {
00294         trk_quality = 10;
00295       }
00296 
00297     if (trk->GetFromStrip())
00298       {
00299         trk_quality = 20;
00300       }
00301 
00302     if (trk->GetStraight())
00303       {
00304         trk_quality++;
00305 
00306         if (trk->GetSingleHit())
00307           {
00308             trk_quality++;
00309           }
00310       }
00311 
00312     
00313     caloEvent.SetTrkQuality(trk_quality);
00314 
00315     float extrapol_x = offset_x;
00316     float extrapol_y = offset_y;
00317     
00318     //Extrapolate at prototype
00319     
00320     //straight tracks
00321     if (trk_quality%10 != 0)
00322       {
00323         extrapol_x += trk->GetBx();
00324         extrapol_y += trk->GetBy();     
00325       }
00326 
00327     //inclined tracks
00328     else
00329       {
00330         extrapol_x += trk->GetAx() * dist_z + trk->GetBx();
00331         extrapol_y += trk->GetAy() * dist_z + trk->GetBy();
00332       }
00333 
00334 
00335     caloEvent.SetXExtrapol(extrapol_x);
00336     caloEvent.SetYExtrapol(extrapol_y);
00337 
00338 
00339   
00340 
00341     //loop over hits
00342 
00343     int nchannel = evt.GetNchannel();
00344     int nhit_m2_cut = 0;
00345 
00346     for (int j=0;j<nchannel;j++)
00347       {
00348         MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00349           
00350         //look in m2 prototype
00351         if (channel->GetChamberId() == 10)
00352           {
00353             Long64_t dt = channel->GetBcIdDif() - channel->GetBcIdHit();
00354               
00355             int zpos = (int)channel->GetZ() / 10000;
00356 
00357             if (dt>=dt_min && dt<=dt_max)
00358               {
00359                 int digit = channel->GetDigitalValue();
00360                 int xpos = (int)channel->GetX() / 10000;
00361                 int ypos = (int)channel->GetY() / 10000;
00362                   
00363                 int t = (int)dt;
00364                   
00365                 CaloHit hit;
00366 
00367                 hit.SetX(xpos);
00368                 hit.SetY(ypos);           
00369                 hit.SetZ(zpos);
00370                 hit.SetDeltaT(t);
00371                 hit.SetThreshold(digit);
00372 
00373                 caloEvent.AddHit(hit);  
00374               }
00375           }
00376       }
00377   }
00378 

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