/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/lcio/createLcioCalorimeterHit.cc

Go to the documentation of this file.
00001 /* @version $Revision: 1560 $ * @modifiedby $Author: jacquem $ * @lastmodified $Date: 2012-03-06 09:54:17 +0100 (Tue, 06 Mar 2012) $ */
00002 
00003 #include "IO/LCWriter.h"
00004 #include "IMPL/LCRunHeaderImpl.h"
00005 #include "IMPL/LCEventImpl.h"
00006 #include "IMPL/LCTOOLS.h"
00007 #include "IMPL/LCEventImpl.h"
00008 #include "IMPL/CalorimeterHitImpl.h"
00009 #include "IMPL/LCCollectionVec.h"
00010 #include "IMPL/LCFlagImpl.h"
00011 
00012 #include "lcio.h"
00013 
00014 #include "tools/MicroException.hh"
00015 #include "tools/Arguments.hh"
00016 
00017 #include <sstream>
00018 #include <iostream>
00019 
00020 #include <TRandom.h>
00021 #include <TTree.h>
00022 #include <TFile.h>
00023 #include <TStyle.h>
00024 #include <TKey.h>
00025 #include <TreeClass.hh>
00026 
00027 #include <Riostream.h>
00028 #include <sstream>
00029 #include <string>
00030 #include "mysql/Mysql.hh"
00031 #include "MicroException.hh"
00032 #include <iostream>
00033 #include <fstream>
00034 
00035 //#include <TROOT.h>
00036 //#include <TRint.h>
00037 #include "TH2I.h"
00038 //#include "TNtuple.h"
00039 
00040 using namespace lcio;
00041 
00042 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName);
00043 Long64_t buildCaloEvent(const MTEvent& evt, LCEventImpl&, Long64_t bcidAbsPrev, bool &track,TH1F& hResidualX, TH1F& hResidualY);
00044 
00045 void usage(void)
00046 {
00047     cout  << "     : buildCaloHits -f filename outputfileName" << endl;
00048 }
00049 
00050 int main(int argc, char** argv) {
00051 
00052   if ( argc < 4 || argc > 5 )
00053   {
00054     usage();
00055     return -1;
00056   }
00057 
00058   // Get input file name in a input file
00059   if ( strcmp (argv[1] ,"-f") == 0 )
00060   {
00061      if ( argc != 4 ) { usage(); return -1; } 
00062      string outputFileName = argv[3];
00063 
00064      ifstream myReadFile;
00065      myReadFile.open(argv[2]);
00066      string inputRootFile;
00067      if (myReadFile.is_open()) {
00068      while (!myReadFile.eof()) {
00069         getline(myReadFile,inputRootFile);
00070         if ( inputRootFile.size() > 0 ) 
00071         {
00072           selectAndCopyEvent(inputRootFile,outputFileName);
00073         }
00074      }
00075     }
00076     myReadFile.close();
00077     return 0;
00078   }
00079   else 
00080   {
00081     usage();
00082     return -1;
00083   }
00084   return 0;
00085 }
00086 
00087 
00088   /*-----------------------------------------------------------------------------------------------*/
00089 
00090 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName)
00091 {
00092 
00093  
00094   TFile readFile(inputFileName.c_str(),"READONLY");
00095 
00096   ui16 runId = 0;
00097 
00098   // lcio File Writer 
00099   LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter()  ;
00100   lcWrt = LCFactory::getInstance()->createLCWriter()  ;
00101 
00102   // Lcio Run header
00103   LCRunHeaderImpl* runHdr = new LCRunHeaderImpl() ;
00104   runHdr->setDetectorName( "micromegas microroc m2 and Gassiplex telescope" ) ;
00105   runHdr->setDescription( " testing lcio with micromegas" ) ;
00106   /*
00107 string ecalName("micromegas_microroc_m2_calorimeter") ;
00108   runHdr->addActiveSubdetector( ecalName ) ;
00109 
00110   string telName("telecsope") ;
00111   runHdr->addActiveSubdetector( telName ) ;
00112 */
00113   TIter nextkey(readFile.GetListOfKeys());
00114   TKey *key = NULL;
00115   
00116   while (key= (TKey*)nextkey() )
00117   {
00118     TTree *readTree = (TTree*)key->ReadObj();
00119 
00120     MTEvent *readEvt =  new MTEvent();
00121 
00122     TBranch *branch= readTree->GetBranch("MTEvent");
00123 
00124     branch->SetAddress(&readEvt);
00125 
00126   // Open lcio new file in write mode
00127     lcWrt->open( outputFileName , LCIO::WRITE_NEW )  ;
00128 
00129     Double32_t temp=0;
00130     Double32_t pres=0;
00131 
00132     Long64_t bcIdAbsPrev = -1;
00133     runHdr->setRunNumber(runId++ ) ;
00134 
00135     // Loop over event in current input TTree
00136     int nbEntries = readTree->GetEntries();
00137     ui16 evtNum = 0;
00138     TH1F hResidualX("hResidualX" , "hResidualX;xtel - xm2 (pad)" ,201,-100,100); 
00139     TH1F hResidualY("hResidualY" , "hResidualY;ytel - ym2 (pad)" ,201,-100,100); 
00140 
00141     for ( evtNum = 0; evtNum < nbEntries ; evtNum++)
00142     {
00143       // lcio event
00144       LCEventImpl* lcioEvtImpl =  new LCEventImpl();
00145       // Get input Event throw readEvt
00146       readTree->GetEntry(evtNum);
00147       if ( evtNum % 1 == 0)
00148       {
00149         cout << "Done for entry " << evtNum  +1 << " / " << nbEntries << "\r" << flush;
00150       }
00151 
00152       temp = temp + readEvt->GetTemperature();
00153       pres = pres + readEvt->GetPressure();
00154       bool track = false;
00155       bcIdAbsPrev = buildCaloEvent(*readEvt,*lcioEvtImpl,bcIdAbsPrev, track, hResidualX,hResidualY);
00156       if( track )
00157       {
00158         lcWrt->writeEvent( lcioEvtImpl ) ;
00159       } 
00160       delete lcioEvtImpl;
00161     }
00162     pres = pres / evtNum;
00163     temp = temp / evtNum;
00164     //run.SetTemperature(temp);
00165     //run.SetPressure(pres);
00166     
00167     //run.SetOffsetX(-101 + hResidualX.GetMaximumBin()); 
00168     //run.SetOffsetY(-101 + hResidualY.GetMaximumBin()); 
00169 
00170   
00171     lcWrt->close() ;
00172     delete lcWrt;
00173     delete readEvt;
00174   }
00175   readFile.Close();
00176 
00177  cout << endl;
00178 }
00179 
00180 
00181 
00182 
00183 
00184 //
00185 // Build Tree with position, time and digit of M2 prototype hits
00186 // + cut on time information (last readout less than 3.35 s before)
00187 // + cut on high multiplicity in m2 prototype inside dt window from 0-20 bcid
00188 // + cut on the dt distribution between 5 and 10
00189 
00190 
00191 
00192   Long64_t buildCaloEvent(const MTEvent& evt, LCEventImpl& caloEvent, Long64_t bcIdAbsPrev ,bool& track,TH1F& hResidualX, TH1F& hResidualY)
00193   {
00194 
00195   caloEvent.setTimeStamp( evt.GetTimestamp() * 1000000  ) ;
00196   caloEvent.setDetectorName( "Lapp Microroc Micromegas and Gassiplex telecope" ) ;
00197 
00198 
00199   int nchannel = 0;
00200   //Define bcIdAbsPrev, bcid_abs of first readout
00201   if (bcIdAbsPrev == -1)
00202   {
00203     nchannel = evt.GetNchannel();
00204 
00205     for (int j=0;j<nchannel;j++)
00206     {             
00207       MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00208 
00209       if (channel->GetChamberId() == 10)
00210       {
00211        return  channel->GetBcId_Abs();
00212       }
00213     }
00214   }
00215 
00216 
00217   /*m2 prototype time cut*/
00218   int dt_min = 4;
00219   int dt_max = 10;
00220 
00221   /*spark cut = Nhit inside 0-20 dt region*/
00222   int nhit_spark_cut = 1000;
00223 
00224   /*telescope energy cut*/
00225   int adc_cut = 25;
00226 
00227   int i = 0;
00228   int t = 0;
00229   int adc = 0;
00230   int xpos = 0;
00231   int ypos = 0;
00232   int zpos = 0;
00233   int zmax = 0;
00234   int digit = 0;
00235   int chbid = 0;
00236   int nhit_m2_cut = 0;
00237   double time = 0;
00238   TString name,title;
00239   Long64_t tprev,t0,t1,t2,t3,dt;
00240 
00241   int nhit[3];
00242   int xmax[3];
00243   int ymax[3];
00244 
00245   TH2I * hxy_tel_adc[3];
00246 
00247 
00248   for (int i=0;i<3;i++)
00249   {
00250     nhit[i] = 0;
00251     xmax[i] = 0;
00252     ymax[i] = 0;
00253 
00254     name = Form("hxy_tel_adc_chamber_%i",i+7);
00255     title = Form("Hit pattern (adc>%i) chamber %i;y (cm);x (cm)",adc_cut,i+7);
00256     hxy_tel_adc[i] = new TH2I(name,title,32,0,32,32,0,32);
00257   }
00258 
00259   //if t0 is defined start loop over events
00260 
00261   nchannel = evt.GetNchannel();
00262 
00263   if (nchannel!=0)
00264   {
00265     //determine time to previous readout
00266     for (int j=0;j<nchannel;j++)
00267     {
00268       MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00269       if (channel->GetChamberId() == 10)
00270       {
00271         t1 = channel->GetBcId_Abs();
00272         j = nchannel;
00273       }
00274     }
00275 
00276     time = t1 - bcIdAbsPrev;
00277     time *= 200e-9;
00278     bcIdAbsPrev = t1;
00279 
00280 
00281     //if readout occured after less than 3.35 s of previous readout
00282     //skip also first event as hit time info is wrong
00283     if (time <= 3.35)
00284     {
00285       //look for a track in telescope
00286       for (int j=0;j<nchannel;j++)
00287       {
00288         MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00289         chbid = channel->GetChamberId();
00290 
00291         //look in pad chambers
00292         if (chbid == 7 || chbid == 8 || chbid == 9)
00293         {
00294           adc = channel->GetAnalogValue();
00295 
00296           if (adc >= adc_cut)
00297           {
00298             nhit[chbid-7]++;
00299             hxy_tel_adc[chbid-7]->Fill(channel->GetY()*1e-4,channel->GetX()*1e-4,adc);
00300           }
00301         }
00302       }
00303 
00304       //require that 3 pad chambers have at least 1 hit
00305       if (nhit[0]>0 && nhit[1]>0 && nhit[2]>0)
00306       {
00307         //find position of ADC maxima
00308 
00309         for (int k=0;k<3;k++)
00310         {
00311           hxy_tel_adc[k]->GetMaximumBin(ymax[k],xmax[k],zmax);
00312         }
00313 
00314         //require aligned maxima
00315 
00316         if (ymax[0]==ymax[1] && ymax[0]==ymax[2] &&
00317             xmax[0]==xmax[1] && xmax[0]==xmax[2])
00318         {
00319           //reloop over hits
00320           nhit_m2_cut = 0;
00321 
00322           for (int j=0;j<nchannel;j++)
00323           {
00324             MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00325 
00326             //look in m2 prototype
00327             if (channel->GetChamberId() == 10)
00328             {                                 
00329               t2 = channel->GetBcId_Dif();
00330               t3 = channel->GetBcId_Hit();
00331               dt = t2 - t3;
00332 
00333               if (dt>=0 && dt<=20)
00334               {
00335                 nhit_m2_cut++;
00336               }
00337             }
00338           }
00339 
00340           //reject high multiplicity event in time region of signal
00341           if (nhit_m2_cut < nhit_spark_cut)
00342           {
00343                   track = 1;    
00344             caloEvent.parameters().setValue("XTelescope", xmax[0] );
00345             caloEvent.parameters().setValue("YTelescope", ymax[0] );
00346 
00347             //reloop over hits
00348             LCCollectionVec* calVec = new LCCollectionVec( LCIO::CALORIMETERHIT )  ;
00349             for (int j=0;j<nchannel;j++)
00350             {
00351               MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00352 
00353               //look in m2 prototype
00354               if (channel->GetChamberId() == 10)
00355               {
00356                 t3 = channel->GetBcId_Hit();
00357                 dt = t2 - t3;
00358 
00359                 if (dt>=dt_min && dt<=dt_max)
00360                 {
00361                   digit = channel->GetDigitalValue();
00362                   xpos = (int)channel->GetX();
00363                   ypos = (int)channel->GetY();
00364                   zpos = (int)channel->GetZ();
00365 
00366                   xpos /= 10000;
00367                   ypos /= 10000;
00368 
00369                   t = (int)dt;
00370 
00371 //                  CaloHit hit;
00372                         hResidualX.Fill(xmax[0]-xpos);
00373                         hResidualY.Fill(ymax[0]-ypos);
00374 /*
00375                   hit.SetDeltaT(t);
00376                   hit.SetThreshold(digit);
00377 */
00378 
00379                   CalorimeterHitImpl* caloHit = new CalorimeterHitImpl() ;
00380                   //caloHit->parameters().setValue("DeltaT", t);
00381                   //caloHit->parameters().setValue("Threshold", digit);
00382                   try
00383                   {
00384                     // Create RawCalorimeterHit
00385                     caloHit->setEnergy( digit )  ;
00386                     caloHit->setTime( t )  ;
00387                     const float pos[3] = { xpos, ypos, 0};
00388                     caloHit->setPosition(pos);
00389 
00390 
00391 
00392                       LCFlagImpl chFlag(0) ;
00393                       //chFlag.setBit( LCIO::CHBIT_ID1 ) ;
00394                       chFlag.setBit( LCIO::RCHBIT_LONG )   ;  // long(1) - short(0) , incl./excl. position
00395                       chFlag.setBit( LCIO::RCHBIT_BARREL ) ;  // barrel(1) - endcap(0)
00396                       chFlag.setBit( LCIO::RCHBIT_TIME   ) ; // 1: time information stored
00397                       //chFlag.setBit( LCIO::RCHBIT_ID1   )  ;  // cellid1 stored
00398                       chFlag.setBit( LCIO::RCHBIT_NO_PTR ) ; // 1: pointer tag not added
00399                       //chFlag.setBit( LCIO::RCHBIT_ENERGY_ERROR )  ;   // 1: store energy error
00400                       calVec->setFlag( chFlag.getFlag()) ;
00401 
00402                     calVec->push_back( caloHit ) ;
00403 
00404                   } // end try block
00405                   catch (MicroException e)
00406                   {
00407                   }
00408                   //LCTOOLS::printRawCalorimeterHits(calVec);
00409                   // add all collections to the event
00410 
00411                 }
00412               }
00413             }
00414             caloEvent.addCollection( calVec , "m2" ) ;
00415           }
00416           else { track = 0; }
00417         }
00418       } //end nhit[0]>0 && nhit[1]>0 && nhit[2]>0
00419     } // end time <= 3.35 && i != 0
00420   } // enf if nChannel != 0
00421     
00422   for (int i=0;i<3;i++)
00423   {
00424    delete hxy_tel_adc[i];
00425   }
00426 
00427   return bcIdAbsPrev;
00428 }
00429 

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