void EUTelProcessorTrackAnalysis::processEvent(LCEvent * evt){

	EUTelEventImpl * event = static_cast<EUTelEventImpl*> (evt); ///We change the class so we can use EUTelescope functions

	if (event->getEventType() == kEORE) {
		streamlog_out(DEBUG4) << "EORE found: nothing else to do." << endl;
		return;
	}else if (event->getEventType() == kUNKNOWN) {
		streamlog_out(WARNING2) << "Event number " << event->getEventNumber() << " in run " << event->getRunNumber() << " is of unknown type. Continue considering it as a normal Data Event." << endl;
	}
	LCCollection* eventCollection = NULL;
	try {
		eventCollection = evt->getCollection(_trackInputCollectionName);
		streamlog_out(DEBUG1) << "collection : " << _trackInputCollectionName << " retrieved" << std::endl;
	}catch (DataNotAvailableException e) {
		streamlog_out(MESSAGE0) << _trackInputCollectionName << " collection not available" << std::endl;
		throw marlin::SkipEventException(this);
	}
	if (eventCollection != NULL) {
		streamlog_out(DEBUG2) << "Collection contains data! Continue!" << endl;
		for (int iTrack = 0; iTrack < eventCollection->getNumberOfElements(); ++iTrack){
			EUTelTrack track = *(static_cast<EUTelTrack*> (eventCollection->getElementAt(iTrack)));
			_analysis->plotResidualVsPosition(track);	
			_analysis->plotIncidenceAngles(track);
		}

	}	
	
	
	
	
}
void EUTelProcessorPlotTrack::processEvent(LCEvent * evt){

	EUTelEventImpl * event = static_cast<EUTelEventImpl*> (evt); ///We change the class so we can use EUTelescope functions

	
	//////////////////////////////////////////////////////////////////////// Do not process last events
	if (event->getEventType() == kEORE) {
		streamlog_out(DEBUG4) << "EORE found: nothing else to do." << endl;
		return;
  }else if (event->getEventType() == kUNKNOWN) {
  	streamlog_out(WARNING2) << "Event number " << event->getEventNumber() << " in run " << event->getRunNumber() << " is of unknown type. Continue considering it as a normal Data Event." << endl;
  }
	////////////////////////////////////////////////////////////////////////
	
	//////////////////////////////////////////////////////////////////////////Try to access collection	
	LCCollection* col = NULL;
	try{
  	col = evt->getCollection(_trackCandidatesInputCollectionName);
 		streamlog_out(DEBUG1) << "collection : " << _trackCandidatesInputCollectionName << " retrieved" << std::endl;
  } catch (DataNotAvailableException e) {
  	streamlog_out(MESSAGE0) << _trackCandidatesInputCollectionName << " collection not available" << std::endl;
  	throw marlin::SkipEventException(this);
  }
	//////////////////////////////////////////////////////////////////////

	


}
void EUTelProcessorRootCreate::processEvent(LCEvent * evt){
	try{
		EUTelEventImpl * event = static_cast<EUTelEventImpl*> (evt); ///We change the class so we can use EUTelescope functions

		if (event->getEventType() == kEORE) {
			streamlog_out(DEBUG4) << "EORE found: nothing else to do." << std::endl;
			return;
		}else if (event->getEventType() == kUNKNOWN) {
			streamlog_out(WARNING2) << "Event number " << event->getEventNumber() << " in run " << event->getRunNumber() << " is of unknown type. Continue considering it as a normal Data Event." << std::endl;
		}
        streamlog_out(DEBUG2) << "Collection contains data! Continue!" << std::endl;
        EUTelReaderGenericLCIO reader = EUTelReaderGenericLCIO(); streamlog_out(DEBUG2) << "Collection contains data! Continue! line 53" << std::endl;
        streamlog_out(DEBUG2) << "_trackInputCollectionName = " <<_trackInputCollectionName<<std::endl;
        std::vector<EUTelTrack> tracks = reader.getTracks(evt, _trackInputCollectionName);
        /** LCReader stuff goes here. **/
        for (size_t i = 0; i < tracks.size(); ++i){
            tracks.at(i).print();
        }
    }catch (DataNotAvailableException e) {
		throw marlin::SkipEventException(this);
	}
	catch(std::string &e){
		streamlog_out(MESSAGE9) << e << std::endl;
		throw marlin::SkipEventException( this ) ;
	}
	catch(lcio::Exception& e){
		streamlog_out(MESSAGE9) << e.what() <<std::endl;
		throw marlin::StopProcessingException( this ) ;
	}
	catch(...){
		streamlog_out(MESSAGE9)<<"Unknown exception in process function of root reader" <<std::endl;
		throw marlin::StopProcessingException( this ) ;
	}

	
}
void EUTelProcessorCoordinateTransformHits::processEvent(LCEvent* event)
{
		//Check the event type and if it is the last event.
		EUTelEventImpl* evt	= static_cast<EUTelEventImpl*>(event);				
		if( evt->getEventType() == kEORE )
		{
				streamlog_out( MESSAGE5 ) << "EORE found: nothing else to do." << std::endl;
				return;
		} 
		else if( evt->getEventType() == kUNKNOWN )
		{
				streamlog_out( WARNING2 ) << "Event number " << evt->getEventNumber() << " in run " << evt->getRunNumber()
						<< " is of unknown type. Continue considering it as a normal Data Event." << std::endl;
		}

		//Opens collection for input.
		LCCollection* inputCollection = nullptr;
		try
		{
				inputCollection = evt->getCollection(_hitCollectionNameInput);
		}
		catch (DataNotAvailableException& e)
		{
				streamlog_out( WARNING2 ) << _hitCollectionNameInput << " collection not available" << std::endl;
				return;
		}

		LCCollectionVec* outputCollection = nullptr;
		try
		{
				outputCollection  = static_cast<LCCollectionVec*> (event->getCollection( _hitCollectionNameOutput ));
		}
		catch(...)
		{
				outputCollection = new LCCollectionVec(LCIO::TRACKERHIT);
		}

		std::string encoding = inputCollection->getParameters().getStringVal( LCIO::CellIDEncoding );

		if(encoding.empty())
		{
			encoding = EUTELESCOPE::HITENCODING;
		}

		lcio::CellIDDecoder<TrackerHitImpl> hitDecoder ( encoding );
		lcio::UTIL::CellIDReencoder<TrackerHitImpl> cellReencoder( encoding, outputCollection );

		//Now get each individual hit LOOP OVER!
		for(int iHit = 0; iHit < inputCollection->getNumberOfElements(); ++iHit)
		{  
			TrackerHitImpl*	inputHit = static_cast<TrackerHitImpl*>(inputCollection->getElementAt(iHit));
			TrackerHitImpl* outputHit = new IMPL::TrackerHitImpl(); 

			//Call the local2masterHit/master2localHit function defined int EUTelGeometryTelescopeDescription
			int properties = hitDecoder(inputHit)["properties"];
			int sensorID = hitDecoder(inputHit)["sensorID"];
			
			const double* inputPos = inputHit->getPosition();
			double outputPos[3];

			if( !(properties & kHitInGlobalCoord) && !_undoAlignment )
			{
				streamlog_out(DEBUG5) << "Transforming hit from local to global!" << std::endl;
              //  std::cout<<"Local Sensor: " << sensorID << " " << inputPos[0]<< " " << inputPos[1]<< " " << inputPos[2]<<std::endl;
				geo::gGeometry().local2Master(sensorID, inputPos, outputPos);
              //  std::cout<<"Global Sensor: " << sensorID << " " << outputPos[0]<< " " << outputPos[1]<< " " << outputPos[2]<<std::endl;

			}
			else if( (properties & kHitInGlobalCoord) && _undoAlignment )
			{
				streamlog_out(DEBUG5) << "Transforming hit from global to local!" << std::endl;
				geo::gGeometry().master2Local(sensorID, inputPos, outputPos);
			}
			else
			{
				std::cout << "Properties: " << properties <<std::endl;
				std::string errMsg;
				if(!_undoAlignment) errMsg = "Provided global hit, but trying to transform into global. Something is wrong!";
				else errMsg = "Provided local hit, but trying to transform into local. Something is wrong!";
				throw InvalidGeometryException(errMsg);
			}
	
			//Fill the new outputHit with information
			outputHit->setPosition(outputPos);
			outputHit->setCovMatrix( inputHit->getCovMatrix());
			outputHit->setType( inputHit->getType() );
			outputHit->setTime( inputHit->getTime() );
			outputHit->setCellID0( inputHit->getCellID0() );
			outputHit->setCellID1( inputHit->getCellID1() );
			outputHit->setQuality( inputHit->getQuality() );
			outputHit->rawHits() = inputHit->getRawHits();

			cellReencoder.readValues(outputHit);
			//^= is a bitwise XOR i.e. we will switch the coordinate sytsem
			cellReencoder["properties"] = properties ^= kHitInGlobalCoord;
			cellReencoder.setCellID(outputHit);

			outputCollection->push_back(outputHit);
		}
	
		//Now push the hit for this event onto the collection
		try
		{	
				event->addCollection(outputCollection, _hitCollectionNameOutput );
		}
		catch(...)
		{
				streamlog_out ( WARNING5 )  << "Problem with pushing collection onto event" << std::endl;
		}
}